55 lines
1.6 KiB
C#
55 lines
1.6 KiB
C#
namespace UtopiaCanteenSystem.Services;
|
|
|
|
/// <summary>
|
|
/// Server-side backend dependencies: SQLite, RFID processing, HRMS cache sync, production post.
|
|
/// Only constructed when <see cref="Models.AppMode.Server"/>.
|
|
/// </summary>
|
|
public sealed class CanteenBackendServices
|
|
{
|
|
private readonly SemaphoreSlim _cacheSyncLock = new(1, 1);
|
|
private readonly SemaphoreSlim _orderSyncLock = new(1, 1);
|
|
|
|
public CanteenBackendServices(
|
|
RfidService rfid,
|
|
IOfflineCacheSyncService offlineCacheSync,
|
|
ISyncService productionSync,
|
|
IConfigService config)
|
|
{
|
|
Rfid = rfid;
|
|
OfflineCacheSync = offlineCacheSync;
|
|
ProductionSync = productionSync;
|
|
Config = config;
|
|
}
|
|
|
|
public RfidService Rfid { get; }
|
|
public IOfflineCacheSyncService OfflineCacheSync { get; }
|
|
public ISyncService ProductionSync { get; }
|
|
public IConfigService Config { get; }
|
|
|
|
public async Task<T> RunCacheSyncExclusiveAsync<T>(Func<Task<T>> action, CancellationToken cancellationToken = default)
|
|
{
|
|
await _cacheSyncLock.WaitAsync(cancellationToken).ConfigureAwait(false);
|
|
try
|
|
{
|
|
return await action().ConfigureAwait(false);
|
|
}
|
|
finally
|
|
{
|
|
_cacheSyncLock.Release();
|
|
}
|
|
}
|
|
|
|
public async Task<T> RunOrderSyncExclusiveAsync<T>(Func<Task<T>> action, CancellationToken cancellationToken = default)
|
|
{
|
|
await _orderSyncLock.WaitAsync(cancellationToken).ConfigureAwait(false);
|
|
try
|
|
{
|
|
return await action().ConfigureAwait(false);
|
|
}
|
|
finally
|
|
{
|
|
_orderSyncLock.Release();
|
|
}
|
|
}
|
|
}
|