Fixed export synchronization

- Replaced Monitor by another semaphore, because async method calls are
  not working with monitors!
This commit is contained in:
Thorsten Sommer 2023-01-22 21:50:15 +01:00
parent 35009dba7c
commit 52927d4715
Signed by: tsommer
GPG Key ID: 371BBA77A02C0108

View File

@ -7,8 +7,8 @@ namespace Processor;
public static class ExportProcessor public static class ExportProcessor
{ {
private static readonly Timer EXPORT_TIMER = new(); private static readonly Timer EXPORT_TIMER = new();
private static readonly object EXPORT_LOCK = new(); private static readonly SemaphoreSlim EXPORT_SEMAPHORE_BRICK_WALL = new(2);
private static readonly SemaphoreSlim EXPORT_SEMAPHORE = new(2); private static readonly SemaphoreSlim EXPORT_SEMAPHORE_EXPORTING = new(1);
static ExportProcessor() static ExportProcessor()
{ {
@ -19,19 +19,19 @@ public static class ExportProcessor
private static async Task ExportToJson() private static async Task ExportToJson()
{ {
if(!await EXPORT_SEMAPHORE.WaitAsync(1)) if(!await EXPORT_SEMAPHORE_BRICK_WALL.WaitAsync(1))
return; return;
Monitor.Enter(EXPORT_LOCK);
try try
{ {
await EXPORT_SEMAPHORE_EXPORTING.WaitAsync();
await using var db = ProcessorMeta.ServiceProvider.GetRequiredService<DataContext>(); await using var db = ProcessorMeta.ServiceProvider.GetRequiredService<DataContext>();
await db.ExportAsync(Environment.ExpandEnvironmentVariables(Path.Join(await AppSettings.GetAutoExportDestinationPath(), await AppSettings.GetAutoExportFilename()))); await db.ExportAsync(Environment.ExpandEnvironmentVariables(Path.Join(await AppSettings.GetAutoExportDestinationPath(), await AppSettings.GetAutoExportFilename())));
} }
finally finally
{ {
Monitor.Exit(EXPORT_LOCK); EXPORT_SEMAPHORE_EXPORTING.Release();
EXPORT_SEMAPHORE.Release(); EXPORT_SEMAPHORE_BRICK_WALL.Release();
} }
} }