-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix schedule concurrency errors #2277
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -118,6 +118,41 @@ protected async Task<T> RetryRequestAsync<T>(Func<Task<T>> callbackDelegate) | |
return await RetryRequestAsync<T>(callbackDelegate); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Keep trying a request if the failure is caused by an optimistic concurrency error. | ||
/// </summary> | ||
/// <typeparam name="T"></typeparam> | ||
/// <param name="callbackDelegate"></param> | ||
/// <returns></returns> | ||
public async Task<T> HandleConcurrencyAsync<T>(Func<Task<T>> callbackDelegate) | ||
{ | ||
// Keep trying to update the record and handle concurrency errors. | ||
while (true) | ||
{ | ||
try | ||
{ | ||
return await callbackDelegate(); | ||
} | ||
catch (HttpClientRequestException ex) | ||
{ | ||
// If it's a concurrency error, keep trying. Otherwise throw the error. | ||
this.Logger.LogError(ex, "Failed to complete request. Determining if this is a concurrency error."); | ||
var data = ex.Data["Body"] as string; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I couldn't find a safe way to determine if the key exists in the dictionary. Regrettably .net uses a IDictionary object that doesn't have those features. |
||
if (!String.IsNullOrWhiteSpace(data)) | ||
{ | ||
var json = JsonSerializer.Deserialize<API.Models.ErrorResponseModel>(data, _serializerOptions); | ||
if (json != null && json.Type == nameof(Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException)) | ||
{ | ||
// A concurrency error can be resolved by loading the latest and reapplying the values. | ||
continue; | ||
} | ||
} | ||
// It wasn't a concurrency error, throw as a real failure. | ||
throw; | ||
} | ||
} | ||
} | ||
#endregion | ||
|
||
#region Kafka Methods | ||
|
@@ -826,13 +861,19 @@ protected async Task<T> RetryRequestAsync<T>(Func<Task<T>> callbackDelegate) | |
|
||
/// <summary> | ||
/// Make a request to the API to update the event schedule for the specified 'model'. | ||
/// The most common issue with this endpoint is concurrency errors. Retrying won't fix that, so use the HandleConcurrency function and set retry = false. | ||
/// </summary> | ||
/// <param name="model"></param> | ||
/// <param name="retry"></param> | ||
/// <returns></returns> | ||
public async Task<API.Areas.Services.Models.EventSchedule.EventScheduleModel?> UpdateEventScheduleAsync(API.Areas.Services.Models.EventSchedule.EventScheduleModel model) | ||
public async Task<API.Areas.Services.Models.EventSchedule.EventScheduleModel?> UpdateEventScheduleAsync(API.Areas.Services.Models.EventSchedule.EventScheduleModel model, bool retry = true) | ||
{ | ||
var url = this.Options.ApiUrl.Append($"services/events/schedules/{model.Id}"); | ||
return await RetryRequestAsync(async () => await this.OpenClient.PutAsync<API.Areas.Services.Models.EventSchedule.EventScheduleModel?>(url, JsonContent.Create(model))); | ||
|
||
if (retry) | ||
return await RetryRequestAsync(async () => await this.OpenClient.PutAsync<API.Areas.Services.Models.EventSchedule.EventScheduleModel?>(url, JsonContent.Create(model))); | ||
else | ||
return await this.OpenClient.PutAsync<API.Areas.Services.Models.EventSchedule.EventScheduleModel?>(url, JsonContent.Create(model)); | ||
} | ||
#endregion | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Essentially keep trying until successful. It will only error out if the exception is not a optimistic concurrency error.