Thorsten Sommer
0587af18f2
- Added import algorithm - Added FromJsonX() methods to data models - Added possibility to work with temp. database file - Added export processor to handle export process & triggers - Added something changed event e.g. as export trigger - Added possibility to import a JSON export - Updated Git icon
46 lines
1.4 KiB
C#
46 lines
1.4 KiB
C#
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();
|
|
private static readonly object EXPORT_LOCK = new();
|
|
private static readonly SemaphoreSlim EXPORT_SEMAPHORE = new(2);
|
|
|
|
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()
|
|
{
|
|
if(!await EXPORT_SEMAPHORE.WaitAsync(1))
|
|
return;
|
|
|
|
Monitor.Enter(EXPORT_LOCK);
|
|
try
|
|
{
|
|
await using var db = ProcessorMeta.ServiceProvider.GetRequiredService<DataContext>();
|
|
await db.ExportAsync(Environment.ExpandEnvironmentVariables(Path.Join(await AppSettings.GetAutoExportDestinationPath(), await AppSettings.GetAutoExportFilename())));
|
|
}
|
|
finally
|
|
{
|
|
Monitor.Exit(EXPORT_LOCK);
|
|
EXPORT_SEMAPHORE.Release();
|
|
}
|
|
}
|
|
|
|
public static async Task TriggerExport()
|
|
{
|
|
if (!await AppSettings.GetAutoExportEnabled())
|
|
return;
|
|
|
|
EXPORT_TIMER.Stop();
|
|
EXPORT_TIMER.Start();
|
|
}
|
|
} |