74 lines
4.6 KiB
C#
74 lines
4.6 KiB
C#
using System.Data;
|
|
using Dapper;
|
|
using PlotLine.Models;
|
|
namespace PlotLine.Data;
|
|
|
|
public interface IGeographicQueue
|
|
{
|
|
Task<GeographicJob?> ClaimAsync(bool enabled);
|
|
Task<IReadOnlyList<LocationItem>> ChainAsync(int locationId);
|
|
Task<string?> CacheAsync(string requestKey);
|
|
Task<bool> ReserveAsync(string provider,TimeSpan interval,string requestKey,Guid lease);
|
|
Task ReleaseAsync(Guid lease,int cooldownSeconds=0);
|
|
Task CompleteAsync(GeographicJob job,string status,string jobState,string? key=null,string? candidates=null,string? match=null,string? message=null,bool failure=false,int delay=0);
|
|
}
|
|
|
|
public sealed class GeographyRepository(ISqlConnectionFactory connections) : IGeographicQueue
|
|
{
|
|
public async Task<IReadOnlyList<LocationGeography>> GetAsync(int projectId, int userId, int? locationId = null)
|
|
{
|
|
using var db = connections.CreateConnection();
|
|
return (await db.QueryAsync<LocationGeography>("dbo.Geography_Get", new { ProjectID=projectId, UserID=userId, LocationID=locationId }, commandType:CommandType.StoredProcedure)).ToList();
|
|
}
|
|
public async Task<GeographicJob?> ClaimAsync(bool enabled)
|
|
{
|
|
using var db=connections.CreateConnection();
|
|
return await db.QuerySingleOrDefaultAsync<GeographicJob>("dbo.Geography_Claim",new { Enabled=enabled },commandType:CommandType.StoredProcedure);
|
|
}
|
|
public async Task<IReadOnlyList<LocationItem>> ChainAsync(int locationId)
|
|
{
|
|
using var db=connections.CreateConnection();
|
|
return (await db.QueryAsync<LocationItem>("dbo.Geography_Chain",new {LocationID=locationId},commandType:CommandType.StoredProcedure)).ToList();
|
|
}
|
|
public async Task<string?> CacheAsync(string requestKey)
|
|
{
|
|
using var db=connections.CreateConnection();
|
|
return await db.QuerySingleOrDefaultAsync<string>("dbo.Geography_Cache",new {RequestKey=requestKey},commandType:CommandType.StoredProcedure);
|
|
}
|
|
public async Task<bool> ReserveAsync(string provider,TimeSpan interval,string requestKey,Guid lease)
|
|
{
|
|
using var db=connections.CreateConnection();
|
|
return await db.QuerySingleAsync<bool>("dbo.Geography_Reserve",new {Provider=provider,RequestKey=requestKey,Lease=lease,IntervalMs=(int)Math.Clamp(interval.TotalMilliseconds,0,86400000)},commandType:CommandType.StoredProcedure);
|
|
}
|
|
public async Task CompleteAsync(GeographicJob job,string status,string jobState,string? key=null,string? candidates=null,string? match=null,string? message=null,bool failure=false,int delay=0)
|
|
{
|
|
using var db=connections.CreateConnection();
|
|
await db.ExecuteAsync("dbo.Geography_Complete",new {job.LocationID,job.Generation,job.Lease,Status=status,JobState=jobState,RequestKey=key,CandidatesJson=candidates,MatchJson=match,Message=message,Failure=failure,DelaySeconds=delay},commandType:CommandType.StoredProcedure);
|
|
}
|
|
public async Task ChooseAsync(int projectId,int userId,int locationId,long generation,int candidateIndex)
|
|
{
|
|
using var db=connections.CreateConnection();
|
|
await db.ExecuteAsync("dbo.Geography_Choose",new {ProjectID=projectId,UserID=userId,LocationID=locationId,Generation=generation,CandidateIndex=candidateIndex},commandType:CommandType.StoredProcedure);
|
|
}
|
|
public async Task RetryAsync(int projectId,int userId,int locationId,long generation,bool replaceConfirmed=false)
|
|
{
|
|
using var db=connections.CreateConnection();
|
|
await db.ExecuteAsync("dbo.Geography_Retry",new {ProjectID=projectId,UserID=userId,LocationID=locationId,Generation=generation,ReplaceConfirmed=replaceConfirmed},commandType:CommandType.StoredProcedure);
|
|
}
|
|
public async Task ExcludeAsync(int projectId,int userId,int locationId,long generation,bool isExcluded)
|
|
{
|
|
using var db=connections.CreateConnection();
|
|
await db.ExecuteAsync("dbo.Geography_Exclude",new {ProjectID=projectId,UserID=userId,LocationID=locationId,Generation=generation,IsExcluded=isExcluded},commandType:CommandType.StoredProcedure);
|
|
}
|
|
public async Task SubmitAsync(int projectId,int userId,int locationId,long generation)
|
|
{
|
|
using var db=connections.CreateConnection();
|
|
await db.ExecuteAsync("dbo.Geography_SubmitHierarchy",new {ProjectID=projectId,UserID=userId,LocationID=locationId,Generation=generation},commandType:CommandType.StoredProcedure);
|
|
}
|
|
public async Task ReleaseAsync(Guid lease,int cooldownSeconds=0)
|
|
{
|
|
using var db=connections.CreateConnection();
|
|
await db.ExecuteAsync("dbo.Geography_ProviderRelease",new {Lease=lease,CooldownSeconds=cooldownSeconds},commandType:CommandType.StoredProcedure);
|
|
}
|
|
}
|