fix: make default initialization concurrency-safe
This commit is contained in:
@@ -357,6 +357,7 @@ public sealed class ApplicationChecklistService : IApplicationChecklistService
|
||||
var missing = Defaults.Where(t => !existing.Contains(t.Key)).ToList();
|
||||
if (missing.Count == 0) return items;
|
||||
|
||||
var seeded = new List<ApplicationChecklistItem>(missing.Count);
|
||||
var order = 0;
|
||||
foreach (var template in Defaults)
|
||||
{
|
||||
@@ -377,12 +378,27 @@ public sealed class ApplicationChecklistService : IApplicationChecklistService
|
||||
};
|
||||
_db.ApplicationChecklistItems.Add(item);
|
||||
items.Add(item);
|
||||
seeded.Add(item);
|
||||
}
|
||||
order++;
|
||||
}
|
||||
|
||||
await _db.SaveChangesAsync(ct);
|
||||
return items;
|
||||
try
|
||||
{
|
||||
await _db.SaveChangesAsync(ct);
|
||||
return items;
|
||||
}
|
||||
catch (DbUpdateException)
|
||||
{
|
||||
foreach (var item in seeded) _db.Entry(item).State = EntityState.Detached;
|
||||
|
||||
var persisted = await LoadItemsAsync(ownerUserId, jobApplicationId, ct);
|
||||
var persistedKeys = persisted.Where(i => i.SystemKey is not null)
|
||||
.Select(i => i.SystemKey!)
|
||||
.ToHashSet(StringComparer.Ordinal);
|
||||
if (missing.Any(template => !persistedKeys.Contains(template.Key))) throw;
|
||||
return persisted;
|
||||
}
|
||||
}
|
||||
|
||||
private async Task SyncAutoCompletionAsync(List<ApplicationChecklistItem> items, ChecklistSignals signals, CancellationToken ct)
|
||||
|
||||
Reference in New Issue
Block a user