2023-01-22 18:35:57 +00:00
|
|
|
|
using DataModel.Database.Common;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Timer = System.Timers.Timer;
|
|
|
|
|
|
|
|
|
|
namespace Processor;
|
|
|
|
|
|
|
|
|
|
public static class ExportProcessor
|
|
|
|
|
{
|
|
|
|
|
private static readonly Timer EXPORT_TIMER = new();
|
2023-01-22 20:50:15 +00:00
|
|
|
|
private static readonly SemaphoreSlim EXPORT_SEMAPHORE_BRICK_WALL = new(2);
|
|
|
|
|
private static readonly SemaphoreSlim EXPORT_SEMAPHORE_EXPORTING = new(1);
|
2023-01-22 18:35:57 +00:00
|
|
|
|
|
|
|
|
|
static ExportProcessor()
|
|
|
|
|
{
|
|
|
|
|
EXPORT_TIMER.Interval = 6_000; // 6 seconds
|
|
|
|
|
EXPORT_TIMER.AutoReset = false;
|
|
|
|
|
EXPORT_TIMER.Elapsed += async (sender, args) => await ExportToJson();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static async Task ExportToJson()
|
|
|
|
|
{
|
2023-01-22 20:50:15 +00:00
|
|
|
|
if(!await EXPORT_SEMAPHORE_BRICK_WALL.WaitAsync(1))
|
2023-01-22 18:35:57 +00:00
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2023-01-22 20:50:15 +00:00
|
|
|
|
await EXPORT_SEMAPHORE_EXPORTING.WaitAsync();
|
2023-01-22 18:35:57 +00:00
|
|
|
|
await using var db = ProcessorMeta.ServiceProvider.GetRequiredService<DataContext>();
|
|
|
|
|
await db.ExportAsync(Environment.ExpandEnvironmentVariables(Path.Join(await AppSettings.GetAutoExportDestinationPath(), await AppSettings.GetAutoExportFilename())));
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
2023-01-22 20:50:15 +00:00
|
|
|
|
EXPORT_SEMAPHORE_EXPORTING.Release();
|
|
|
|
|
EXPORT_SEMAPHORE_BRICK_WALL.Release();
|
2023-01-22 18:35:57 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static async Task TriggerExport()
|
|
|
|
|
{
|
|
|
|
|
if (!await AppSettings.GetAutoExportEnabled())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
EXPORT_TIMER.Stop();
|
|
|
|
|
EXPORT_TIMER.Start();
|
|
|
|
|
}
|
|
|
|
|
}
|