Skip to content

Commit

Permalink
Fix issue with Retry Policy
Browse files Browse the repository at this point in the history
Log message contained non-existent context key.
  • Loading branch information
Lee Wadhams committed Jun 6, 2018
1 parent 981f50f commit bbab9d4
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,6 @@ protected IMongoCollection<T> GetCollection<T>()
return collection;
}

protected Task Retry(Func<Task> func)
{
var policy = GetRetryPolicy();

return policy.ExecuteAsync(func);
}

protected Task<T> Retry<T>(Func<Task<T>> func)
{
var policy = GetRetryPolicy();

return policy.ExecuteAsync(func);
}

private Polly.Retry.RetryPolicy GetRetryPolicy()
{
return Policy
Expand All @@ -63,7 +49,7 @@ private Polly.Retry.RetryPolicy GetRetryPolicy()
TimeSpan.FromSeconds(2),
TimeSpan.FromSeconds(4)
}, (exception, timeSpan, retryCount, context) => {
_logger.LogWarning($"Error executing Mongo Command for {context["methodName"]}. Retrying in {timeSpan.Seconds} secs...attempt: {retryCount}");
_logger.LogWarning($"Error executing Mongo Command for method {context.OperationKey}. Retrying in {timeSpan.Seconds} secs...attempt: {retryCount}");
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using MongoDB.Driver;
using Polly;

namespace Esfa.Recruit.Vacancies.Client.Infrastructure.QueryStore
{
Expand All @@ -26,7 +27,7 @@ async Task<bool> IQueryStore.DeleteAsync<T>(string key)
throw new InfrastructureException($"Expected that collection: {Collection} would already be created.");

var filter = Builders<T>.Filter.Eq(d => d.Id, key);
var result = await RetryPolicy.ExecuteAsync(() => collection.DeleteOneAsync(filter));
var result = await RetryPolicy.ExecuteAsync(context => collection.DeleteOneAsync(filter), new Context(nameof(IQueryStore.DeleteAsync)));

return result.DeletedCount == 1;
}
Expand All @@ -36,7 +37,7 @@ async Task<IEnumerable<T>> IQueryStore.GetAllByTypeAsync<T>(string typeName)
var filter = Builders<T>.Filter.Eq(d => d.Type, typeName);

var collection = GetCollection<T>();
var result = await RetryPolicy.ExecuteAsync(() => collection.FindAsync(filter));
var result = await RetryPolicy.ExecuteAsync(context => collection.FindAsync(filter), new Context(nameof(IQueryStore.GetAllByTypeAsync)));

return result?.ToEnumerable();
}
Expand All @@ -46,7 +47,7 @@ async Task<T> IQueryStore.GetAsync<T>(string key)
var filter = Builders<T>.Filter.Eq(d => d.Id, key);

var collection = GetCollection<T>();
var result = await RetryPolicy.ExecuteAsync(() => collection.FindAsync(filter));
var result = await RetryPolicy.ExecuteAsync(context => collection.FindAsync(filter), new Context(nameof(IQueryStore.GetAsync)));

return result?.FirstOrDefault();
}
Expand All @@ -60,7 +61,7 @@ Task IQueryStore.UpsertAsync<T>(T item)

var filter = Builders<T>.Filter.Eq(d => d.Id, item.Id);

return RetryPolicy.ExecuteAsync(() => collection.ReplaceOneAsync(filter, item, new UpdateOptions { IsUpsert = true }));
return RetryPolicy.ExecuteAsync(context => collection.ReplaceOneAsync(filter, item, new UpdateOptions { IsUpsert = true }), new Context(nameof(IQueryStore.UpsertAsync)));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using MongoDB.Driver;
using Polly;

namespace Esfa.Recruit.Vacancies.Client.Infrastructure.Repositories
{
Expand All @@ -23,15 +24,15 @@ public async Task<User> GetAsync(string idamsUserId)
var filter = Builders<User>.Filter.Eq(v => v.IdamsUserId, idamsUserId);

var collection = GetCollection<User>();
var result = await RetryPolicy.ExecuteAsync(() => collection.FindAsync(filter));
var result = await RetryPolicy.ExecuteAsync(context => collection.FindAsync(filter), new Context(nameof(GetAsync)));
return result.SingleOrDefault();
}

