House rules: primary constructors, a cancellation token on every async call, and the rules fail the build
Build / Build (push) Successful in 30s
Build / Build (push) Successful in 30s
- Services, controllers and jobs take their dependencies through primary constructors and use them directly; where a method already named a local after a dependency, the dependency takes its type's name. - Every async method takes a CancellationToken last and passes it down to MongoDB, HTTP and the job queue. The setup claim is still given back when a request is cancelled. - The federation and media options are read through IOptionsMonitor. - The Mastodon entities keep sending "" for empty strings, and every model field where null means something says so. - The inbox's 503 with Retry-After, which other servers retry, is the one server error left, and says why. - Arasaka.HouseRules.Analyzers checks all of this in every build, here and in PrivaPub.ClientModels as decePubClient builds it. Checked against GoToSocial in the pasture: 64 passed, 0 failed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01W4YtEJrgxG12Pc39Fai8Bd
This commit is contained in:
1 parent
f0a14c737f
commit
0f73f69239
176 files changed
+2789
-3974
No files matched your search
@@ -13,17 +13,12 @@ namespace PrivaPub.Infrastructure.Jobs
|
||||
Task Failed(string host, string error, CancellationToken token);
|
||||
}
|
||||
|
||||
public class HostCircuitBreaker : IHostCircuitBreaker
|
||||
public class HostCircuitBreaker(
|
||||
IMemoryCache cache) : IHostCircuitBreaker
|
||||
{
|
||||
public const int Threshold = 10;
|
||||
static readonly TimeSpan CacheLifetime = TimeSpan.FromSeconds(30);
|
||||
|
||||
readonly IMemoryCache _cache;
|
||||
|
||||
public HostCircuitBreaker(IMemoryCache cache)
|
||||
{
|
||||
_cache = cache;
|
||||
}
|
||||
|
||||
public async Task<DateTime?> UnavailableUntil(string host, CancellationToken token)
|
||||
{
|
||||
@@ -43,7 +38,7 @@ namespace PrivaPub.Infrastructure.Jobs
|
||||
.Modify(i => i.LastSuccessAt, DateTime.UtcNow)
|
||||
.Option(o => o.IsUpsert = true)
|
||||
.ExecuteAsync(token);
|
||||
_cache.Remove(Key(host));
|
||||
cache.Remove(Key(host));
|
||||
}
|
||||
|
||||
public async Task Failed(string host, string error, CancellationToken token)
|
||||
@@ -61,11 +56,11 @@ namespace PrivaPub.Infrastructure.Jobs
|
||||
await DB.Default.Update<RemoteInstance>().MatchID(instance.ID)
|
||||
.Modify(i => i.UnavailableUntil, now + quarantine)
|
||||
.ExecuteAsync(token);
|
||||
_cache.Remove(Key(host));
|
||||
cache.Remove(Key(host));
|
||||
}
|
||||
|
||||
async Task<RemoteInstance> Instance(string host, CancellationToken token) =>
|
||||
await _cache.GetOrCreateAsync(Key(host), async entry =>
|
||||
await cache.GetOrCreateAsync(Key(host), async entry =>
|
||||
{
|
||||
entry.AbsoluteExpirationRelativeToNow = CacheLifetime;
|
||||
return await DB.Default.Find<RemoteInstance>().Match(i => i.Host == host).ExecuteFirstAsync(token);
|
||||
|
||||
@@ -14,25 +14,18 @@ namespace PrivaPub.Infrastructure.Jobs
|
||||
Task<JobOutcome> Handle(Job job, CancellationToken token);
|
||||
}
|
||||
|
||||
public class JobWorker : BackgroundService
|
||||
public class JobWorker(
|
||||
IJobQueue jobQueue,
|
||||
IEnumerable<IJobHandler> handlers,
|
||||
ILogger<JobWorker> logger) : BackgroundService
|
||||
{
|
||||
static readonly TimeSpan Poll = TimeSpan.FromSeconds(5);
|
||||
static readonly TimeSpan ReapInterval = TimeSpan.FromSeconds(30);
|
||||
|
||||
readonly IJobQueue _queue;
|
||||
readonly IEnumerable<IJobHandler> _handlers;
|
||||
readonly ILogger<JobWorker> _logger;
|
||||
|
||||
public JobWorker(IJobQueue queue, IEnumerable<IJobHandler> handlers, ILogger<JobWorker> logger)
|
||||
{
|
||||
_queue = queue;
|
||||
_handlers = handlers;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
var loops = _handlers.SelectMany(handler =>
|
||||
var loops = handlers.SelectMany(handler =>
|
||||
{
|
||||
var inFlight = new ConcurrentDictionary<string, int>(StringComparer.OrdinalIgnoreCase);
|
||||
return Enumerable.Range(0, handler.Concurrency).Select(_ => Run(handler, inFlight, stoppingToken));
|
||||
@@ -44,11 +37,11 @@ namespace PrivaPub.Infrastructure.Jobs
|
||||
{
|
||||
while (!stoppingToken.IsCancellationRequested)
|
||||
{
|
||||
Job job;
|
||||
var job = default(Job);
|
||||
try
|
||||
{
|
||||
var busy = inFlight.Where(h => h.Value >= handler.PerHostLimit).Select(h => h.Key).ToList();
|
||||
job = await _queue.Lease(handler.Kind, busy, stoppingToken);
|
||||
job = await jobQueue.Lease(handler.Kind, busy, stoppingToken);
|
||||
}
|
||||
catch (OperationCanceledException) when (stoppingToken.IsCancellationRequested)
|
||||
{
|
||||
@@ -56,14 +49,14 @@ namespace PrivaPub.Infrastructure.Jobs
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "{Worker} could not lease a {Kind} job", nameof(JobWorker), handler.Kind);
|
||||
logger.LogError(ex, "{Worker} could not lease a {Kind} job", nameof(JobWorker), handler.Kind);
|
||||
await Delay(Poll, stoppingToken);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (job == default)
|
||||
{
|
||||
await _queue.WaitForWork(handler.Kind, Poll, stoppingToken);
|
||||
await jobQueue.WaitForWork(handler.Kind, Poll, stoppingToken);
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -75,7 +68,7 @@ namespace PrivaPub.Infrastructure.Jobs
|
||||
var renewing = KeepLease(job, running);
|
||||
try
|
||||
{
|
||||
JobOutcome outcome;
|
||||
var outcome = default(JobOutcome);
|
||||
try
|
||||
{
|
||||
using var scope = HttpScope.Triggered(handler.Kind.ToString().ToLowerInvariant());
|
||||
@@ -87,24 +80,24 @@ namespace PrivaPub.Infrastructure.Jobs
|
||||
}
|
||||
catch (OperationCanceledException) when (running.IsCancellationRequested)
|
||||
{
|
||||
_logger.LogWarning("{Kind} job {Id} lost its lease and was stopped", handler.Kind, job.ID);
|
||||
logger.LogWarning("{Kind} job {Id} lost its lease and was stopped", handler.Kind, job.ID);
|
||||
continue;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "{Kind} job {Id} threw", handler.Kind, job.ID);
|
||||
logger.LogError(ex, "{Kind} job {Id} threw", handler.Kind, job.ID);
|
||||
outcome = JobOutcome.Retry(ex.GetType().Name);
|
||||
}
|
||||
|
||||
if (outcome.Result != JobResult.Done && job.Attempts >= handler.MaxAttempts && outcome.Result == JobResult.Retry)
|
||||
_logger.LogWarning("{Kind} job {Id} for {Host} is dead after {Attempts} attempts: {Error}",
|
||||
logger.LogWarning("{Kind} job {Id} for {Host} is dead after {Attempts} attempts: {Error}",
|
||||
handler.Kind, job.ID, job.Host, job.Attempts, outcome.Error);
|
||||
if (!await _queue.Finish(job, outcome, handler.MaxAttempts, CancellationToken.None))
|
||||
_logger.LogWarning("{Kind} job {Id} lost its lease before it finished; its outcome is dropped", handler.Kind, job.ID);
|
||||
if (!await jobQueue.Finish(job, outcome, handler.MaxAttempts, CancellationToken.None))
|
||||
logger.LogWarning("{Kind} job {Id} lost its lease before it finished; its outcome is dropped", handler.Kind, job.ID);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "{Kind} job {Id} could not be finished; its lease will expire", handler.Kind, job.ID);
|
||||
logger.LogError(ex, "{Kind} job {Id} could not be finished; its lease will expire", handler.Kind, job.ID);
|
||||
}
|
||||
finally
|
||||
{
|
||||
@@ -116,9 +109,10 @@ namespace PrivaPub.Infrastructure.Jobs
|
||||
}
|
||||
|
||||
// renews the lease every third of its length until the handler ends; a lease found lost cancels the handler
|
||||
[System.Diagnostics.CodeAnalysis.SuppressMessage("HouseRules", "HR0003", Justification = "Takes the source it cancels when the lease is lost; its token already follows the worker's.")]
|
||||
async Task KeepLease(Job job, CancellationTokenSource running)
|
||||
{
|
||||
var every = _queue.LeaseFor / 3;
|
||||
var every = jobQueue.LeaseFor / 3;
|
||||
while (!running.IsCancellationRequested)
|
||||
{
|
||||
try
|
||||
@@ -132,7 +126,7 @@ namespace PrivaPub.Infrastructure.Jobs
|
||||
|
||||
try
|
||||
{
|
||||
if (await _queue.Renew(job, CancellationToken.None))
|
||||
if (await jobQueue.Renew(job, CancellationToken.None))
|
||||
continue;
|
||||
await running.CancelAsync();
|
||||
return;
|
||||
@@ -140,7 +134,7 @@ namespace PrivaPub.Infrastructure.Jobs
|
||||
catch (Exception ex)
|
||||
{
|
||||
// a passing database error: the lease has two more thirds to go, so try again at the next turn
|
||||
_logger.LogWarning(ex, "{Worker} could not renew the lease of job {Id}", nameof(JobWorker), job.ID);
|
||||
logger.LogWarning(ex, "{Worker} could not renew the lease of job {Id}", nameof(JobWorker), job.ID);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -151,9 +145,9 @@ namespace PrivaPub.Infrastructure.Jobs
|
||||
{
|
||||
try
|
||||
{
|
||||
var reaped = await _queue.Reap(stoppingToken);
|
||||
var reaped = await jobQueue.Reap(stoppingToken);
|
||||
if (reaped > 0)
|
||||
_logger.LogWarning("{Worker} returned {Count} expired leases to the queue", nameof(JobWorker), reaped);
|
||||
logger.LogWarning("{Worker} returned {Count} expired leases to the queue", nameof(JobWorker), reaped);
|
||||
}
|
||||
catch (OperationCanceledException) when (stoppingToken.IsCancellationRequested)
|
||||
{
|
||||
@@ -161,7 +155,7 @@ namespace PrivaPub.Infrastructure.Jobs
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "{Worker} reaper failed", nameof(JobWorker));
|
||||
logger.LogError(ex, "{Worker} reaper failed", nameof(JobWorker));
|
||||
}
|
||||
await Delay(ReapInterval, stoppingToken);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user