fix(news): use CreateAsyncScope for Playwright service and gracefully handle DbUpdateException on duplicate URLs
This commit is contained in:
@@ -158,10 +158,23 @@ public class NewsDbService : INewsDbService
|
||||
await _context.SaveChangesAsync();
|
||||
await _finlyticLogger.LogDebugAsync(SettingKeys.NewsChannel, "[Lifecycle] Registered new article with status 'Pending'. ID: {Id}, Url: {Url}", article.Id, trimmedUrl);
|
||||
}
|
||||
catch (DbUpdateConcurrencyException)
|
||||
catch (DbUpdateException ex)
|
||||
{
|
||||
await _finlyticLogger.LogWarningAsync(SettingKeys.NewsChannel, "[NewsChannel] Concurrency hit during insert for URL: {Url}. Fetching existing fallback.", trimmedUrl);
|
||||
return await _context.NewsArticles.FirstAsync(a => a.SourceUrl == trimmedUrl);
|
||||
_context.Entry(article).State = EntityState.Detached;
|
||||
if (article.MatchedAssets != null)
|
||||
{
|
||||
foreach (var match in article.MatchedAssets)
|
||||
{
|
||||
_context.Entry(match).State = EntityState.Detached;
|
||||
}
|
||||
}
|
||||
await _finlyticLogger.LogWarningAsync(SettingKeys.NewsChannel, "[NewsChannel] Unique constraint or concurrency hit during insert for URL: {Url}. Fetching existing fallback. ({Message})", trimmedUrl, ex.InnerException?.Message ?? ex.Message);
|
||||
var existing = await _context.NewsArticles.AsNoTracking().FirstOrDefaultAsync(a => a.SourceUrl == trimmedUrl);
|
||||
if (existing != null)
|
||||
{
|
||||
return existing;
|
||||
}
|
||||
throw;
|
||||
}
|
||||
|
||||
return article;
|
||||
|
||||
Reference in New Issue
Block a user