public Task UpsertUserAsync(User user)
{
var filter = Builders<User>.Filter.Eq(v => v.Id, user.Id);
var collection = GetCollection<User>();
return RetryPolicy.ExecuteAsync(() => collection.ReplaceOneAsync(filter, user, new UpdateOptions { IsUpsert = true }));
return RetryPolicy.ExecuteAsync(context => collection.ReplaceOneAsync(filter, user, new UpdateOptions { IsUpsert = true }), new Context(nameof(UpsertUserAsync)));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Esfa.Recruit.Vacancies.Client.Application.Exceptions;
using Esfa.Recruit.Vacancies.Client.Infrastructure.Exceptions;
using Microsoft.Extensions.Logging;
using Polly;

namespace Esfa.Recruit.Vacancies.Client.Infrastructure.Repositories
{
Expand All @@ -29,7 +30,7 @@ public MongoDbVacancyRepository(ILogger<MongoDbVacancyRepository> logger, IOptio
public async Task CreateAsync(Vacancy vacancy)
{
var collection = GetCollection<Vacancy>();
await RetryPolicy.ExecuteAsync(() => collection.InsertOneAsync(vacancy));
await RetryPolicy.ExecuteAsync(context => collection.InsertOneAsync(vacancy), new Context(nameof(CreateAsync)));
}

public async Task<Vacancy> GetVacancyAsync(long vacancyReference)
Expand Down Expand Up @@ -58,7 +59,7 @@ private async Task<Vacancy> FindVacancy<TField>(Expression<Func<Vacancy, TField>
var options = new FindOptions<Vacancy> { Limit = 1 };

var collection = GetCollection<Vacancy>();
var result = await RetryPolicy.ExecuteAsync(() => collection.FindAsync(filter, options));
var result = await RetryPolicy.ExecuteAsync(context => collection.FindAsync(filter, options), new Context(nameof(FindVacancy)));
return result.SingleOrDefault();
}

Expand All @@ -68,7 +69,7 @@ public async Task<IEnumerable<T>> GetVacanciesByEmployerAccountAsync<T>(string e

var collection = GetCollection<BsonDocument>();

var result = await RetryPolicy.ExecuteAsync(() => collection.FindAsync<T>(filter));
var result = await RetryPolicy.ExecuteAsync(context => collection.FindAsync<T>(filter), new Context(nameof(GetVacanciesByEmployerAccountAsync)));

return await result.ToListAsync();
}
Expand All @@ -87,15 +88,15 @@ public async Task<Vacancy> GetSingleVacancyForPostcode(string postcode)
};

var collection = GetCollection<Vacancy>();
var result = await RetryPolicy.ExecuteAsync(() => collection.FindAsync(filter, options));
var result = await RetryPolicy.ExecuteAsync(context => collection.FindAsync(filter, options), new Context(nameof(GetSingleVacancyForPostcode)));
return result.SingleOrDefault();
}

public async Task UpdateAsync(Vacancy vacancy)
{
var filter = Builders<Vacancy>.Filter.Eq(v => v.Id, vacancy.Id);
var collection = GetCollection<Vacancy>();
await RetryPolicy.ExecuteAsync(() => collection.ReplaceOneAsync(filter, vacancy));
await RetryPolicy.ExecuteAsync(context => collection.ReplaceOneAsync(filter, vacancy), new Context(nameof(UpdateAsync)));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using MongoDB.Driver;
using Polly;

namespace Esfa.Recruit.Vacancies.Client.Infrastructure.Repositories
{
Expand All @@ -24,25 +25,25 @@ public MongoDbVacancyReviewRepository(ILogger<MongoDbVacancyReviewRepository> lo
public Task CreateAsync(VacancyReview vacancy)
{
var collection = GetCollection<VacancyReview>();
return RetryPolicy.ExecuteAsync(() => collection.InsertOneAsync(vacancy));
return RetryPolicy.ExecuteAsync(context => collection.InsertOneAsync(vacancy), new Context(nameof(CreateAsync)));
}

public async Task<VacancyReview> GetAsync(Guid reviewId)
{
var filter = Builders<VacancyReview>.Filter.Eq(r => r.Id, reviewId);

var collection = GetCollection<VacancyReview>();
var result = await RetryPolicy.ExecuteAsync(() => collection.FindAsync(filter));
var result = await RetryPolicy.ExecuteAsync(context => collection.FindAsync(filter), new Context(nameof(GetAsync)));
return result.SingleOrDefault();
}

public async Task<IEnumerable<VacancyReview>> GetAllAsync()
{
var collection = GetCollection<VacancyReview>();
var result = await RetryPolicy.ExecuteAsync(() => collection
var result = await RetryPolicy.ExecuteAsync(context => collection
.Find(FilterDefinition<VacancyReview>.Empty)
.Sort(Builders<VacancyReview>.Sort.Descending(r => r.CreatedDate))
.ToListAsync());
.ToListAsync(), new Context(nameof(GetAllAsync)));

return result;
}
Expand All @@ -53,7 +54,7 @@ public Task UpdateAsync(VacancyReview review)
var filter = filterBuilder.Eq(v => v.Id, review.Id) & filterBuilder.Eq(v => v.VacancyReference, review.VacancyReference);
var collection = GetCollection<VacancyReview>();

return RetryPolicy.ExecuteAsync(() => collection.ReplaceOneAsync(filter, review));
return RetryPolicy.ExecuteAsync(context => collection.ReplaceOneAsync(filter, review), new Context(nameof(UpdateAsync)));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using MongoDB.Driver;
using Polly;

namespace Esfa.Recruit.Vacancies.Client.Infrastructure.SequenceStore
{
Expand All @@ -26,7 +27,7 @@ public async Task<long> GenerateAsync()
var update = Builders<Sequence>.Update.Inc(x => x.LastValue, 1);
var options = new FindOneAndUpdateOptions<Sequence> { ReturnDocument = ReturnDocument.After };

var newSequence = await RetryPolicy.ExecuteAsync(() => collection.FindOneAndUpdateAsync(filter, update, options));
var newSequence = await RetryPolicy.ExecuteAsync(context => collection.FindOneAndUpdateAsync(filter, update, options), new Context(nameof(GenerateAsync)));

if (newSequence == null)
{
Expand Down

0 comments on commit bbab9d4

Please sign in to comment.