• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ImmediatePlatform / Immediate.Jobs / 30640252480

31 Jul 2026 02:49PM UTC coverage: 83.214% (+0.1%) from 83.088%
30640252480

push

github

web-flow
Fast-forward scheduled jobs from the dashboard (#80)

23 of 25 new or added lines in 4 files covered. (92.0%)

5 existing lines in 1 file now uncovered.

7466 of 8972 relevant lines covered (83.21%)

2.75 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

83.17
/src/Immediate.Jobs.EntityFrameworkCore/EntityFrameworkCoreJobStorage.cs
1
using System.Data.Common;
2
using Microsoft.EntityFrameworkCore;
3

4
// TODO: remove and fix diagnostics
5
#pragma warning disable MA0015 // Specify the parameter name in ArgumentException
6

7
namespace Immediate.Jobs.EntityFrameworkCore;
8

9
/// <summary>An optimistic-concurrency EF Core implementation of <see cref="IJobStorage"/>.</summary>
10
/// <typeparam name="TContext">The application context containing the Immediate.Jobs model.</typeparam>
11
/// <param name="contextFactory">The factory used to create application database contexts.</param>
12
/// <param name="timeProvider">The clock used for storage timestamps, or <see langword="null"/> to use the system clock.</param>
13
public sealed class EntityFrameworkCoreJobStorage<TContext>(
5✔
14
        IDbContextFactory<TContext> contextFactory,
5✔
15
        TimeProvider? timeProvider = null
5✔
16
) : IRecurringJobStorage, IJobGraphStorage, IJobStorageReplica
5✔
17
        where TContext : DbContext
18
{
19
        private const int MaxConcurrencyAttempts = 5;
20
        private const int MaxConsecutiveFailedFairClaims = 5;
21
        private readonly TimeProvider _timeProvider = timeProvider ?? TimeProvider.System;
5✔
22

23
        /// <inheritdoc />
24
        public ValueTask DisposeAsync() => ValueTask.CompletedTask;
5✔
25

26
        /// <inheritdoc />
27
        public async ValueTask InitializeAsync(CancellationToken cancellationToken = default)
28
        {
29
                await using var context = await contextFactory.CreateDbContextAsync(cancellationToken).ConfigureAwait(false);
4✔
30
                _ = context.Model.FindEntityType(typeof(ImmediateJobEntity))
4✔
31
                        ?? throw new ImmediateJobException("Immediate.Jobs entities are not configured. Call modelBuilder.AddImmediateJobs() from OnModelCreating.");
4✔
32
        }
3✔
33

34
        /// <inheritdoc />
35
        public async ValueTask EnqueueAsync(JobRecord job, CancellationToken cancellationToken = default)
36
        {
37
                ArgumentNullException.ThrowIfNull(job);
5✔
38
                await ExecuteWithStrategyAsync(
5✔
39
                        operationCancellationToken => EnqueueCoreAsync(job, operationCancellationToken),
5✔
40
                        cancellationToken
5✔
41
                ).ConfigureAwait(false);
5✔
42
        }
5✔
43

44
        private async Task EnqueueCoreAsync(JobRecord job, CancellationToken cancellationToken)
45
        {
46
                await using var context = await contextFactory.CreateDbContextAsync(cancellationToken).ConfigureAwait(false);
5✔
47
                await using var transaction = await context.Database.BeginTransactionAsync(cancellationToken).ConfigureAwait(false);
5✔
48
                if (job.GroupId is { } groupId && !await HasLiveGroupJobsAsync(
5✔
49
                        context,
5✔
50
                        job.QueueName,
5✔
51
                        groupId,
5✔
52
                        cancellationToken
5✔
53
                ).ConfigureAwait(false))
5✔
54
                {
55
                        var cursor = await context.Set<ImmediateFairQueueGroupEntity>()
5✔
56
                                .SingleOrDefaultAsync(
5✔
57
                                        group => group.QueueName == job.QueueName && group.GroupId == groupId,
5✔
58
                                        cancellationToken
5✔
59
                                )
5✔
60
                                .ConfigureAwait(false);
5✔
61
                        if (cursor is not null)
5✔
62
                                _ = context.Remove(cursor);
4✔
63
                }
64

65
                _ = context.Set<ImmediateJobEntity>().Add(ToEntity(job));
5✔
66
                _ = await context.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
5✔
67
                await transaction.CommitAsync(cancellationToken).ConfigureAwait(false);
5✔
68
        }
5✔
69

70
        /// <inheritdoc />
71
        public async ValueTask EnqueueContinuationAsync(
72
                JobRecord job,
73
                IReadOnlyList<JobContinuationEdge> edges,
74
                CancellationToken cancellationToken = default
75
        )
76
        {
77
                ArgumentNullException.ThrowIfNull(job);
5✔
78
                ArgumentNullException.ThrowIfNull(edges);
5✔
79
                await ExecuteGraphInsertAsync(batch: null, [job], edges, cancellationToken).ConfigureAwait(false);
5✔
80
        }
5✔
81

82
        /// <inheritdoc />
83
        public async ValueTask EnqueueBatchAsync(
84
                JobBatchRecord batch,
85
                IReadOnlyList<JobRecord> jobs,
86
                IReadOnlyList<JobContinuationEdge> edges,
87
                CancellationToken cancellationToken = default
88
        )
89
        {
90
                ArgumentNullException.ThrowIfNull(batch);
5✔
91
                ArgumentNullException.ThrowIfNull(jobs);
5✔
92
                ArgumentNullException.ThrowIfNull(edges);
5✔
93
                if (jobs.Count == 0)
5✔
94
                        throw new ImmediateJobException("An atomic batch cannot be committed without jobs.");
×
95
                await ExecuteGraphInsertAsync(batch, jobs, edges, cancellationToken).ConfigureAwait(false);
5✔
96
        }
5✔
97

98
        private ValueTask ExecuteGraphInsertAsync(
99
                JobBatchRecord? batch,
100
                IReadOnlyList<JobRecord> jobs,
101
                IReadOnlyList<JobContinuationEdge> edges,
102
                CancellationToken cancellationToken
103
        ) => RetryConcurrencyAsync(
5✔
104
                operationCancellationToken => InsertGraphCoreAsync(batch, jobs, edges, operationCancellationToken),
5✔
105
                cancellationToken
5✔
106
        );
5✔
107

108
        private async Task InsertGraphCoreAsync(
109
                JobBatchRecord? batch,
110
                IReadOnlyList<JobRecord> jobs,
111
                IReadOnlyList<JobContinuationEdge> edges,
112
                CancellationToken cancellationToken
113
        )
114
        {
115
                var jobIds = jobs.Select(static job => job.Id).ToHashSet(StringComparer.Ordinal);
5✔
116
                if (jobIds.Count != jobs.Count)
5✔
117
                        throw new ImmediateJobException("A batch or continuation insert contains duplicate job identifiers.");
×
118
                if (batch is not null && jobs.Any(job => !string.Equals(job.BatchId, batch.Id, StringComparison.Ordinal)))
5✔
119
                        throw new ImmediateJobException("Every atomic batch member must carry the committed batch identifier.");
×
120

121
                var edgeEntities = edges.Select(ToEntity).ToArray();
5✔
122
                if (edgeEntities.Any(edge => !jobIds.Contains(edge.ChildJobId)))
5✔
123
                        throw new ImmediateJobException("Every continuation edge must target a job inserted by the same operation.");
×
124
                if (edgeEntities.DistinctBy(static edge => (edge.ChildJobId, edge.ParentKind, edge.ParentId)).Count() != edgeEntities.Length)
5✔
125
                        throw new ImmediateJobException("Duplicate continuation edges are not allowed.");
×
126
                ThrowIfCyclic(jobIds, edgeEntities);
5✔
127

128
                await using var context = await contextFactory.CreateDbContextAsync(cancellationToken).ConfigureAwait(false);
5✔
129
                await using var transaction = await context.Database.BeginTransactionAsync(cancellationToken).ConfigureAwait(false);
5✔
130
                var jobEntities = jobs.Select(ToEntity).ToDictionary(static job => job.Id, StringComparer.Ordinal);
5✔
131
                await EvaluateInitialDependenciesAsync(
5✔
132
                        context,
5✔
133
                        jobEntities,
5✔
134
                        edgeEntities,
5✔
135
                        _timeProvider.GetUtcNow(),
5✔
136
                        cancellationToken
5✔
137
                ).ConfigureAwait(false);
5✔
138

139
                if (batch is not null)
5✔
140
                {
141
                        var terminal = jobEntities.Values.Where(static job => IsTerminal(job.State)).ToArray();
5✔
142
                        var pending = jobEntities.Count - terminal.Length;
5✔
143
                        var succeeded = terminal.Count(static job => job.State == JobState.Succeeded);
4✔
144
                        var failed = terminal.Count(static job => job.State == JobState.Failed);
4✔
145
                        var cancelled = terminal.Count(static job => job.State == JobState.Cancelled);
4✔
146
                        _ = context.Add(new ImmediateJobBatchEntity
5✔
147
                        {
5✔
148
                                Id = batch.Id,
5✔
149
                                CreatedAt = batch.CreatedAt,
5✔
150
                                TotalJobs = jobEntities.Count,
5✔
151
                                PendingCount = pending,
5✔
152
                                SucceededCount = succeeded,
5✔
153
                                FailedCount = failed,
5✔
154
                                CancelledCount = cancelled,
5✔
155
                                StartedAt = batch.StartedAt,
5✔
156
                                CompletedAt = pending == 0 ? batch.CompletedAt ?? _timeProvider.GetUtcNow() : null,
5✔
157
                                State = pending == 0 ? GetTerminalBatchState(failed, cancelled) : BatchState.Executing,
5✔
158
                                ConcurrencyStamp = Guid.NewGuid(),
5✔
159
                        });
5✔
160
                }
161

162
                context.AddRange(jobEntities.Values);
5✔
163
                context.AddRange(edgeEntities);
5✔
164
                _ = await context.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
5✔
165
                await transaction.CommitAsync(cancellationToken).ConfigureAwait(false);
5✔
166
        }
5✔
167

168
        /// <inheritdoc />
169
        public async ValueTask<IReadOnlyList<JobRecord>> AcquireDueJobsAsync(
170
                JobAcquisitionRequest request,
171
                CancellationToken cancellationToken = default
172
        )
173
        {
174
                ArgumentNullException.ThrowIfNull(request);
5✔
175
                ArgumentException.ThrowIfNullOrWhiteSpace(request.WorkerId);
5✔
176
                ArgumentOutOfRangeException.ThrowIfLessThanOrEqual(request.Lease, TimeSpan.Zero);
5✔
177
                ArgumentOutOfRangeException.ThrowIfLessThanOrEqual(request.BatchSize, 0);
5✔
178
                if (request.FairQueues is not null)
5✔
179
                        return await AcquireDueJobsFairAsync(request, cancellationToken).ConfigureAwait(false);
5✔
180

181
                var now = _timeProvider.GetUtcNow();
5✔
182
                var acquired = new List<JobRecord>(request.BatchSize);
5✔
183
                foreach (var queue in request.Queues)
5✔
184
                {
185
                        var queueCapacity = Math.Min(queue.Capacity, request.BatchSize - acquired.Count);
5✔
186
                        if (queueCapacity <= 0)
5✔
187
                                continue;
188

189
                        var jobCapacities = queue.JobCapacities.ToDictionary(static pair => pair.Key, static pair => pair.Value, StringComparer.Ordinal);
5✔
190
                        while (queueCapacity > 0)
5✔
191
                        {
192
                                var eligibleNames = jobCapacities.Where(static pair => pair.Value > 0).Select(static pair => pair.Key).ToArray();
5✔
193
                                if (eligibleNames.Length == 0)
5✔
194
                                        break;
195

196
                                await using var readContext = await contextFactory.CreateDbContextAsync(cancellationToken).ConfigureAwait(false);
5✔
197
                                var candidates = await readContext.Set<ImmediateJobEntity>()
5✔
198
                                        .AsNoTracking()
5✔
199
                                        .Where(job => job.QueueName == queue.QueueName && eligibleNames.Contains(job.JobName) &&
5✔
200
                                                (((job.State == JobState.Scheduled || job.State == JobState.Pending) && job.DueAt <= now)
5✔
201
                                                        || (job.State == JobState.Active && job.LeaseExpiresAt <= now)))
5✔
202
                                        .OrderBy(job => job.DueAt)
5✔
203
                                        .ThenBy(job => job.CreatedAt)
5✔
204
                                        .ThenBy(job => job.Id)
5✔
205
                                        .Take(queueCapacity)
5✔
206
                                        .ToListAsync(cancellationToken)
5✔
207
                                        .ConfigureAwait(false);
5✔
208
                                if (candidates.Count == 0)
5✔
209
                                        break;
210

211
                                var selected = new List<ImmediateJobEntity>(candidates.Count);
5✔
212
                                var selectionCapacities = new Dictionary<string, int>(jobCapacities, StringComparer.Ordinal);
5✔
213
                                foreach (var candidate in candidates)
5✔
214
                                {
215
                                        if (selectionCapacities[candidate.JobName] <= 0)
5✔
216
                                                continue;
217
                                        selectionCapacities[candidate.JobName]--;
5✔
218
                                        selected.Add(candidate);
5✔
219
                                }
220

221
                                var claimed = await AcquireCandidatesAsync(
5✔
222
                                        selected,
5✔
223
                                        request.WorkerId,
5✔
224
                                        request.Lease,
5✔
225
                                        now,
5✔
226
                                        cancellationToken
5✔
227
                                ).ConfigureAwait(false);
5✔
228
                                foreach (var job in claimed)
5✔
229
                                {
230
                                        jobCapacities[job.JobName]--;
5✔
231
                                        queueCapacity--;
5✔
232
                                        acquired.Add(job);
5✔
233
                                }
234

235
                                if (claimed.Count == 0)
5✔
236
                                        break;
237
                        }
4✔
238
                }
4✔
239

240
                return acquired;
5✔
241
        }
4✔
242

243
        private async ValueTask<IReadOnlyList<JobRecord>> AcquireDueJobsFairAsync(
244
                JobAcquisitionRequest request,
245
                CancellationToken cancellationToken
246
        )
247
        {
248
                var now = _timeProvider.GetUtcNow();
5✔
249
                var acquired = new List<JobRecord>(request.BatchSize);
5✔
250
                foreach (var queue in request.Queues)
5✔
251
                {
252
                        var consecutiveFailedClaims = 0;
5✔
253
                        var queueCapacity = Math.Min(queue.Capacity, request.BatchSize - acquired.Count);
5✔
254
                        if (queueCapacity <= 0)
5✔
255
                                continue;
256

257
                        var jobCapacities = queue.JobCapacities.ToDictionary(
5✔
258
                                static pair => pair.Key,
5✔
259
                                static pair => pair.Value,
5✔
260
                                StringComparer.Ordinal
5✔
261
                        );
5✔
262
                        while (queueCapacity > 0)
5✔
263
                        {
264
                                var eligibleNames = jobCapacities
5✔
265
                                        .Where(static pair => pair.Value > 0)
5✔
266
                                        .Select(static pair => pair.Key)
5✔
267
                                        .ToArray();
5✔
268
                                if (eligibleNames.Length == 0)
5✔
269
                                        break;
270

271
                                await using var readContext = await contextFactory.CreateDbContextAsync(cancellationToken).ConfigureAwait(false);
5✔
272
                                var eligibleQuery = readContext.Set<ImmediateJobEntity>()
5✔
273
                                        .AsNoTracking()
5✔
274
                                        .Where(job => job.QueueName == queue.QueueName && eligibleNames.Contains(job.JobName) &&
5✔
275
                                                (((job.State == JobState.Scheduled || job.State == JobState.Pending) && job.DueAt <= now)
5✔
276
                                                        || (job.State == JobState.Active && job.LeaseExpiresAt <= now)));
5✔
277
                                if (!await eligibleQuery
5✔
278
                                        .AnyAsync(static job => job.GroupId != null, cancellationToken)
5✔
279
                                        .ConfigureAwait(false))
5✔
280
                                {
281
                                        var claimed = await AcquireFairFastPathAsync(
4✔
282
                                                queue.QueueName,
4✔
283
                                                jobCapacities,
4✔
284
                                                queueCapacity,
4✔
285
                                                request.WorkerId,
4✔
286
                                                request.Lease,
4✔
287
                                                now,
4✔
288
                                                cancellationToken
4✔
289
                                        ).ConfigureAwait(false);
4✔
290
                                        queueCapacity -= claimed.Count;
4✔
291
                                        acquired.AddRange(claimed);
4✔
292
                                        break;
4✔
293
                                }
294

295
                                var groupedHeads = await eligibleQuery
5✔
296
                                        .Where(static job => job.GroupId != null)
5✔
297
                                        .GroupBy(static job => job.GroupId)
5✔
298
                                        .Select(static group => group
5✔
299
                                                .OrderBy(job => job.DueAt)
5✔
300
                                                .ThenBy(job => job.CreatedAt)
5✔
301
                                                .ThenBy(job => job.Id)
5✔
302
                                                .First())
5✔
303
                                        .ToListAsync(cancellationToken)
5✔
304
                                        .ConfigureAwait(false);
5✔
305
                                var ungroupedHead = await eligibleQuery
5✔
306
                                        .Where(static job => job.GroupId == null)
5✔
307
                                        .OrderBy(job => job.DueAt)
5✔
308
                                        .ThenBy(job => job.CreatedAt)
5✔
309
                                        .ThenBy(job => job.Id)
5✔
310
                                        .FirstOrDefaultAsync(cancellationToken)
5✔
311
                                        .ConfigureAwait(false);
5✔
312
                                if (groupedHeads.Count == 0)
5✔
313
                                {
314
                                        var claimed = await AcquireFairFastPathAsync(
×
315
                                                queue.QueueName,
×
316
                                                jobCapacities,
×
317
                                                queueCapacity,
×
318
                                                request.WorkerId,
×
319
                                                request.Lease,
×
320
                                                now,
×
321
                                                cancellationToken
×
322
                                        ).ConfigureAwait(false);
×
323
                                        queueCapacity -= claimed.Count;
×
324
                                        acquired.AddRange(claimed);
×
325
                                        break;
×
326
                                }
327

328
                                var activeQuery = readContext.Set<ImmediateJobEntity>()
5✔
329
                                        .AsNoTracking()
5✔
330
                                        .Where(job => job.QueueName == queue.QueueName
5✔
331
                                                && job.State == JobState.Active
5✔
332
                                                && job.LeaseExpiresAt > now);
5✔
333
                                var totalInflight = await activeQuery
5✔
334
                                        .CountAsync(cancellationToken)
5✔
335
                                        .ConfigureAwait(false);
5✔
336
                                var groupedHeadIds = groupedHeads.Select(static job => job.Id).ToArray();
5✔
337
                                var cursorQuery = readContext.Set<ImmediateFairQueueGroupEntity>()
5✔
338
                                        .AsNoTracking()
5✔
339
                                        .Where(group => group.QueueName == queue.QueueName);
5✔
340
                                var groupStateQuery = eligibleQuery
5✔
341
                                        .Where(job => groupedHeadIds.Contains(job.Id));
5✔
342
                                var groupStates = request.FairQueues!.GroupRoundRobin
5✔
343
                                        ? await groupStateQuery
5✔
344
                                                .Select(job => new FairQueueCandidateState(
5✔
345
                                                        job.Id,
5✔
346
                                                        activeQuery.Count(active => active.GroupId == job.GroupId),
5✔
347
                                                        cursorQuery
5✔
348
                                                                .Where(cursor => cursor.GroupId == job.GroupId)
5✔
349
                                                                .Select(static cursor => cursor.LastServedSequence)
5✔
350
                                                                .FirstOrDefault()
5✔
351
                                                ))
5✔
352
                                                .ToDictionaryAsync(static state => state.JobId, StringComparer.Ordinal, cancellationToken)
5✔
353
                                                .ConfigureAwait(false)
5✔
354
                                        : await groupStateQuery
5✔
355
                                                .Select(job => new FairQueueCandidateState(
5✔
356
                                                        job.Id,
5✔
357
                                                        activeQuery.Count(active => active.GroupId == job.GroupId),
5✔
358
                                                        0
5✔
359
                                                ))
5✔
360
                                                .ToDictionaryAsync(static state => state.JobId, StringComparer.Ordinal, cancellationToken)
4✔
361
                                                .ConfigureAwait(false);
5✔
362
                                var nextSequence = 0L;
5✔
363
                                if (request.FairQueues.GroupRoundRobin)
5✔
364
                                {
365
                                        var maxSequence = await cursorQuery
5✔
366
                                                .MaxAsync(static group => (long?)group.LastServedSequence, cancellationToken)
5✔
367
                                                .ConfigureAwait(false);
5✔
368
                                        nextSequence = (maxSequence ?? 0) + 1;
5✔
369
                                }
370

371
                                var candidates = ungroupedHead is null ? groupedHeads : [.. groupedHeads, ungroupedHead];
5✔
372
                                var ranked = candidates.Select(job =>
5✔
373
                                {
5✔
374
                                        FairQueueCandidateState? state = null;
5✔
375
                                        if (job.GroupId is not null)
5✔
376
                                                _ = groupStates.TryGetValue(job.Id, out state);
5✔
377
                                        var noisy = IsNoisy(job.GroupId, state?.Inflight ?? 0, totalInflight, request.FairQueues);
5✔
378
                                        return new
5✔
379
                                        {
5✔
380
                                                Job = job,
5✔
381
                                                Noisy = noisy,
5✔
382
                                                NoisyInflight = noisy ? state!.Inflight : 0,
5✔
383
                                                LastServedSequence = state?.LastServedSequence ?? 0,
5✔
384
                                        };
5✔
385
                                });
5✔
386
                                var selected = ranked
5✔
387
                                        .OrderBy(static candidate => candidate.Noisy)
5✔
388
                                        .ThenBy(static candidate => candidate.NoisyInflight)
5✔
389
                                        .ThenBy(candidate => request.FairQueues.GroupRoundRobin
5✔
390
                                                ? candidate.LastServedSequence
5✔
391
                                                : 0)
5✔
392
                                        .ThenBy(static candidate => candidate.Job.DueAt)
5✔
393
                                        .ThenBy(static candidate => candidate.Job.CreatedAt)
5✔
394
                                        .ThenBy(static candidate => candidate.Job.Id, StringComparer.Ordinal)
5✔
395
                                        .First()
5✔
396
                                        .Job;
5✔
397
                                var claimedJob = request.FairQueues.GroupRoundRobin
5✔
398
                                        ? await AcquireFairCandidateAsync(
5✔
399
                                                selected,
5✔
400
                                                request.WorkerId,
5✔
401
                                                request.Lease,
5✔
402
                                                now,
5✔
403
                                                nextSequence,
5✔
404
                                                cancellationToken
5✔
405
                                        ).ConfigureAwait(false)
5✔
406
                                        : GetFirstOrDefault(await AcquireCandidatesAsync(
5✔
407
                                                        [selected],
5✔
408
                                                        request.WorkerId,
5✔
409
                                                        request.Lease,
5✔
410
                                                        now,
5✔
411
                                                        cancellationToken
5✔
412
                                                ).ConfigureAwait(false));
5✔
413
                                if (claimedJob is null)
5✔
414
                                {
415
                                        if (++consecutiveFailedClaims >= MaxConsecutiveFailedFairClaims)
4✔
416
                                                break;
4✔
417
                                        continue;
418
                                }
419

420
                                consecutiveFailedClaims = 0;
5✔
421
                                jobCapacities[claimedJob.JobName]--;
5✔
422
                                queueCapacity--;
5✔
423
                                acquired.Add(claimedJob);
5✔
424
                        }
4✔
425
                }
4✔
426

427
                return acquired;
5✔
428
        }
4✔
429

430
        private async ValueTask<IReadOnlyList<JobRecord>> AcquireFairFastPathAsync(
431
                string queueName,
432
                Dictionary<string, int> jobCapacities,
433
                int queueCapacity,
434
                string workerId,
435
                TimeSpan lease,
436
                DateTimeOffset now,
437
                CancellationToken cancellationToken
438
        )
439
        {
440
                var acquired = new List<JobRecord>(queueCapacity);
4✔
441
                while (queueCapacity > 0)
4✔
442
                {
443
                        var eligibleNames = jobCapacities
4✔
444
                                .Where(static pair => pair.Value > 0)
4✔
445
                                .Select(static pair => pair.Key)
4✔
446
                                .ToArray();
4✔
447
                        if (eligibleNames.Length == 0)
4✔
448
                                break;
449

450
                        await using var readContext = await contextFactory.CreateDbContextAsync(cancellationToken).ConfigureAwait(false);
4✔
451
                        var candidates = await readContext.Set<ImmediateJobEntity>()
4✔
452
                                .AsNoTracking()
4✔
453
                                .Where(job => job.QueueName == queueName && eligibleNames.Contains(job.JobName) &&
4✔
454
                                        (((job.State == JobState.Scheduled || job.State == JobState.Pending) && job.DueAt <= now)
4✔
455
                                                || (job.State == JobState.Active && job.LeaseExpiresAt <= now)))
4✔
456
                                .OrderBy(job => job.DueAt)
4✔
457
                                .ThenBy(job => job.CreatedAt)
4✔
458
                                .ThenBy(job => job.Id)
4✔
459
                                .Take(queueCapacity)
4✔
460
                                .ToListAsync(cancellationToken)
4✔
461
                                .ConfigureAwait(false);
4✔
462
                        if (candidates.Count == 0)
4✔
463
                                break;
464

465
                        var selected = new List<ImmediateJobEntity>(candidates.Count);
4✔
466
                        var selectionCapacities = new Dictionary<string, int>(jobCapacities, StringComparer.Ordinal);
4✔
467
                        foreach (var candidate in candidates)
4✔
468
                        {
469
                                if (selectionCapacities[candidate.JobName] <= 0)
4✔
470
                                        continue;
471
                                selectionCapacities[candidate.JobName]--;
4✔
472
                                selected.Add(candidate);
4✔
473
                        }
474

475
                        var claimed = await AcquireCandidatesAsync(
4✔
476
                                selected,
4✔
477
                                workerId,
4✔
478
                                lease,
4✔
479
                                now,
4✔
480
                                cancellationToken
4✔
481
                        ).ConfigureAwait(false);
4✔
482
                        foreach (var job in claimed)
4✔
483
                        {
484
                                jobCapacities[job.JobName]--;
4✔
485
                                queueCapacity--;
4✔
486
                                acquired.Add(job);
4✔
487
                        }
488

489
                        if (claimed.Count == 0)
4✔
490
                                break;
491
                }
3✔
492

493
                return acquired;
4✔
494
        }
3✔
495

496
        private async ValueTask<JobRecord?> AcquireFairCandidateAsync(
497
                ImmediateJobEntity candidate,
498
                string workerId,
499
                TimeSpan lease,
500
                DateTimeOffset now,
501
                long nextSequence,
502
                CancellationToken cancellationToken
503
        )
504
        {
505
                await using var strategyContext = await contextFactory.CreateDbContextAsync(cancellationToken).ConfigureAwait(false);
5✔
506
                var strategy = strategyContext.Database.CreateExecutionStrategy();
5✔
507
                return await strategy.ExecuteAsync(
5✔
508
                        operationCancellationToken => AcquireFairCandidateCoreAsync(
5✔
509
                                candidate,
5✔
510
                                workerId,
5✔
511
                                lease,
5✔
512
                                now,
5✔
513
                                nextSequence,
5✔
514
                                operationCancellationToken
5✔
515
                        ),
5✔
516
                        cancellationToken
5✔
517
                ).ConfigureAwait(false);
5✔
518
        }
4✔
519

520
        private async Task<JobRecord?> AcquireFairCandidateCoreAsync(
521
                ImmediateJobEntity candidate,
522
                string workerId,
523
                TimeSpan lease,
524
                DateTimeOffset now,
525
                long nextSequence,
526
                CancellationToken cancellationToken
527
        )
528
        {
529
                await using var context = await contextFactory.CreateDbContextAsync(cancellationToken).ConfigureAwait(false);
5✔
530
                await using var transaction = await context.Database.BeginTransactionAsync(cancellationToken).ConfigureAwait(false);
5✔
531
                var entity = Copy(candidate);
5✔
532
                _ = context.Attach(entity);
5✔
533
                entity.State = JobState.Active;
5✔
534
                entity.WorkerId = workerId;
5✔
535
                entity.LeaseExpiresAt = now + lease;
5✔
536
                entity.Attempt++;
5✔
537
                entity.CompletedAt = null;
5✔
538
                entity.ExecutionTraceId = null;
5✔
539
                entity.ExecutionSpanId = null;
5✔
540
                entity.ExecutionStartedAt = null;
5✔
541
                entity.ConcurrencyStamp = Guid.NewGuid();
5✔
542

543
                if (candidate.GroupId is { } groupId)
5✔
544
                {
545
                        var group = await context.Set<ImmediateFairQueueGroupEntity>()
5✔
546
                                .SingleOrDefaultAsync(
5✔
547
                                        item => item.QueueName == candidate.QueueName && item.GroupId == groupId,
5✔
548
                                        cancellationToken
5✔
549
                                )
5✔
550
                                .ConfigureAwait(false);
5✔
551
                        if (group is null)
5✔
552
                        {
553
                                _ = context.Add(new ImmediateFairQueueGroupEntity
5✔
554
                                {
5✔
555
                                        QueueName = candidate.QueueName,
5✔
556
                                        GroupId = groupId,
5✔
557
                                        LastServedSequence = nextSequence,
5✔
558
                                        ConcurrencyStamp = Guid.NewGuid(),
5✔
559
                                });
5✔
560
                        }
561
                        else if (group.LastServedSequence >= nextSequence)
4✔
562
                        {
563
                                // Selection observed an older cursor snapshot. Re-rank instead of moving this group backward.
564
                                return null;
4✔
565
                        }
566
                        else
567
                        {
568
                                group.LastServedSequence = nextSequence;
4✔
569
                                group.ConcurrencyStamp = Guid.NewGuid();
4✔
570
                        }
571
                }
572

573
                if (entity.BatchId is { } batchId)
5✔
574
                {
575
                        var batch = await context.Set<ImmediateJobBatchEntity>()
×
576
                                .SingleOrDefaultAsync(item => item.Id == batchId, cancellationToken)
×
577
                                .ConfigureAwait(false);
×
578
                        if (batch is not null && batch.StartedAt is null)
×
579
                        {
580
                                batch.StartedAt = now;
×
581
                                batch.ConcurrencyStamp = Guid.NewGuid();
×
582
                        }
583
                }
584

585
                try
586
                {
587
                        _ = await context.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
5✔
588
                        await transaction.CommitAsync(cancellationToken).ConfigureAwait(false);
5✔
589
                        return ToRecord(entity);
5✔
590
                }
591
                catch (DbUpdateException)
4✔
592
                {
593
                        // The job or its group cursor changed after candidate selection.
594
                        return null;
5✔
595
                }
596
        }
4✔
597

598
        private static JobRecord? GetFirstOrDefault(IReadOnlyList<JobRecord> jobs) =>
599
                jobs.Count == 0 ? null : jobs[0];
4✔
600

601
        private static bool IsNoisy(
602
                string? groupId,
603
                int inflight,
604
                int totalInflight,
605
                FairQueuePolicy policy
606
        )
607
        {
608
                return groupId is not null
5✔
609
                        && totalInflight > 0
5✔
610
                        && inflight >= policy.MinInflightForNoisy
5✔
611
                        && (double)inflight / totalInflight > policy.ConcurrencyShareThreshold;
5✔
612
        }
613

614
        private sealed record FairQueueCandidateState(
5✔
615
                string JobId,
5✔
616
                int Inflight,
5✔
617
                long LastServedSequence
5✔
618
        );
5✔
619

620
        /// <inheritdoc />
621
        public async ValueTask<IReadOnlyList<JobRecord>> AcquireJobsAsync(
622
                IReadOnlyCollection<string> jobIds,
623
                string workerId,
624
                TimeSpan lease,
625
                CancellationToken cancellationToken = default
626
        )
627
        {
628
                ArgumentNullException.ThrowIfNull(jobIds);
5✔
629
                ArgumentException.ThrowIfNullOrWhiteSpace(workerId);
5✔
630
                ArgumentOutOfRangeException.ThrowIfLessThanOrEqual(lease, TimeSpan.Zero);
5✔
631
                if (jobIds.Count == 0)
5✔
632
                        return [];
×
633

634
                var now = _timeProvider.GetUtcNow();
5✔
635
                var ids = jobIds.ToArray();
5✔
636
                await using var readContext = await contextFactory.CreateDbContextAsync(cancellationToken).ConfigureAwait(false);
5✔
637
                var candidates = await readContext.Set<ImmediateJobEntity>()
5✔
638
                        .AsNoTracking()
5✔
639
                        .Where(job => ids.Contains(job.Id) &&
5✔
640
                                (((job.State == JobState.Scheduled || job.State == JobState.Pending) && job.DueAt <= now)
5✔
641
                                || (job.State == JobState.Active && job.LeaseExpiresAt <= now)))
5✔
642
                        .ToListAsync(cancellationToken)
5✔
643
                        .ConfigureAwait(false);
5✔
644

645
                return await AcquireCandidatesAsync(candidates, workerId, lease, now, cancellationToken).ConfigureAwait(false);
5✔
646
        }
4✔
647

648
        private async ValueTask<IReadOnlyList<JobRecord>> AcquireCandidatesAsync(
649
                List<ImmediateJobEntity> candidates,
650
                string workerId,
651
                TimeSpan lease,
652
                DateTimeOffset now,
653
                CancellationToken cancellationToken
654
        )
655
        {
656
                var acquired = new List<JobRecord>(candidates.Count);
5✔
657
                foreach (var candidate in candidates)
5✔
658
                {
659
                        await using var context = await contextFactory.CreateDbContextAsync(cancellationToken).ConfigureAwait(false);
5✔
660
                        var entity = Copy(candidate);
5✔
661
                        _ = context.Attach(entity);
5✔
662
                        entity.State = JobState.Active;
5✔
663
                        entity.WorkerId = workerId;
5✔
664
                        entity.LeaseExpiresAt = now + lease;
5✔
665
                        entity.Attempt++;
5✔
666
                        entity.CompletedAt = null;
5✔
667
                        entity.ExecutionTraceId = null;
5✔
668
                        entity.ExecutionSpanId = null;
5✔
669
                        entity.ExecutionStartedAt = null;
5✔
670
                        entity.ConcurrencyStamp = Guid.NewGuid();
5✔
671
                        if (entity.BatchId is { } batchId)
5✔
672
                        {
673
                                var batch = await context.Set<ImmediateJobBatchEntity>()
5✔
674
                                        .SingleOrDefaultAsync(item => item.Id == batchId, cancellationToken)
5✔
675
                                        .ConfigureAwait(false);
5✔
676
                                if (batch is not null && batch.StartedAt is null)
5✔
677
                                {
678
                                        batch.StartedAt = now;
5✔
679
                                        batch.ConcurrencyStamp = Guid.NewGuid();
5✔
680
                                }
681
                        }
682

683
                        try
684
                        {
685
                                _ = await context.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
5✔
686
                                acquired.Add(ToRecord(entity));
5✔
687
                        }
5✔
688
                        catch (DbUpdateConcurrencyException)
1✔
689
                        {
690
                                // Another scheduler claimed or changed this candidate first.
691
                        }
1✔
692
                }
4✔
693

694
                return acquired;
5✔
695
        }
4✔
696

697
        /// <inheritdoc />
698
        public ValueTask SetExecutionTelemetryAsync(
699
                string jobId,
700
                string workerId,
701
                string? traceId,
702
                string? spanId,
703
                DateTimeOffset startedAt,
704
                CancellationToken cancellationToken = default
705
        ) => MutateOwnedAsync(jobId, workerId, job =>
1✔
706
        {
1✔
707
                job.ExecutionTraceId = traceId;
1✔
708
                job.ExecutionSpanId = spanId;
1✔
709
                job.ExecutionStartedAt = startedAt;
1✔
710
        }, cancellationToken);
1✔
711

712
        /// <inheritdoc />
713
        public ValueTask RenewLeaseAsync(string jobId, string workerId, TimeSpan lease, CancellationToken cancellationToken = default)
714
        {
715
                ArgumentOutOfRangeException.ThrowIfLessThanOrEqual(lease, TimeSpan.Zero);
×
716
                return MutateOwnedAsync(jobId, workerId, job => job.LeaseExpiresAt = _timeProvider.GetUtcNow() + lease, cancellationToken);
×
717
        }
718

719
        /// <inheritdoc />
720
        public ValueTask CompleteAsync(string jobId, string workerId, CancellationToken cancellationToken = default) =>
721
                CompleteWithContinuationsAsync(jobId, workerId, [], cancellationToken);
5✔
722

723
        /// <inheritdoc />
724
        public ValueTask CompleteWithContinuationsAsync(
725
                string jobId,
726
                string workerId,
727
                IReadOnlyList<JobContinuationAddition> additions,
728
                CancellationToken cancellationToken = default
729
        )
730
        {
731
                ArgumentNullException.ThrowIfNull(additions);
5✔
732
                return MutateOwnedWithDependenciesAsync(
5✔
733
                        jobId,
5✔
734
                        workerId,
5✔
735
                        error: null,
5✔
736
                        nextRetryAt: null,
5✔
737
                        succeeded: true,
5✔
738
                        additions,
5✔
739
                        cancellationToken
5✔
740
                );
5✔
741
        }
742

743
        /// <inheritdoc />
744
        public ValueTask AddBatchJobAsync(
745
                string currentJobId,
746
                JobRecord job,
747
                ContinuationOptions options,
748
                CancellationToken cancellationToken = default
749
        )
750
        {
751
                ArgumentNullException.ThrowIfNull(job);
5✔
752
                if (options == ContinuationOptions.Detached)
5✔
753
                        throw new ImmediateJobException("AddToBatchAsync cannot create a detached job.");
×
754
                return RetryConcurrencyAsync(
5✔
755
                        operationCancellationToken => AddBatchJobCoreAsync(
5✔
756
                                currentJobId,
5✔
757
                                job,
5✔
758
                                options,
5✔
759
                                operationCancellationToken
5✔
760
                        ),
5✔
761
                        cancellationToken
5✔
762
                );
5✔
763
        }
764

765
        /// <inheritdoc />
766
        public ValueTask FailAsync(
767
                string jobId,
768
                string workerId,
769
                string error,
770
                DateTimeOffset? nextRetryAt,
771
                CancellationToken cancellationToken = default
772
        ) => MutateOwnedWithDependenciesAsync(jobId, workerId, error, nextRetryAt, succeeded: false, [], cancellationToken);
5✔
773

774
        /// <inheritdoc />
775
        public async ValueTask UpsertRecurringAsync(RecurringJobSchedule schedule, CancellationToken cancellationToken = default)
776
        {
777
                ArgumentNullException.ThrowIfNull(schedule);
5✔
778
                await using var context = await contextFactory.CreateDbContextAsync(cancellationToken).ConfigureAwait(false);
5✔
779
                if (await UpdateRecurringAsync(context, schedule, cancellationToken).ConfigureAwait(false) != 0)
5✔
780
                        return;
781
                await ThrowIfReplacingCodeDefinedScheduleAsync(context, schedule, cancellationToken).ConfigureAwait(false);
5✔
782

783
                _ = context.Add(ToEntity(schedule));
5✔
784
                try
785
                {
786
                        _ = await context.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
5✔
787
                }
5✔
788
                catch (DbUpdateException)
789
                {
790
                        // A competing node inserted the same schedule after our update attempt.
791
                        await using var retryContext = await contextFactory.CreateDbContextAsync(cancellationToken).ConfigureAwait(false);
×
792
                        if (await UpdateRecurringAsync(retryContext, schedule, cancellationToken).ConfigureAwait(false) != 0)
×
793
                                return;
794
                        await ThrowIfReplacingCodeDefinedScheduleAsync(retryContext, schedule, cancellationToken).ConfigureAwait(false);
×
795
                        throw;
×
796
                }
797
        }
4✔
798

799
        /// <inheritdoc />
800
        public async ValueTask RemoveObsoleteCodeDefinedRecurringAsync(
801
                IReadOnlyCollection<string> activeScheduleNames,
802
                CancellationToken cancellationToken = default
803
        )
804
        {
805
                ArgumentNullException.ThrowIfNull(activeScheduleNames);
4✔
806
                await using var context = await contextFactory.CreateDbContextAsync(cancellationToken).ConfigureAwait(false);
4✔
807
                var schedules = context.Set<ImmediateRecurringJobEntity>()
4✔
808
                        .Where(schedule => schedule.IsCodeDefined);
4✔
809
                if (activeScheduleNames.Count != 0)
4✔
810
                        schedules = schedules.Where(schedule => !activeScheduleNames.Contains(schedule.Name));
4✔
811
                _ = await schedules.ExecuteDeleteAsync(cancellationToken).ConfigureAwait(false);
4✔
812
        }
4✔
813

814
        /// <inheritdoc />
815
        public async ValueTask RemoveRecurringAsync(string name, CancellationToken cancellationToken = default)
816
        {
817
                ArgumentException.ThrowIfNullOrWhiteSpace(name);
5✔
818
                await using var context = await contextFactory.CreateDbContextAsync(cancellationToken).ConfigureAwait(false);
5✔
819
                var removed = await context.Set<ImmediateRecurringJobEntity>()
5✔
820
                        .Where(schedule => schedule.Name == name && !schedule.IsCodeDefined)
5✔
821
                        .ExecuteDeleteAsync(cancellationToken)
5✔
822
                        .ConfigureAwait(false);
5✔
823
                if (removed != 0)
5✔
824
                        return;
825
                if (await context.Set<ImmediateRecurringJobEntity>()
5✔
826
                        .AnyAsync(schedule => schedule.Name == name, cancellationToken)
5✔
827
                        .ConfigureAwait(false))
5✔
828
                {
829
                        throw new ImmediateJobException("Code-defined recurring schedules cannot be deleted.");
4✔
830
                }
831

832
                throw new KeyNotFoundException($"Recurring schedule '{name}' was not found.");
5✔
833
        }
3✔
834

835
        /// <inheritdoc />
836
        public ValueTask PauseRecurringAsync(string name, CancellationToken cancellationToken = default)
837
                => MutateRecurringAsync(name, schedule => schedule.IsPaused = true, cancellationToken);
×
838

839
        /// <inheritdoc />
840
        public ValueTask ResumeRecurringAsync(string name, CancellationToken cancellationToken = default)
841
                => MutateRecurringAsync(name, schedule => schedule.IsPaused = false, cancellationToken);
×
842

843
        /// <inheritdoc />
844
        public async ValueTask<IReadOnlyList<RecurringJobSchedule>> GetDueRecurringAsync(DateTimeOffset now, int batchSize, CancellationToken cancellationToken = default)
845
        {
846
                ArgumentOutOfRangeException.ThrowIfLessThanOrEqual(batchSize, 0);
×
847
                await using var context = await contextFactory.CreateDbContextAsync(cancellationToken).ConfigureAwait(false);
×
848
                return await context.Set<ImmediateRecurringJobEntity>()
×
849
                        .AsNoTracking()
×
850
                        .Where(schedule => !schedule.IsPaused && schedule.NextRunAt <= now)
×
851
                        .OrderBy(schedule => schedule.NextRunAt)
×
852
                        .Take(batchSize)
×
853
                        .Select(schedule => new RecurringJobSchedule
×
854
                        {
×
855
                                Name = schedule.Name,
×
856
                                JobName = schedule.JobName,
×
857
                                Cron = schedule.Cron,
×
858
                                TimeZone = schedule.TimeZone,
×
859
                                IsCodeDefined = schedule.IsCodeDefined,
×
860
                                IsPaused = schedule.IsPaused,
×
861
                                NextRunAt = schedule.NextRunAt,
×
862
                                LastRunAt = schedule.LastRunAt,
×
863
                        })
×
864
                        .ToListAsync(cancellationToken)
×
865
                        .ConfigureAwait(false);
×
866
        }
867

868
        /// <inheritdoc />
869
        public async ValueTask<bool> MaterializeRecurringAsync(
870
                RecurringJobSchedule schedule,
871
                JobRecord job,
872
                DateTimeOffset nextRunAt,
873
                CancellationToken cancellationToken = default
874
        )
875
        {
876
                ArgumentNullException.ThrowIfNull(schedule);
5✔
877
                ArgumentNullException.ThrowIfNull(job);
5✔
878
                await using var strategyContext = await contextFactory.CreateDbContextAsync(cancellationToken).ConfigureAwait(false);
5✔
879
                var strategy = strategyContext.Database.CreateExecutionStrategy();
5✔
880
                return await strategy.ExecuteAsync(
5✔
881
                        operationCancellationToken => MaterializeRecurringCoreAsync(
5✔
882
                                schedule,
5✔
883
                                job,
5✔
884
                                nextRunAt,
5✔
885
                                operationCancellationToken
5✔
886
                        ),
5✔
887
                        cancellationToken
5✔
888
                ).ConfigureAwait(false);
5✔
889
        }
4✔
890

891
        private async Task<bool> MaterializeRecurringCoreAsync(
892
                RecurringJobSchedule schedule,
893
                JobRecord job,
894
                DateTimeOffset nextRunAt,
895
                CancellationToken cancellationToken
896
        )
897
        {
898
                await using var context = await contextFactory.CreateDbContextAsync(cancellationToken).ConfigureAwait(false);
5✔
899
                await using var transaction = await context.Database.BeginTransactionAsync(cancellationToken).ConfigureAwait(false);
5✔
900
                var entity = await context.Set<ImmediateRecurringJobEntity>()
5✔
901
                        .SingleOrDefaultAsync(item => item.Name == schedule.Name, cancellationToken)
5✔
902
                        .ConfigureAwait(false);
5✔
903
                if (entity is null || entity.IsPaused || entity.NextRunAt != schedule.NextRunAt)
5✔
904
                        return false;
1✔
905

906
                entity.LastRunAt = schedule.NextRunAt;
5✔
907
                entity.NextRunAt = nextRunAt;
5✔
908
                entity.ConcurrencyStamp = Guid.NewGuid();
5✔
909
                _ = context.Add(ToEntity(job));
5✔
910
                try
911
                {
912
                        _ = await context.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
5✔
913
                        await transaction.CommitAsync(cancellationToken).ConfigureAwait(false);
5✔
914
                        return true;
5✔
915
                }
916
                catch (DbUpdateException)
917
                {
918
                        await transaction.RollbackAsync(cancellationToken).ConfigureAwait(false);
×
919
                        return false;
×
920
                }
921
        }
4✔
922

923
        /// <inheritdoc />
924
        public async ValueTask<JobMonitoringSnapshot> GetMonitoringSnapshotAsync(CancellationToken cancellationToken = default)
925
        {
926
                await using var context = await contextFactory.CreateDbContextAsync(cancellationToken).ConfigureAwait(false);
5✔
927
                var rawCounts = await context.Set<ImmediateJobEntity>()
5✔
928
                        .AsNoTracking()
5✔
929
                        .GroupBy(job => job.State)
5✔
930
                        .Select(group => new { State = group.Key, Count = group.LongCount() })
5✔
931
                        .ToListAsync(cancellationToken)
5✔
932
                        .ConfigureAwait(false);
5✔
933
                var counts = Enum.GetValues<JobState>().ToDictionary(static state => state, static _ => 0L);
5✔
934
                foreach (var item in rawCounts)
5✔
935
                        counts[item.State] = item.Count;
5✔
936

937
                var recurring = await context.Set<ImmediateRecurringJobEntity>()
5✔
938
                        .AsNoTracking()
5✔
939
                        .OrderBy(schedule => schedule.Name)
5✔
940
                        .Select(schedule => new RecurringJobSchedule
5✔
941
                        {
5✔
942
                                Name = schedule.Name,
5✔
943
                                JobName = schedule.JobName,
5✔
944
                                Cron = schedule.Cron,
5✔
945
                                TimeZone = schedule.TimeZone,
5✔
946
                                IsCodeDefined = schedule.IsCodeDefined,
5✔
947
                                IsPaused = schedule.IsPaused,
5✔
948
                                NextRunAt = schedule.NextRunAt,
5✔
949
                                LastRunAt = schedule.LastRunAt,
5✔
950
                        })
5✔
951
                        .ToListAsync(cancellationToken)
5✔
952
                        .ConfigureAwait(false);
5✔
953
                var cutoff = _timeProvider.GetUtcNow() - TimeSpan.FromMinutes(2);
5✔
954
                var servers = await context.Set<ImmediateJobServerEntity>()
5✔
955
                        .AsNoTracking()
5✔
956
                        .Where(server => server.LastHeartbeat >= cutoff)
5✔
957
                        .OrderBy(server => server.WorkerId)
5✔
958
                        .Select(server => new JobServerSnapshot(server.WorkerId, server.LastHeartbeat, server.ActiveWorkers, server.MaxWorkers))
5✔
959
                        .ToListAsync(cancellationToken)
5✔
960
                        .ConfigureAwait(false);
5✔
961
                return new(_timeProvider.GetUtcNow(), counts, recurring, servers)
5✔
962
                {
5✔
963
                        Capabilities = this.GetCapabilities(),
5✔
964
                };
5✔
965
        }
4✔
966

967
        /// <inheritdoc />
968
        public async ValueTask<IReadOnlyList<JobRecord>> QueryJobsAsync(JobQuery query, CancellationToken cancellationToken = default)
969
        {
970
                ArgumentNullException.ThrowIfNull(query);
5✔
971
                ArgumentOutOfRangeException.ThrowIfNegative(query.Skip);
5✔
972
                ArgumentOutOfRangeException.ThrowIfLessThanOrEqual(query.Take, 0);
5✔
973
                await using var context = await contextFactory.CreateDbContextAsync(cancellationToken).ConfigureAwait(false);
5✔
974
                var jobs = context.Set<ImmediateJobEntity>().AsNoTracking();
5✔
975
                if (query.Id is { } id)
5✔
976
                        jobs = jobs.Where(job => job.Id == id);
5✔
977
                if (query.State is { } state)
5✔
978
                        jobs = jobs.Where(job => job.State == state);
4✔
979
                if (!string.IsNullOrWhiteSpace(query.QueueName))
5✔
980
                        jobs = jobs.Where(job => job.QueueName == query.QueueName);
×
981
                if (!string.IsNullOrWhiteSpace(query.JobName))
5✔
982
                        jobs = jobs.Where(job => job.JobName == query.JobName);
×
983
                if (!string.IsNullOrWhiteSpace(query.Search))
5✔
984
                {
985
                        var search = query.Search.ToUpperInvariant();
×
986
                        // The parameterless form is intentionally used because relational providers translate it to SQL.
987
#pragma warning disable CA1304, CA1311, CA1862, MA0011
988
                        jobs = jobs.Where(job => job.JobName.ToUpper().Contains(search));
×
989
#pragma warning restore CA1304, CA1311, CA1862, MA0011
990
                }
991

992
                var entities = await jobs.OrderByDescending(job => job.CreatedAt)
5✔
993
                        .ThenBy(job => job.Id)
5✔
994
                        .Skip(query.Skip)
5✔
995
                        .Take(query.Take)
5✔
996
                        .ToListAsync(cancellationToken)
5✔
997
                        .ConfigureAwait(false);
5✔
998
                return [.. entities.Select(ToRecord)];
5✔
999
        }
4✔
1000

1001
        /// <inheritdoc />
1002
        public async ValueTask<BatchStatus?> GetBatchStatusAsync(
1003
                string batchId,
1004
                CancellationToken cancellationToken = default
1005
        )
1006
        {
1007
                ArgumentException.ThrowIfNullOrWhiteSpace(batchId);
5✔
1008
                await using var context = await contextFactory.CreateDbContextAsync(cancellationToken).ConfigureAwait(false);
5✔
1009
                var batch = await context.Set<ImmediateJobBatchEntity>()
5✔
1010
                        .AsNoTracking()
5✔
1011
                        .SingleOrDefaultAsync(item => item.Id == batchId, cancellationToken)
5✔
1012
                        .ConfigureAwait(false);
5✔
1013
                return batch is null ? null : ToStatus(batch);
5✔
1014
        }
4✔
1015

1016
        /// <inheritdoc />
1017
        public async ValueTask<IReadOnlyList<JobContinuationEdge>> GetIncomingEdgesAsync(
1018
                IReadOnlyCollection<string> childJobIds,
1019
                CancellationToken cancellationToken = default
1020
        )
1021
        {
1022
                ArgumentNullException.ThrowIfNull(childJobIds);
5✔
1023
                foreach (var childJobId in childJobIds)
5✔
1024
                        ArgumentException.ThrowIfNullOrWhiteSpace(childJobId);
5✔
1025
                if (childJobIds.Count == 0)
5✔
1026
                        return [];
5✔
1027

1028
                var ids = childJobIds.Distinct(StringComparer.Ordinal).ToArray();
5✔
1029
                await using var context = await contextFactory.CreateDbContextAsync(cancellationToken).ConfigureAwait(false);
5✔
1030
                var edges = await context.Set<ImmediateJobContinuationEntity>()
5✔
1031
                        .AsNoTracking()
5✔
1032
                        .Where(edge => ids.Contains(edge.ChildJobId))
5✔
1033
                        .OrderBy(edge => edge.ChildJobId)
5✔
1034
                        .ThenBy(edge => edge.ParentKind)
5✔
1035
                        .ThenBy(edge => edge.ParentId)
5✔
1036
                        .ToListAsync(cancellationToken)
5✔
1037
                        .ConfigureAwait(false);
5✔
1038
                return [.. edges.Select(ToContinuationEdge)];
5✔
1039
        }
4✔
1040

1041
        /// <inheritdoc />
1042
        public async ValueTask<IReadOnlyList<BatchStatus>> QueryBatchesAsync(
1043
                JobBatchQuery query,
1044
                CancellationToken cancellationToken = default
1045
        )
1046
        {
1047
                ArgumentNullException.ThrowIfNull(query);
×
1048
                ArgumentOutOfRangeException.ThrowIfNegative(query.Skip);
×
1049
                ArgumentOutOfRangeException.ThrowIfLessThanOrEqual(query.Take, 0);
×
1050
                await using var context = await contextFactory.CreateDbContextAsync(cancellationToken).ConfigureAwait(false);
×
1051
                var batches = context.Set<ImmediateJobBatchEntity>().AsNoTracking();
×
1052
                if (query.State is { } state)
×
1053
                        batches = batches.Where(batch => batch.State == state);
×
1054
                var entities = await batches.OrderByDescending(batch => batch.CreatedAt)
×
1055
                        .ThenBy(batch => batch.Id)
×
1056
                        .Skip(query.Skip)
×
1057
                        .Take(query.Take)
×
1058
                        .ToListAsync(cancellationToken)
×
1059
                        .ConfigureAwait(false);
×
1060
                return [.. entities.Select(ToStatus)];
×
1061
        }
1062

1063
        /// <inheritdoc />
1064
        public async ValueTask<IReadOnlyList<BatchMemberStatus>> QueryBatchMembersAsync(
1065
                string batchId,
1066
                BatchMemberQuery query,
1067
                CancellationToken cancellationToken = default
1068
        )
1069
        {
1070
                ArgumentException.ThrowIfNullOrWhiteSpace(batchId);
4✔
1071
                ArgumentNullException.ThrowIfNull(query);
4✔
1072
                ArgumentOutOfRangeException.ThrowIfNegative(query.Skip);
4✔
1073
                ArgumentOutOfRangeException.ThrowIfLessThanOrEqual(query.Take, 0);
4✔
1074
                await using var context = await contextFactory.CreateDbContextAsync(cancellationToken).ConfigureAwait(false);
4✔
1075
                var jobs = context.Set<ImmediateJobEntity>()
4✔
1076
                        .AsNoTracking()
4✔
1077
                        .Where(job => job.BatchId == batchId);
4✔
1078
                if (query.State is { } state)
4✔
1079
                        jobs = jobs.Where(job => job.State == state);
×
1080
                return await jobs.OrderBy(job => job.CreatedAt)
4✔
1081
                        .ThenBy(job => job.Id)
4✔
1082
                        .Skip(query.Skip)
4✔
1083
                        .Take(query.Take)
4✔
1084
                        .Select(job => new BatchMemberStatus(
4✔
1085
                                job.Id,
4✔
1086
                                job.JobName,
4✔
1087
                                job.QueueName,
4✔
1088
                                job.State,
4✔
1089
                                job.Attempt,
4✔
1090
                                job.CreatedAt,
4✔
1091
                                job.CompletedAt,
4✔
1092
                                job.LastError
4✔
1093
                        ))
4✔
1094
                        .ToListAsync(cancellationToken)
4✔
1095
                        .ConfigureAwait(false);
4✔
1096
        }
3✔
1097

1098
        /// <inheritdoc />
1099
        public async ValueTask<BatchGraph?> GetBatchGraphAsync(
1100
                string batchId,
1101
                CancellationToken cancellationToken = default
1102
        )
1103
        {
1104
                ArgumentException.ThrowIfNullOrWhiteSpace(batchId);
4✔
1105
                await using var context = await contextFactory.CreateDbContextAsync(cancellationToken).ConfigureAwait(false);
4✔
1106
                if (!await context.Set<ImmediateJobBatchEntity>()
4✔
1107
                        .AnyAsync(batch => batch.Id == batchId, cancellationToken)
4✔
1108
                        .ConfigureAwait(false))
4✔
1109
                {
1110
                        return null;
4✔
1111
                }
1112

1113
                var jobs = await context.Set<ImmediateJobEntity>()
×
1114
                        .AsNoTracking()
×
1115
                        .Where(job => job.BatchId == batchId)
×
1116
                        .OrderBy(job => job.CreatedAt)
×
1117
                        .ThenBy(job => job.Id)
×
1118
                        .Select(job => new BatchGraphNode(job.Id, job.JobName, job.State))
×
1119
                        .ToListAsync(cancellationToken)
×
1120
                        .ConfigureAwait(false);
×
1121
                var ids = jobs.Select(static job => job.JobId).ToArray();
×
1122
                var edges = ids.Length == 0
×
1123
                        ? []
×
1124
                        : await context.Set<ImmediateJobContinuationEntity>()
×
1125
                                .AsNoTracking()
×
1126
                                .Where(edge => ids.Contains(edge.ChildJobId))
×
1127
                                .OrderBy(edge => edge.ChildJobId)
×
1128
                                .ThenBy(edge => edge.ParentKind)
×
1129
                                .ThenBy(edge => edge.ParentId)
×
1130
                                .ToListAsync(cancellationToken)
×
1131
                                .ConfigureAwait(false);
×
1132
                return new(batchId, jobs, [.. edges.Select(ToGraphEdge)]);
4✔
1133
        }
3✔
1134

1135
        /// <inheritdoc />
1136
        public async ValueTask<JobStatus?> GetJobStatusAsync(
1137
                string jobId,
1138
                CancellationToken cancellationToken = default
1139
        )
1140
        {
1141
                ArgumentException.ThrowIfNullOrWhiteSpace(jobId);
5✔
1142
                await using var context = await contextFactory.CreateDbContextAsync(cancellationToken).ConfigureAwait(false);
5✔
1143
                var job = await context.Set<ImmediateJobEntity>()
5✔
1144
                        .AsNoTracking()
5✔
1145
                        .SingleOrDefaultAsync(item => item.Id == jobId, cancellationToken)
5✔
1146
                        .ConfigureAwait(false);
5✔
1147
                if (job is null)
5✔
1148
                        return null;
5✔
1149
                var edges = await context.Set<ImmediateJobContinuationEntity>()
5✔
1150
                        .AsNoTracking()
5✔
1151
                        .Where(edge => edge.ChildJobId == jobId)
5✔
1152
                        .OrderBy(edge => edge.ParentKind)
5✔
1153
                        .ThenBy(edge => edge.ParentId)
5✔
1154
                        .ToListAsync(cancellationToken)
5✔
1155
                        .ConfigureAwait(false);
5✔
1156
                return new(
5✔
1157
                        job.Id,
5✔
1158
                        job.JobName,
5✔
1159
                        job.QueueName,
5✔
1160
                        job.State,
5✔
1161
                        job.Attempt,
5✔
1162
                        MaxAttempts: null,
5✔
1163
                        job.CreatedAt,
5✔
1164
                        job.DueAt,
5✔
1165
                        job.CompletedAt,
5✔
1166
                        job.LastError,
5✔
1167
                        job.BatchId,
5✔
1168
                        [.. edges.Select(ToGraphEdge)]
5✔
1169
                );
5✔
1170
        }
4✔
1171

1172
        /// <inheritdoc />
1173
        public ValueTask CancelBatchAsync(string batchId, CancellationToken cancellationToken = default)
1174
        {
1175
                ArgumentException.ThrowIfNullOrWhiteSpace(batchId);
5✔
1176
                return ExecuteWithStrategyAsync(
5✔
1177
                        operationCancellationToken => CancelBatchCoreAsync(batchId, operationCancellationToken),
5✔
1178
                        cancellationToken
5✔
1179
                );
5✔
1180
        }
1181

1182
        private async Task CancelBatchCoreAsync(string batchId, CancellationToken cancellationToken)
1183
        {
1184
                var now = _timeProvider.GetUtcNow();
5✔
1185
                await using var context = await contextFactory.CreateDbContextAsync(cancellationToken).ConfigureAwait(false);
5✔
1186
                await using var transaction = await context.Database.BeginTransactionAsync(cancellationToken).ConfigureAwait(false);
5✔
1187
                var batch = await context.Set<ImmediateJobBatchEntity>()
5✔
1188
                        .SingleOrDefaultAsync(item => item.Id == batchId, cancellationToken)
5✔
1189
                        .ConfigureAwait(false)
5✔
1190
                        ?? throw new KeyNotFoundException($"Batch '{batchId}' was not found.");
5✔
1191
                if (batch.State != BatchState.Executing)
5✔
1192
                        throw new ImmediateJobException("Only an executing batch can be cancelled.");
×
1193

1194
                var jobs = await context.Set<ImmediateJobEntity>()
5✔
1195
                        .Where(job => job.BatchId == batchId)
5✔
1196
                        .ToListAsync(cancellationToken)
5✔
1197
                        .ConfigureAwait(false);
5✔
1198
                foreach (var job in jobs)
5✔
1199
                {
1200
                        if (IsTerminal(job.State))
5✔
1201
                                continue;
1202
                        job.State = JobState.Cancelled;
5✔
1203
                        job.CompletedAt = now;
5✔
1204
                        job.WorkerId = null;
5✔
1205
                        job.LeaseExpiresAt = null;
5✔
1206
                        job.ConcurrencyStamp = Guid.NewGuid();
5✔
1207
                        await PropagateTerminalAsync(context, job, now, cancellationToken).ConfigureAwait(false);
5✔
1208
                }
1209

1210
                var terminalGroups = GetTerminalFairQueueGroups(context);
5✔
1211
                _ = await context.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
5✔
1212
                await transaction.CommitAsync(cancellationToken).ConfigureAwait(false);
5✔
1213
                foreach (var (queueName, groupId) in terminalGroups)
5✔
1214
                {
1215
                        await TryRemoveFairQueueCursorAsync(queueName, groupId, CancellationToken.None).ConfigureAwait(false);
×
1216
                }
1217
        }
5✔
1218

1219
        /// <inheritdoc />
1220
        public ValueTask DeleteBatchAsync(string batchId, CancellationToken cancellationToken = default)
1221
        {
1222
                ArgumentException.ThrowIfNullOrWhiteSpace(batchId);
5✔
1223
                return ExecuteWithStrategyAsync(
5✔
1224
                        operationCancellationToken => DeleteBatchCoreAsync(batchId, operationCancellationToken),
5✔
1225
                        cancellationToken
5✔
1226
                );
5✔
1227
        }
1228

1229
        private async Task DeleteBatchCoreAsync(string batchId, CancellationToken cancellationToken)
1230
        {
1231
                await using var context = await contextFactory.CreateDbContextAsync(cancellationToken).ConfigureAwait(false);
5✔
1232
                await using var transaction = await context.Database.BeginTransactionAsync(cancellationToken).ConfigureAwait(false);
5✔
1233
                var batch = await context.Set<ImmediateJobBatchEntity>()
5✔
1234
                        .SingleOrDefaultAsync(item => item.Id == batchId, cancellationToken)
5✔
1235
                        .ConfigureAwait(false)
5✔
1236
                        ?? throw new KeyNotFoundException($"Batch '{batchId}' was not found.");
5✔
1237
                if (batch.State == BatchState.Executing)
4✔
1238
                        throw new ImmediateJobException("Only a terminal batch can be deleted.");
×
1239

1240
                var jobs = await context.Set<ImmediateJobEntity>()
4✔
1241
                        .Where(job => job.BatchId == batchId)
4✔
1242
                        .ToListAsync(cancellationToken)
4✔
1243
                        .ConfigureAwait(false);
4✔
1244
                var jobIds = jobs.Select(static job => job.Id).ToArray();
4✔
1245
                var edges = await context.Set<ImmediateJobContinuationEntity>()
4✔
1246
                        .Where(edge =>
4✔
1247
                                jobIds.Contains(edge.ChildJobId)
4✔
1248
                                || (edge.ParentKind == ContinuationParentKind.Job && jobIds.Contains(edge.ParentId))
4✔
1249
                                || (edge.ParentKind == ContinuationParentKind.Batch && edge.ParentId == batchId))
4✔
1250
                        .ToListAsync(cancellationToken)
4✔
1251
                        .ConfigureAwait(false);
4✔
1252
                context.RemoveRange(edges);
4✔
1253
                context.RemoveRange(jobs);
4✔
1254
                _ = context.Remove(batch);
4✔
1255
                _ = await context.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
4✔
1256
                await transaction.CommitAsync(cancellationToken).ConfigureAwait(false);
4✔
1257
        }
4✔
1258

1259
        /// <inheritdoc />
1260
        public ValueTask RetryAsync(string jobId, CancellationToken cancellationToken = default)
1261
        {
1262
                ArgumentException.ThrowIfNullOrWhiteSpace(jobId);
5✔
1263
                return ExecuteWithStrategyAsync(
5✔
1264
                        operationCancellationToken => RetryCoreAsync(jobId, operationCancellationToken),
5✔
1265
                        cancellationToken
5✔
1266
                );
5✔
1267
        }
1268

1269
        private async Task RetryCoreAsync(string jobId, CancellationToken cancellationToken)
1270
        {
1271
                await using var context = await contextFactory.CreateDbContextAsync(cancellationToken).ConfigureAwait(false);
5✔
1272
                await using var transaction = await context.Database.BeginTransactionAsync(cancellationToken).ConfigureAwait(false);
5✔
1273
                var job = await context.Set<ImmediateJobEntity>()
5✔
1274
                        .SingleOrDefaultAsync(item => item.Id == jobId &&
5✔
1275
                                (item.State == JobState.Failed || item.State == JobState.Scheduled), cancellationToken)
5✔
1276
                        .ConfigureAwait(false);
5✔
1277
                if (job is null)
5✔
1278
                {
1279
                        if (await context.Set<ImmediateJobEntity>()
5✔
1280
                                .AnyAsync(item => item.Id == jobId, cancellationToken)
5✔
1281
                                .ConfigureAwait(false))
5✔
1282
                        {
1283
                                throw new ImmediateJobException("Only failed or scheduled jobs can be retried.");
4✔
1284
                        }
1285

1286
                        throw new KeyNotFoundException($"Job '{jobId}' was not found.");
5✔
1287
                }
1288

1289
                var wasFailed = job.State == JobState.Failed;
1✔
1290
                if (wasFailed && job.BatchId is { } batchId)
1✔
1291
                {
1292
                        var batch = await context.Set<ImmediateJobBatchEntity>()
×
1293
                                .SingleAsync(item => item.Id == batchId, cancellationToken)
×
1294
                                .ConfigureAwait(false);
×
1295
                        batch.PendingCount++;
×
1296
                        batch.FailedCount = Math.Max(0, batch.FailedCount - 1);
×
1297
                        batch.State = BatchState.Executing;
×
1298
                        batch.CompletedAt = null;
×
1299
                        batch.ConcurrencyStamp = Guid.NewGuid();
×
1300
                }
1301

1302
                job.State = JobState.Pending;
1✔
1303
                job.DueAt = _timeProvider.GetUtcNow();
1✔
1304
                job.WorkerId = null;
1✔
1305
                job.LeaseExpiresAt = null;
1✔
1306
                if (wasFailed)
1✔
1307
                {
1308
                        job.CompletedAt = null;
1✔
1309
                        job.LastError = null;
1✔
1310
                }
1311

1312
                job.ConcurrencyStamp = Guid.NewGuid();
1✔
1313
                try
1314
                {
1315
                        _ = await context.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
1✔
1316
                }
1✔
1317
                catch (DbUpdateConcurrencyException)
1318
                {
1319
                        if (!await context.Set<ImmediateJobEntity>()
×
1320
                                .AsNoTracking()
×
1321
                                .AnyAsync(item => item.Id == jobId, cancellationToken)
×
1322
                                .ConfigureAwait(false))
×
1323
                        {
1324
                                throw new KeyNotFoundException($"Job '{jobId}' was not found.");
×
1325
                        }
1326

NEW
1327
                        throw new ImmediateJobException("Only failed or scheduled jobs can be retried.");
×
1328
                }
1329

1330
                await transaction.CommitAsync(cancellationToken).ConfigureAwait(false);
1✔
1331
        }
1✔
1332

1333
        /// <inheritdoc />
1334
        public ValueTask DeleteAsync(string jobId, CancellationToken cancellationToken = default)
1335
        {
1336
                ArgumentException.ThrowIfNullOrWhiteSpace(jobId);
5✔
1337
                return ExecuteWithStrategyAsync(
5✔
1338
                        operationCancellationToken => DeleteCoreAsync(jobId, operationCancellationToken),
5✔
1339
                        cancellationToken
5✔
1340
                );
5✔
1341
        }
1342

1343
        private async Task DeleteCoreAsync(string jobId, CancellationToken cancellationToken)
1344
        {
1345
                await using var context = await contextFactory.CreateDbContextAsync(cancellationToken).ConfigureAwait(false);
5✔
1346
                await using var transaction = await context.Database.BeginTransactionAsync(cancellationToken).ConfigureAwait(false);
5✔
1347
                var job = await context.Set<ImmediateJobEntity>()
5✔
1348
                        .AsNoTracking()
5✔
1349
                        .SingleOrDefaultAsync(item => item.Id == jobId
5✔
1350
                                && (item.State == JobState.Succeeded || item.State == JobState.Failed || item.State == JobState.Cancelled), cancellationToken)
5✔
1351
                        .ConfigureAwait(false);
5✔
1352
                if (job is null)
5✔
1353
                {
1354
                        if (await context.Set<ImmediateJobEntity>()
5✔
1355
                                .AnyAsync(item => item.Id == jobId, cancellationToken)
5✔
1356
                                .ConfigureAwait(false))
5✔
1357
                        {
1358
                                throw new ImmediateJobException("Only terminal jobs can be deleted.");
4✔
1359
                        }
1360

1361
                        throw new KeyNotFoundException($"Job '{jobId}' was not found.");
5✔
1362
                }
1363

1364
                if (job.BatchId is not null)
1✔
1365
                        throw new ImmediateJobException("Batch members are deleted with their batch so the workflow remains coherent.");
×
1366
                _ = await context.Set<ImmediateJobContinuationEntity>()
1✔
1367
                        .Where(edge => edge.ChildJobId == jobId ||
1✔
1368
                                (edge.ParentKind == ContinuationParentKind.Job && edge.ParentId == jobId))
1✔
1369
                        .ExecuteDeleteAsync(cancellationToken)
1✔
1370
                        .ConfigureAwait(false);
1✔
1371
                var removed = await context.Set<ImmediateJobEntity>()
1✔
1372
                        .Where(item => item.Id == jobId &&
1✔
1373
                                (item.State == JobState.Succeeded || item.State == JobState.Failed || item.State == JobState.Cancelled))
1✔
1374
                        .ExecuteDeleteAsync(cancellationToken)
1✔
1375
                        .ConfigureAwait(false);
1✔
1376
                if (removed == 0)
1✔
1377
                {
1378
                        if (await context.Set<ImmediateJobEntity>()
×
1379
                                .AnyAsync(item => item.Id == jobId, cancellationToken)
×
1380
                                .ConfigureAwait(false))
×
1381
                        {
1382
                                throw new ImmediateJobException("Only terminal jobs can be deleted.");
×
1383
                        }
1384

1385
                        throw new KeyNotFoundException($"Job '{jobId}' was not found.");
×
1386
                }
1387

1388
                await transaction.CommitAsync(cancellationToken).ConfigureAwait(false);
1✔
1389
        }
1✔
1390

1391
        /// <inheritdoc />
1392
        public ValueTask PurgeJobsAsync(
1393
                TimeSpan succeededRetention,
1394
                TimeSpan failedRetention,
1395
                CancellationToken cancellationToken = default
1396
        )
1397
        {
1398
                var now = _timeProvider.GetUtcNow();
4✔
1399
                return ExecuteWithStrategyAsync(
4✔
1400
                        operationCancellationToken => PurgeJobsCoreAsync(
4✔
1401
                                now - succeededRetention,
4✔
1402
                                now - failedRetention,
4✔
1403
                                operationCancellationToken
4✔
1404
                        ),
4✔
1405
                        cancellationToken
4✔
1406
                );
4✔
1407
        }
1408

1409
        /// <inheritdoc />
1410
        public ValueTask PurgeBatchesAsync(
1411
                TimeSpan batchSucceededRetention,
1412
                TimeSpan batchFailedRetention,
1413
                CancellationToken cancellationToken = default
1414
        )
1415
        {
1416
                var now = _timeProvider.GetUtcNow();
4✔
1417
                return ExecuteWithStrategyAsync(
4✔
1418
                        operationCancellationToken => PurgeBatchesCoreAsync(
4✔
1419
                                now - batchSucceededRetention,
4✔
1420
                                now - batchFailedRetention,
4✔
1421
                                operationCancellationToken
4✔
1422
                        ),
4✔
1423
                        cancellationToken
4✔
1424
                );
4✔
1425
        }
1426

1427
        private async Task PurgeJobsCoreAsync(
1428
                DateTimeOffset succeededBefore,
1429
                DateTimeOffset failedBefore,
1430
                CancellationToken cancellationToken
1431
        )
1432
        {
1433
                await using var context = await contextFactory.CreateDbContextAsync(cancellationToken).ConfigureAwait(false);
4✔
1434
                await using var transaction = await context.Database.BeginTransactionAsync(cancellationToken).ConfigureAwait(false);
4✔
1435
                var jobs = await context.Set<ImmediateJobEntity>()
4✔
1436
                        .Where(job => job.BatchId == null
4✔
1437
                                && ((job.State == JobState.Succeeded && job.CompletedAt < succeededBefore)
4✔
1438
                                || ((job.State == JobState.Failed || job.State == JobState.Cancelled) && job.CompletedAt < failedBefore))
4✔
1439
                        )
4✔
1440
                        .ToListAsync(cancellationToken)
4✔
1441
                        .ConfigureAwait(false);
4✔
1442
                if (jobs.Count != 0)
4✔
1443
                {
1444
                        var jobIds = jobs.Select(static job => job.Id).ToArray();
×
1445
                        var edges = await context.Set<ImmediateJobContinuationEntity>()
×
1446
                                .Where(edge =>
×
1447
                                        jobIds.Contains(edge.ChildJobId)
×
1448
                                        || (jobIds.Contains(edge.ParentId) && edge.ParentKind == ContinuationParentKind.Job)
×
1449
                                )
×
1450
                                .ToListAsync(cancellationToken)
×
1451
                                .ConfigureAwait(false);
×
1452
                        context.RemoveRange(edges);
×
1453
                }
1454

1455
                context.RemoveRange(jobs);
4✔
1456
                _ = await context.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
4✔
1457
                await transaction.CommitAsync(cancellationToken).ConfigureAwait(false);
4✔
1458
        }
4✔
1459

1460
        private async Task PurgeBatchesCoreAsync(
1461
                DateTimeOffset batchSucceededBefore,
1462
                DateTimeOffset batchFailedBefore,
1463
                CancellationToken cancellationToken
1464
        )
1465
        {
1466
                await using var context = await contextFactory.CreateDbContextAsync(cancellationToken).ConfigureAwait(false);
4✔
1467
                await using var transaction = await context.Database.BeginTransactionAsync(cancellationToken).ConfigureAwait(false);
4✔
1468
                var batches = await context.Set<ImmediateJobBatchEntity>()
4✔
1469
                        .Where(batch => (batch.State == BatchState.Succeeded && batch.CompletedAt < batchSucceededBefore)
4✔
1470
                                || ((batch.State == BatchState.Failed || batch.State == BatchState.Cancelled)
4✔
1471
                                        && batch.CompletedAt < batchFailedBefore))
4✔
1472
                        .ToListAsync(cancellationToken)
4✔
1473
                        .ConfigureAwait(false);
4✔
1474
                if (batches.Count != 0)
4✔
1475
                {
1476
                        var batchIds = batches.Select(static batch => batch.Id).ToArray();
×
1477
                        var memberIds = await context.Set<ImmediateJobEntity>()
×
1478
                                .Where(job => job.BatchId != null && batchIds.Contains(job.BatchId))
×
1479
                                .Select(job => job.Id)
×
1480
                                .ToListAsync(cancellationToken)
×
1481
                                .ConfigureAwait(false);
×
1482
                        var edges = await context.Set<ImmediateJobContinuationEntity>()
×
1483
                                .Where(edge =>
×
1484
                                        (batchIds.Contains(edge.ParentId) && edge.ParentKind == ContinuationParentKind.Batch)
×
1485
                                        || memberIds.Contains(edge.ChildJobId)
×
1486
                                        || (memberIds.Contains(edge.ParentId) && edge.ParentKind == ContinuationParentKind.Job)
×
1487
                                )
×
1488
                                .ToListAsync(cancellationToken)
×
1489
                                .ConfigureAwait(false);
×
1490
                        context.RemoveRange(edges);
×
1491
                        context.RemoveRange(batches);
×
1492
                }
1493

1494
                _ = await context.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
4✔
1495
                await transaction.CommitAsync(cancellationToken).ConfigureAwait(false);
4✔
1496
        }
4✔
1497

1498
        /// <inheritdoc />
1499
        public async ValueTask HeartbeatAsync(JobServerSnapshot server, CancellationToken cancellationToken = default)
1500
        {
1501
                ArgumentNullException.ThrowIfNull(server);
1✔
1502
                await using var context = await contextFactory.CreateDbContextAsync(cancellationToken).ConfigureAwait(false);
1✔
1503
                var cutoff = _timeProvider.GetUtcNow() - TimeSpan.FromMinutes(2);
1✔
1504
                _ = await context.Set<ImmediateJobServerEntity>()
1✔
1505
                        .Where(item => item.LastHeartbeat < cutoff)
1✔
1506
                        .ExecuteDeleteAsync(cancellationToken)
1✔
1507
                        .ConfigureAwait(false);
1✔
1508
                var entity = await context.Set<ImmediateJobServerEntity>().FindAsync([server.WorkerId], cancellationToken).ConfigureAwait(false);
1✔
1509
                if (entity is null)
1✔
1510
                {
1511
                        _ = context.Add(new ImmediateJobServerEntity
1✔
1512
                        {
1✔
1513
                                WorkerId = server.WorkerId,
1✔
1514
                                LastHeartbeat = server.LastHeartbeat,
1✔
1515
                                ActiveWorkers = server.ActiveWorkers,
1✔
1516
                                MaxWorkers = server.MaxWorkers,
1✔
1517
                        });
1✔
1518
                }
1519
                else
1520
                {
1521
                        entity.LastHeartbeat = server.LastHeartbeat;
×
1522
                        entity.ActiveWorkers = server.ActiveWorkers;
×
1523
                        entity.MaxWorkers = server.MaxWorkers;
×
1524
                }
1525

1526
                _ = await context.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
1✔
1527
        }
1✔
1528

1529
        /// <inheritdoc />
1530
        public async ValueTask<bool> IsHealthyAsync(CancellationToken cancellationToken = default)
1531
        {
1532
                try
1533
                {
1534
                        await using var context = await contextFactory.CreateDbContextAsync(cancellationToken).ConfigureAwait(false);
×
1535
                        return await context.Database.CanConnectAsync(cancellationToken).ConfigureAwait(false);
×
1536
                }
×
1537
                catch (Exception exception) when (exception is DbException or InvalidOperationException)
×
1538
                {
1539
                        return false;
×
1540
                }
1541
        }
×
1542

1543
        private ValueTask MutateOwnedWithDependenciesAsync(
1544
                string jobId,
1545
                string workerId,
1546
                string? error,
1547
                DateTimeOffset? nextRetryAt,
1548
                bool succeeded,
1549
                IReadOnlyList<JobContinuationAddition> additions,
1550
                CancellationToken cancellationToken
1551
        ) => RetryConcurrencyAsync(
5✔
1552
                operationCancellationToken => MutateOwnedCoreAsync(
5✔
1553
                        jobId,
5✔
1554
                        workerId,
5✔
1555
                        error,
5✔
1556
                        nextRetryAt,
5✔
1557
                        succeeded,
5✔
1558
                        additions,
5✔
1559
                        operationCancellationToken
5✔
1560
                ),
5✔
1561
                cancellationToken
5✔
1562
        );
5✔
1563

1564
        private async ValueTask RetryConcurrencyAsync(
1565
                Func<CancellationToken, Task> operation,
1566
                CancellationToken cancellationToken
1567
        )
1568
        {
1569
                for (var attempt = 0; attempt < MaxConcurrencyAttempts; attempt++)
5✔
1570
                {
1571
                        try
1572
                        {
1573
                                await ExecuteWithStrategyAsync(operation, cancellationToken).ConfigureAwait(false);
5✔
1574
                                return;
5✔
1575
                        }
1576
                        catch (DbUpdateConcurrencyException) when (attempt + 1 < MaxConcurrencyAttempts)
5✔
1577
                        {
1578
                                // The transaction rolled back, so the next attempt re-evaluates the graph from durable state.
1579
                        }
1✔
1580
                }
1581
        }
5✔
1582

1583
        private async ValueTask ExecuteWithStrategyAsync(
1584
                Func<CancellationToken, Task> operation,
1585
                CancellationToken cancellationToken
1586
        )
1587
        {
1588
                await using var strategyContext = await contextFactory.CreateDbContextAsync(cancellationToken).ConfigureAwait(false);
5✔
1589
                var strategy = strategyContext.Database.CreateExecutionStrategy();
5✔
1590
                await strategy.ExecuteAsync(operation, cancellationToken).ConfigureAwait(false);
5✔
1591
        }
5✔
1592

1593
        private async ValueTask MutateOwnedAsync(
1594
                string jobId,
1595
                string workerId,
1596
                Action<ImmediateJobEntity> mutate,
1597
                CancellationToken cancellationToken
1598
        )
1599
        {
1600
                await using var context = await contextFactory.CreateDbContextAsync(cancellationToken).ConfigureAwait(false);
1✔
1601
                var job = await context.Set<ImmediateJobEntity>()
1✔
1602
                        .SingleOrDefaultAsync(item => item.Id == jobId && item.State == JobState.Active && item.WorkerId == workerId, cancellationToken)
1✔
1603
                        .ConfigureAwait(false);
1✔
1604
                if (job is null)
1✔
1605
                        return;
1606
                mutate(job);
1✔
1607
                job.ConcurrencyStamp = Guid.NewGuid();
1✔
1608
                try
1609
                {
1610
                        _ = await context.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
1✔
1611
                }
1✔
1612
                catch (DbUpdateConcurrencyException)
×
1613
                {
1614
                        // A stale worker must not update a lease that has been reclaimed.
1615
                }
1✔
1616
        }
1✔
1617

1618
        private async Task MutateOwnedCoreAsync(
1619
                string jobId,
1620
                string workerId,
1621
                string? error,
1622
                DateTimeOffset? nextRetryAt,
1623
                bool succeeded,
1624
                IReadOnlyList<JobContinuationAddition> additions,
1625
                CancellationToken cancellationToken
1626
        )
1627
        {
1628
                await using var context = await contextFactory.CreateDbContextAsync(cancellationToken).ConfigureAwait(false);
5✔
1629
                await using var transaction = await context.Database.BeginTransactionAsync(cancellationToken).ConfigureAwait(false);
5✔
1630
                var job = await context.Set<ImmediateJobEntity>()
5✔
1631
                        .SingleOrDefaultAsync(item => item.Id == jobId && item.State == JobState.Active && item.WorkerId == workerId, cancellationToken)
5✔
1632
                        .ConfigureAwait(false);
5✔
1633
                if (job is null)
5✔
1634
                        throw new ImmediateJobException($"Worker '{workerId}' does not own active job '{jobId}'.");
1✔
1635

1636
                var now = _timeProvider.GetUtcNow();
5✔
1637
                job.WorkerId = null;
5✔
1638
                job.LeaseExpiresAt = null;
5✔
1639
                job.LastError = error;
5✔
1640
                job.ConcurrencyStamp = Guid.NewGuid();
5✔
1641
                if (!succeeded && nextRetryAt is { } retryAt)
5✔
1642
                {
1643
                        job.State = retryAt <= now ? JobState.Pending : JobState.Scheduled;
×
1644
                        job.DueAt = retryAt;
×
1645
                        job.CompletedAt = null;
×
1646
                        _ = await context.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
×
1647
                        await transaction.CommitAsync(cancellationToken).ConfigureAwait(false);
×
1648
                        return;
×
1649
                }
1650

1651
                if (succeeded && additions.Count != 0)
5✔
1652
                {
1653
                        await FlushContinuationAdditionsAsync(context, job, additions, cancellationToken).ConfigureAwait(false);
5✔
1654
                        _ = await context.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
×
1655
                }
1656

1657
                job.State = succeeded ? JobState.Succeeded : JobState.Failed;
5✔
1658
                job.CompletedAt = now;
5✔
1659
                await PropagateTerminalAsync(context, job, now, cancellationToken).ConfigureAwait(false);
5✔
1660
                var terminalGroups = GetTerminalFairQueueGroups(context);
5✔
1661
                _ = await context.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
5✔
1662
                await transaction.CommitAsync(cancellationToken).ConfigureAwait(false);
5✔
1663
                foreach (var (queueName, groupId) in terminalGroups)
5✔
1664
                        await TryRemoveFairQueueCursorAsync(queueName, groupId, CancellationToken.None).ConfigureAwait(false);
5✔
1665
        }
5✔
1666

1667
        private static (string QueueName, string GroupId)[] GetTerminalFairQueueGroups(TContext context) =>
1668
                [
5✔
1669
                        .. context.ChangeTracker
5✔
1670
                                .Entries<ImmediateJobEntity>()
5✔
1671
                                .Select(static entry => entry.Entity)
5✔
1672
                                .Where(static job => job.GroupId is not null && IsTerminal(job.State))
5✔
1673
                                .Select(static job => (job.QueueName, GroupId: job.GroupId!))
5✔
1674
                                .Distinct(),
5✔
1675
                ];
5✔
1676

1677
        private async ValueTask TryRemoveFairQueueCursorAsync(
1678
                string queueName,
1679
                string groupId,
1680
                CancellationToken cancellationToken
1681
        )
1682
        {
1683
                try
1684
                {
1685
                        await using var context = await contextFactory.CreateDbContextAsync(cancellationToken).ConfigureAwait(false);
5✔
1686
                        if (await HasLiveGroupJobsAsync(context, queueName, groupId, cancellationToken).ConfigureAwait(false))
5✔
1687
                        {
1688
                                return;
1689
                        }
1690

1691
                        var cursor = await context.Set<ImmediateFairQueueGroupEntity>()
4✔
1692
                                .SingleOrDefaultAsync(
4✔
1693
                                        group => group.QueueName == queueName && group.GroupId == groupId,
4✔
1694
                                        cancellationToken
4✔
1695
                                )
4✔
1696
                                .ConfigureAwait(false);
4✔
1697
                        if (cursor is null)
4✔
1698
                                return;
1699

1700
                        _ = context.Remove(cursor);
4✔
1701
                        _ = await context.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
4✔
1702
                }
4✔
1703
                catch (Exception exception) when (
×
1704
                        !cancellationToken.IsCancellationRequested
×
1705
                        && exception is DbException or DbUpdateException
×
1706
                )
×
1707
                {
1708
                        // Cleanup is best-effort metadata maintenance and must not invalidate a committed transition.
1709
                }
×
1710
        }
5✔
1711

1712
        private static Task<bool> HasLiveGroupJobsAsync(
1713
                TContext context,
1714
                string queueName,
1715
                string groupId,
1716
                CancellationToken cancellationToken
1717
        ) => context.Set<ImmediateJobEntity>().AnyAsync(
5✔
1718
                job => job.QueueName == queueName
5✔
1719
                        && job.GroupId == groupId
5✔
1720
                        && (job.State == JobState.Pending
5✔
1721
                                || job.State == JobState.Scheduled
5✔
1722
                                || job.State == JobState.Active),
5✔
1723
                cancellationToken
5✔
1724
        );
5✔
1725

1726
        private async Task AddBatchJobCoreAsync(
1727
                string currentJobId,
1728
                JobRecord record,
1729
                ContinuationOptions options,
1730
                CancellationToken cancellationToken
1731
        )
1732
        {
1733
                await using var context = await contextFactory.CreateDbContextAsync(cancellationToken).ConfigureAwait(false);
5✔
1734
                await using var transaction = await context.Database.BeginTransactionAsync(cancellationToken).ConfigureAwait(false);
5✔
1735
                var current = await context.Set<ImmediateJobEntity>()
5✔
1736
                        .SingleOrDefaultAsync(job => job.Id == currentJobId && job.State == JobState.Active, cancellationToken)
5✔
1737
                        .ConfigureAwait(false)
5✔
1738
                        ?? throw new ImmediateJobException($"The current active job '{currentJobId}' was not found.");
5✔
1739
                if (current.BatchId is not { } batchId)
5✔
1740
                        throw new ImmediateJobException("The current job does not belong to a batch.");
×
1741
                if (options is not (ContinuationOptions.BesideContinuations or ContinuationOptions.BeforeContinuations))
5✔
1742
                        throw new ArgumentOutOfRangeException(nameof(options));
×
1743
                if (!string.Equals(record.BatchId, batchId, StringComparison.Ordinal))
5✔
1744
                        throw new ImmediateJobException("The new job must belong to the current job's batch.");
5✔
1745
                if (record.State is JobState.Active or JobState.AwaitingContinuation || IsTerminal(record.State))
×
1746
                        throw new ImmediateJobException($"Concurrent batch member '{record.Id}' has invalid state '{record.State}'.");
×
1747

1748
                var batch = await context.Set<ImmediateJobBatchEntity>()
×
1749
                        .SingleAsync(item => item.Id == batchId && item.State == BatchState.Executing, cancellationToken)
×
1750
                        .ConfigureAwait(false);
×
1751
                var job = ToEntity(record);
×
1752
                _ = context.Add(job);
×
1753
                batch.TotalJobs++;
×
1754
                batch.PendingCount++;
×
1755
                batch.ConcurrencyStamp = Guid.NewGuid();
×
1756

1757
                if (options == ContinuationOptions.BeforeContinuations)
×
1758
                {
1759
                        var waiters = await GetActiveWaitersAsync(context, currentJobId, cancellationToken).ConfigureAwait(false);
×
1760
                        foreach (var waiter in waiters)
×
1761
                        {
1762
                                _ = context.Add(new ImmediateJobContinuationEntity
×
1763
                                {
×
1764
                                        ChildJobId = waiter.Id,
×
1765
                                        ParentKind = ContinuationParentKind.Job,
×
1766
                                        ParentId = job.Id,
×
1767
                                        Trigger = ContinuationTrigger.Success,
×
1768
                                });
×
1769
                                waiter.RemainingDependencies++;
×
1770
                                waiter.ConcurrencyStamp = Guid.NewGuid();
×
1771
                        }
1772
                }
1773

1774
                _ = await context.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
×
1775
                await transaction.CommitAsync(cancellationToken).ConfigureAwait(false);
×
1776
        }
×
1777

1778
        private static async Task FlushContinuationAdditionsAsync(
1779
                TContext context,
1780
                ImmediateJobEntity current,
1781
                IReadOnlyList<JobContinuationAddition> additions,
1782
                CancellationToken cancellationToken
1783
        )
1784
        {
1785
                var ids = new HashSet<string>(StringComparer.Ordinal);
5✔
1786
                var trackedAdditions = 0;
5✔
1787
                foreach (var addition in additions)
5✔
1788
                {
1789
                        ArgumentNullException.ThrowIfNull(addition);
5✔
1790
                        ArgumentNullException.ThrowIfNull(addition.Job);
5✔
1791
                        if (!ids.Add(addition.Job.Id))
5✔
1792
                                throw new ImmediateJobException("Buffered continuations contain duplicate job identifiers.");
×
1793
                        if (!Enum.IsDefined(addition.Trigger))
5✔
1794
                                throw new ArgumentOutOfRangeException(nameof(additions), "Unknown continuation trigger.");
4✔
1795
                        if (addition.Job.State is not (JobState.Pending or JobState.Scheduled))
5✔
1796
                                throw new ImmediateJobException($"Dynamic continuation '{addition.Job.Id}' has invalid state '{addition.Job.State}'.");
×
1797

1798
                        if (addition.Options == ContinuationOptions.Detached)
5✔
1799
                        {
1800
                                if (addition.Job.BatchId is not null)
5✔
1801
                                        throw new ImmediateJobException("A detached continuation cannot belong to a batch.");
5✔
1802
                        }
1803
                        else if (addition.Options is ContinuationOptions.BesideContinuations or ContinuationOptions.BeforeContinuations)
5✔
1804
                        {
1805
                                if (current.BatchId is null || !string.Equals(addition.Job.BatchId, current.BatchId, StringComparison.Ordinal))
5✔
1806
                                        throw new ImmediateJobException("A batch-tracked continuation must belong to the current job's batch.");
5✔
1807
                                trackedAdditions++;
×
1808
                        }
1809
                        else
1810
                        {
1811
                                throw new ArgumentOutOfRangeException(nameof(additions), "Unknown continuation option.");
4✔
1812
                        }
1813
                }
1814

1815
                var waiters = additions.Any(static addition => addition.Options == ContinuationOptions.BeforeContinuations)
×
1816
                        ? await GetActiveWaitersAsync(context, current.Id, cancellationToken).ConfigureAwait(false)
×
1817
                        : [];
×
1818
                ImmediateJobBatchEntity? batch = null;
1819
                if (trackedAdditions != 0)
×
1820
                {
1821
                        if (current.BatchId is not { } batchId)
×
1822
                                throw new ImmediateJobException("The current job does not belong to a batch.");
×
1823
                        batch = await context.Set<ImmediateJobBatchEntity>()
×
1824
                                .SingleAsync(item => item.Id == batchId && item.State == BatchState.Executing, cancellationToken)
×
1825
                                .ConfigureAwait(false);
×
1826
                        batch.TotalJobs += trackedAdditions;
×
1827
                        batch.PendingCount += trackedAdditions;
×
1828
                        batch.ConcurrencyStamp = Guid.NewGuid();
×
1829
                }
1830

1831
                foreach (var addition in additions)
×
1832
                {
1833
                        var job = ToEntity(addition.Job with
×
1834
                        {
×
1835
                                State = JobState.AwaitingContinuation,
×
1836
                                RemainingDependencies = 1,
×
1837
                        });
×
1838
                        _ = context.Add(job);
×
1839
                        _ = context.Add(new ImmediateJobContinuationEntity
×
1840
                        {
×
1841
                                ChildJobId = job.Id,
×
1842
                                ParentKind = ContinuationParentKind.Job,
×
1843
                                ParentId = current.Id,
×
1844
                                Trigger = addition.Trigger,
×
1845
                        });
×
1846

1847
                        if (addition.Options != ContinuationOptions.BeforeContinuations)
×
1848
                                continue;
1849
                        foreach (var waiter in waiters)
×
1850
                        {
1851
                                _ = context.Add(new ImmediateJobContinuationEntity
×
1852
                                {
×
1853
                                        ChildJobId = waiter.Id,
×
1854
                                        ParentKind = ContinuationParentKind.Job,
×
1855
                                        ParentId = job.Id,
×
1856
                                        Trigger = ContinuationTrigger.Success,
×
1857
                                });
×
1858
                                waiter.RemainingDependencies++;
×
1859
                                waiter.ConcurrencyStamp = Guid.NewGuid();
×
1860
                        }
1861
                }
1862
        }
×
1863

1864
        private static async Task<List<ImmediateJobEntity>> GetActiveWaitersAsync(
1865
                TContext context,
1866
                string currentJobId,
1867
                CancellationToken cancellationToken
1868
        )
1869
        {
1870
                var waiterIds = await context.Set<ImmediateJobContinuationEntity>()
×
1871
                        .Where(edge => edge.ParentKind == ContinuationParentKind.Job && edge.ParentId == currentJobId)
×
1872
                        .Select(edge => edge.ChildJobId)
×
1873
                        .Distinct()
×
1874
                        .ToArrayAsync(cancellationToken)
×
1875
                        .ConfigureAwait(false);
×
1876
                return waiterIds.Length == 0
×
1877
                        ? []
×
1878
                        : await context.Set<ImmediateJobEntity>()
×
1879
                                .Where(job => waiterIds.Contains(job.Id) && job.State == JobState.AwaitingContinuation)
×
1880
                                .ToListAsync(cancellationToken)
×
1881
                                .ConfigureAwait(false);
×
1882
        }
1883

1884
        private static async Task PropagateTerminalAsync(
1885
                TContext context,
1886
                ImmediateJobEntity terminalJob,
1887
                DateTimeOffset now,
1888
                CancellationToken cancellationToken
1889
        )
1890
        {
1891
                var parents = new Queue<(ContinuationParentKind Kind, string Id, ContinuationParentOutcome Outcome)>();
5✔
1892
                var processed = new HashSet<(ContinuationParentKind Kind, string Id)>();
5✔
1893
                parents.Enqueue((
5✔
1894
                        ContinuationParentKind.Job,
5✔
1895
                        terminalJob.Id,
5✔
1896
                        GetParentOutcome(terminalJob.State)
5✔
1897
                ));
5✔
1898
                await UpdateBatchForTerminalJobAsync(context, terminalJob, now, parents, cancellationToken).ConfigureAwait(false);
5✔
1899

1900
                while (parents.TryDequeue(out var parent))
5✔
1901
                {
1902
                        if (!processed.Add((parent.Kind, parent.Id)))
5✔
1903
                                continue;
1904
                        var edges = await context.Set<ImmediateJobContinuationEntity>()
5✔
1905
                                .Where(edge => edge.ParentKind == parent.Kind
5✔
1906
                                        && edge.ParentId == parent.Id
5✔
1907
                                        && edge.ParentOutcome == ContinuationParentOutcome.Unsettled)
5✔
1908
                                .ToListAsync(cancellationToken)
5✔
1909
                                .ConfigureAwait(false);
5✔
1910
                        foreach (var edge in edges)
5✔
1911
                        {
1912
                                edge.ParentOutcome = parent.Outcome;
5✔
1913
                                var child = await context.Set<ImmediateJobEntity>()
5✔
1914
                                        .SingleOrDefaultAsync(job => job.Id == edge.ChildJobId, cancellationToken)
5✔
1915
                                        .ConfigureAwait(false);
5✔
1916
                                if (child is null || IsTerminal(child.State))
5✔
1917
                                        continue;
1918

1919
                                if (child.State != JobState.AwaitingContinuation || child.RemainingDependencies <= 0)
5✔
1920
                                        continue;
1921
                                child.RemainingDependencies--;
5✔
1922
                                if (parent.Outcome == ContinuationParentOutcome.Failed)
5✔
1923
                                        child.FailedDependencies++;
5✔
1924
                                if (child.RemainingDependencies == 0)
5✔
1925
                                {
1926
                                        var cancel = await ShouldCancelSettledContinuationAsync(
5✔
1927
                                                context,
5✔
1928
                                                child.Id,
5✔
1929
                                                cancellationToken
5✔
1930
                                        ).ConfigureAwait(false);
5✔
1931
                                        if (cancel)
5✔
1932
                                        {
1933
                                                child.State = JobState.Cancelled;
5✔
1934
                                                child.CompletedAt = now;
5✔
1935
                                                parents.Enqueue((ContinuationParentKind.Job, child.Id, ContinuationParentOutcome.Other));
5✔
1936
                                                await UpdateBatchForTerminalJobAsync(context, child, now, parents, cancellationToken).ConfigureAwait(false);
5✔
1937
                                        }
1938
                                        else
1939
                                        {
1940
                                                child.State = child.DueAt <= now ? JobState.Pending : JobState.Scheduled;
5✔
1941
                                        }
1942
                                }
1943

1944
                                child.ConcurrencyStamp = Guid.NewGuid();
5✔
1945
                        }
4✔
1946
                }
4✔
1947
        }
5✔
1948

1949
        private static async Task<bool> ShouldCancelSettledContinuationAsync(
1950
                TContext context,
1951
                string childJobId,
1952
                CancellationToken cancellationToken
1953
        )
1954
        {
1955
                var edges = await context.Set<ImmediateJobContinuationEntity>()
5✔
1956
                        .Where(edge => edge.ChildJobId == childJobId)
5✔
1957
                        .ToListAsync(cancellationToken)
5✔
1958
                        .ConfigureAwait(false);
5✔
1959
                var requiresFailure = false;
5✔
1960
                var anyFailed = false;
5✔
1961
                foreach (var edge in edges)
5✔
1962
                {
1963
                        if (edge.Trigger == ContinuationTrigger.Success
5✔
1964
                                && edge.ParentOutcome != ContinuationParentOutcome.Succeeded)
5✔
1965
                                return true;
×
1966
                        requiresFailure |= edge.Trigger == ContinuationTrigger.Failure;
5✔
1967
                        anyFailed |= edge.ParentOutcome == ContinuationParentOutcome.Failed;
5✔
1968
                }
1969

1970
                return requiresFailure && !anyFailed;
5✔
1971
        }
4✔
1972

1973
        private static async Task UpdateBatchForTerminalJobAsync(
1974
                TContext context,
1975
                ImmediateJobEntity job,
1976
                DateTimeOffset now,
1977
                Queue<(ContinuationParentKind Kind, string Id, ContinuationParentOutcome Outcome)> parents,
1978
                CancellationToken cancellationToken
1979
        )
1980
        {
1981
                if (job.BatchId is not { } batchId)
5✔
1982
                        return;
5✔
1983
                var batch = await context.Set<ImmediateJobBatchEntity>()
5✔
1984
                        .SingleAsync(item => item.Id == batchId, cancellationToken)
5✔
1985
                        .ConfigureAwait(false);
5✔
1986
                batch.PendingCount = Math.Max(0, batch.PendingCount - 1);
5✔
1987
                switch (job.State)
5✔
1988
                {
1989
                        case JobState.Succeeded:
1990
                                batch.SucceededCount++;
5✔
1991
                                break;
5✔
1992
                        case JobState.Failed:
1993
                                batch.FailedCount++;
×
1994
                                break;
×
1995
                        case JobState.Cancelled:
1996
                                batch.CancelledCount++;
5✔
1997
                                break;
5✔
1998
                        case JobState.AwaitingContinuation:
1999
                        case JobState.AwaitingParameters:
2000
                        case JobState.Scheduled:
2001
                        case JobState.Pending:
2002
                        case JobState.Active:
2003
                                throw new ImmediateJobException($"Job '{job.Id}' is not terminal.");
×
2004
                        default:
2005
                                throw new ArgumentOutOfRangeException(nameof(job), job.State, "Unknown job state.");
×
2006
                }
2007

2008
                batch.ConcurrencyStamp = Guid.NewGuid();
5✔
2009
                if (batch.PendingCount != 0)
5✔
2010
                        return;
5✔
2011
                batch.State = GetTerminalBatchState(batch.FailedCount, batch.CancelledCount);
5✔
2012
                batch.CompletedAt = now;
5✔
2013
                parents.Enqueue((
5✔
2014
                        ContinuationParentKind.Batch,
5✔
2015
                        batch.Id,
5✔
2016
                        GetParentOutcome(batch.State)
5✔
2017
                ));
5✔
2018
        }
5✔
2019

2020
        private static async Task EvaluateInitialDependenciesAsync(
2021
                TContext context,
2022
                Dictionary<string, ImmediateJobEntity> jobs,
2023
                ImmediateJobContinuationEntity[] edges,
2024
                DateTimeOffset now,
2025
                CancellationToken cancellationToken
2026
        )
2027
        {
2028
                var externalJobIds = edges
5✔
2029
                        .Where(edge => edge.ParentKind == ContinuationParentKind.Job && !jobs.ContainsKey(edge.ParentId))
5✔
2030
                        .Select(static edge => edge.ParentId)
5✔
2031
                        .Distinct(StringComparer.Ordinal)
5✔
2032
                        .Order(StringComparer.Ordinal)
5✔
2033
                        .ToArray();
5✔
2034
                var externalBatchIds = edges
5✔
2035
                        .Where(static edge => edge.ParentKind == ContinuationParentKind.Batch)
5✔
2036
                        .Select(static edge => edge.ParentId)
5✔
2037
                        .Distinct(StringComparer.Ordinal)
5✔
2038
                        .Order(StringComparer.Ordinal)
5✔
2039
                        .ToArray();
5✔
2040
                var externalJobEntities = externalJobIds.Length == 0
5✔
2041
                        ? []
5✔
2042
                        : await context.Set<ImmediateJobEntity>()
5✔
2043
                                .Where(job => externalJobIds.Contains(job.Id))
5✔
2044
                                .OrderBy(static job => job.Id)
5✔
2045
                                .ToListAsync(cancellationToken)
5✔
2046
                                .ConfigureAwait(false);
5✔
2047
                var externalBatchEntities = externalBatchIds.Length == 0
5✔
2048
                        ? []
5✔
2049
                        : await context.Set<ImmediateJobBatchEntity>()
5✔
2050
                                .Where(batch => externalBatchIds.Contains(batch.Id))
5✔
2051
                                .OrderBy(static batch => batch.Id)
5✔
2052
                                .ToListAsync(cancellationToken)
5✔
2053
                                .ConfigureAwait(false);
5✔
2054
                var externalJobs = externalJobEntities.ToDictionary(job => job.Id, StringComparer.Ordinal);
5✔
2055
                var externalBatches = externalBatchEntities.ToDictionary(batch => batch.Id, StringComparer.Ordinal);
5✔
2056
                if (externalJobs.Count != externalJobIds.Length || externalBatches.Count != externalBatchIds.Length)
5✔
2057
                        throw new ImmediateJobException("A continuation parent does not exist.");
5✔
2058
                foreach (var parent in externalJobEntities.Where(parent => !IsTerminal(parent.State)))
5✔
2059
                        parent.ConcurrencyStamp = Guid.NewGuid();
5✔
2060
                foreach (var parent in externalBatchEntities.Where(parent => parent.State == BatchState.Executing))
5✔
2061
                        parent.ConcurrencyStamp = Guid.NewGuid();
4✔
2062

2063
                var incoming = edges.ToLookup(static edge => edge.ChildJobId, StringComparer.Ordinal);
5✔
2064
                var changed = true;
5✔
2065
                while (changed)
5✔
2066
                {
2067
                        changed = false;
5✔
2068
                        foreach (var job in jobs.Values)
5✔
2069
                        {
2070
                                var dependencies = incoming[job.Id];
5✔
2071
                                if (!dependencies.Any() || IsTerminal(job.State))
5✔
2072
                                        continue;
2073
                                var remaining = 0;
5✔
2074
                                var failedDependencies = 0;
5✔
2075
                                var requiresFailure = false;
5✔
2076
                                var violated = false;
5✔
2077
                                foreach (var edge in dependencies)
5✔
2078
                                {
2079
                                        var (terminal, parentSucceeded, parentFailed) = GetParentState(
5✔
2080
                                                edge,
5✔
2081
                                                jobs,
5✔
2082
                                                externalJobs,
5✔
2083
                                                externalBatches
5✔
2084
                                        );
5✔
2085
                                        requiresFailure |= edge.Trigger == ContinuationTrigger.Failure;
5✔
2086
                                        if (!terminal)
5✔
2087
                                        {
2088
                                                remaining++;
5✔
2089
                                                continue;
5✔
2090
                                        }
2091

2092
                                        edge.ParentOutcome = GetParentOutcome(parentSucceeded, parentFailed);
1✔
2093

2094
                                        if (parentFailed)
1✔
2095
                                                failedDependencies++;
×
2096
                                        if (edge.Trigger == ContinuationTrigger.Success && !parentSucceeded)
1✔
2097
                                                violated = true;
×
2098
                                }
2099

2100
                                job.FailedDependencies = failedDependencies;
5✔
2101
                                if (violated || (remaining == 0 && requiresFailure && failedDependencies == 0))
5✔
2102
                                {
2103
                                        job.State = JobState.Cancelled;
×
2104
                                        job.RemainingDependencies = 0;
×
2105
                                        job.CompletedAt = now;
×
2106
                                        changed = true;
×
2107
                                }
2108
                                else if (remaining == 0)
5✔
2109
                                {
2110
                                        job.State = job.DueAt <= now ? JobState.Pending : JobState.Scheduled;
1✔
2111
                                        job.RemainingDependencies = 0;
1✔
2112
                                }
2113
                                else
2114
                                {
2115
                                        job.State = JobState.AwaitingContinuation;
5✔
2116
                                        job.RemainingDependencies = remaining;
5✔
2117
                                }
2118
                        }
2119
                }
2120
        }
5✔
2121

2122
        private static (bool Terminal, bool Succeeded, bool Failed) GetParentState(
2123
                ImmediateJobContinuationEntity edge,
2124
                Dictionary<string, ImmediateJobEntity> jobs,
2125
                Dictionary<string, ImmediateJobEntity> externalJobs,
2126
                Dictionary<string, ImmediateJobBatchEntity> externalBatches
2127
        )
2128
        {
2129
                if (edge.ParentKind == ContinuationParentKind.Batch)
5✔
2130
                {
2131
                        var state = externalBatches[edge.ParentId].State;
5✔
2132
                        return (state != BatchState.Executing, state == BatchState.Succeeded, state == BatchState.Failed);
5✔
2133
                }
2134

2135
                var jobState = jobs.TryGetValue(edge.ParentId, out var job) ? job.State : externalJobs[edge.ParentId].State;
5✔
2136
                return (IsTerminal(jobState), jobState == JobState.Succeeded, jobState == JobState.Failed);
5✔
2137
        }
2138

2139
        private static void ThrowIfCyclic(
2140
                HashSet<string> jobIds,
2141
                IReadOnlyList<ImmediateJobContinuationEntity> edges
2142
        )
2143
        {
2144
                var indegree = jobIds.ToDictionary(static id => id, static _ => 0, StringComparer.Ordinal);
5✔
2145
                var children = new Dictionary<string, List<string>>(StringComparer.Ordinal);
5✔
2146
                foreach (var edge in edges.Where(edge =>
5✔
2147
                        edge.ParentKind == ContinuationParentKind.Job && jobIds.Contains(edge.ParentId)))
5✔
2148
                {
2149
                        indegree[edge.ChildJobId]++;
5✔
2150
                        if (!children.TryGetValue(edge.ParentId, out var values))
5✔
2151
                                children[edge.ParentId] = values = [];
5✔
2152
                        values.Add(edge.ChildJobId);
5✔
2153
                }
2154

2155
                var ready = new Queue<string>(indegree.Where(static pair => pair.Value == 0).Select(static pair => pair.Key));
5✔
2156
                var visited = 0;
5✔
2157
                while (ready.TryDequeue(out var parent))
5✔
2158
                {
2159
                        visited++;
5✔
2160
                        if (!children.TryGetValue(parent, out var values))
5✔
2161
                                continue;
2162
                        foreach (var child in values)
5✔
2163
                        {
2164
                                if (--indegree[child] == 0)
5✔
2165
                                        ready.Enqueue(child);
5✔
2166
                        }
2167
                }
2168

2169
                if (visited != jobIds.Count)
5✔
2170
                        throw new ImmediateJobException("The continuation graph contains a dependency cycle.");
×
2171
        }
5✔
2172

2173
        private static bool IsTerminal(JobState state) =>
2174
                state is JobState.Succeeded or JobState.Failed or JobState.Cancelled;
5✔
2175

2176
        private static ContinuationParentOutcome GetParentOutcome(JobState state) => state switch
5✔
2177
        {
5✔
2178
                JobState.Succeeded => ContinuationParentOutcome.Succeeded,
5✔
2179
                JobState.Failed => ContinuationParentOutcome.Failed,
5✔
2180
                JobState.AwaitingContinuation or
5✔
2181
                JobState.AwaitingParameters or
5✔
2182
                JobState.Scheduled or
5✔
2183
                JobState.Pending or
5✔
2184
                JobState.Active or
5✔
2185
                JobState.Cancelled => ContinuationParentOutcome.Other,
5✔
2186
                _ => throw new ArgumentOutOfRangeException(nameof(state), state, "Unknown job state."),
×
2187
        };
5✔
2188

2189
        private static ContinuationParentOutcome GetParentOutcome(BatchState state) => state switch
5✔
2190
        {
5✔
2191
                BatchState.Succeeded => ContinuationParentOutcome.Succeeded,
5✔
2192
                BatchState.Failed => ContinuationParentOutcome.Failed,
×
2193
                BatchState.Executing or BatchState.Cancelled => ContinuationParentOutcome.Other,
5✔
2194
                _ => throw new ArgumentOutOfRangeException(nameof(state), state, "Unknown batch state."),
×
2195
        };
5✔
2196

2197
        private static ContinuationParentOutcome GetParentOutcome(bool succeeded, bool failed) =>
2198
                (succeeded, failed) switch
1✔
2199
                {
1✔
2200
                        (true, _) => ContinuationParentOutcome.Succeeded,
1✔
2201
                        (_, true) => ContinuationParentOutcome.Failed,
×
2202
                        _ => ContinuationParentOutcome.Other,
×
2203
                };
1✔
2204

2205
        private static BatchState GetTerminalBatchState(int failed, int cancelled) =>
2206
                failed != 0 ? BatchState.Failed : cancelled != 0 ? BatchState.Cancelled : BatchState.Succeeded;
5✔
2207

2208
        private async ValueTask MutateRecurringAsync(string name, Action<ImmediateRecurringJobEntity> mutate, CancellationToken cancellationToken)
2209
        {
2210
                ArgumentException.ThrowIfNullOrWhiteSpace(name);
1✔
2211
                await using var context = await contextFactory.CreateDbContextAsync(cancellationToken).ConfigureAwait(false);
1✔
2212
                var schedule = await context.Set<ImmediateRecurringJobEntity>().FindAsync([name], cancellationToken).ConfigureAwait(false);
1✔
2213
                if (schedule is null)
1✔
2214
                        throw new KeyNotFoundException($"Recurring schedule '{name}' was not found.");
1✔
2215
                mutate(schedule);
×
2216
                schedule.ConcurrencyStamp = Guid.NewGuid();
×
2217
                _ = await context.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
×
2218
        }
×
2219

2220
        private static Task<int> UpdateRecurringAsync(
2221
                TContext context,
2222
                RecurringJobSchedule schedule,
2223
                CancellationToken cancellationToken
2224
        )
2225
        {
2226
                var concurrencyStamp = Guid.NewGuid();
5✔
2227
                return context.Set<ImmediateRecurringJobEntity>()
5✔
2228
                        .Where(entity => entity.Name == schedule.Name && (schedule.IsCodeDefined || !entity.IsCodeDefined))
5✔
2229
                        .ExecuteUpdateAsync(setters => setters
5✔
2230
                                .SetProperty(entity => entity.JobName, schedule.JobName)
5✔
2231
                                .SetProperty(entity => entity.Cron, schedule.Cron)
5✔
2232
                                .SetProperty(entity => entity.TimeZone, schedule.TimeZone)
5✔
2233
                                .SetProperty(entity => entity.IsCodeDefined, schedule.IsCodeDefined)
5✔
2234
                                .SetProperty(entity => entity.NextRunAt, schedule.NextRunAt)
5✔
2235
                                .SetProperty(entity => entity.ConcurrencyStamp, concurrencyStamp),
5✔
2236
                                cancellationToken);
5✔
2237
        }
2238

2239
        private static async Task ThrowIfReplacingCodeDefinedScheduleAsync(
2240
                TContext context,
2241
                RecurringJobSchedule schedule,
2242
                CancellationToken cancellationToken
2243
        )
2244
        {
2245
                if (!schedule.IsCodeDefined && await context.Set<ImmediateRecurringJobEntity>()
5✔
2246
                        .AnyAsync(entity => entity.Name == schedule.Name && entity.IsCodeDefined, cancellationToken)
5✔
2247
                        .ConfigureAwait(false))
5✔
2248
                {
2249
                        throw new ImmediateJobException("Code-defined recurring schedules cannot be replaced by dynamic schedules.");
4✔
2250
                }
2251
        }
5✔
2252

2253
        private static ImmediateJobEntity ToEntity(JobRecord job) => new()
5✔
2254
        {
5✔
2255
                Id = job.Id,
5✔
2256
                QueueName = job.QueueName,
5✔
2257
                JobName = job.JobName,
5✔
2258
                GroupId = job.GroupId,
5✔
2259
                Payload = job.Payload,
5✔
2260
                Context = job.Context,
5✔
2261
                State = job.State,
5✔
2262
                DueAt = job.DueAt,
5✔
2263
                CreatedAt = job.CreatedAt,
5✔
2264
                Attempt = job.Attempt,
5✔
2265
                WorkerId = job.WorkerId,
5✔
2266
                LeaseExpiresAt = job.LeaseExpiresAt,
5✔
2267
                LastError = job.LastError,
5✔
2268
                CompletedAt = job.CompletedAt,
5✔
2269
                RecurringKey = job.RecurringKey,
5✔
2270
                TraceParent = job.TraceParent,
5✔
2271
                TraceState = job.TraceState,
5✔
2272
                ExecutionTraceId = job.ExecutionTraceId,
5✔
2273
                ExecutionSpanId = job.ExecutionSpanId,
5✔
2274
                ExecutionStartedAt = job.ExecutionStartedAt,
5✔
2275
                BatchId = job.BatchId,
5✔
2276
                RemainingDependencies = job.RemainingDependencies,
5✔
2277
                FailedDependencies = job.FailedDependencies,
5✔
2278
                ConcurrencyStamp = Guid.NewGuid(),
5✔
2279
        };
5✔
2280

2281
        private static ImmediateJobContinuationEntity ToEntity(JobContinuationEdge edge)
2282
        {
2283
                var hasJobParent = edge.ParentJobId is not null;
5✔
2284
                var hasBatchParent = edge.ParentBatchId is not null;
5✔
2285
                if (hasJobParent == hasBatchParent)
5✔
2286
                        throw new ImmediateJobException("A continuation edge must identify exactly one parent job or batch.");
×
2287
                return new()
5✔
2288
                {
5✔
2289
                        ChildJobId = edge.ChildJobId,
5✔
2290
                        ParentKind = hasJobParent ? ContinuationParentKind.Job : ContinuationParentKind.Batch,
5✔
2291
                        ParentId = edge.ParentJobId ?? edge.ParentBatchId!,
5✔
2292
                        Trigger = edge.Trigger,
5✔
2293
                };
5✔
2294
        }
2295

2296
        private static ImmediateJobEntity Copy(ImmediateJobEntity job) => new()
5✔
2297
        {
5✔
2298
                Id = job.Id,
5✔
2299
                QueueName = job.QueueName,
5✔
2300
                JobName = job.JobName,
5✔
2301
                GroupId = job.GroupId,
5✔
2302
                Payload = job.Payload,
5✔
2303
                Context = job.Context,
5✔
2304
                State = job.State,
5✔
2305
                DueAt = job.DueAt,
5✔
2306
                CreatedAt = job.CreatedAt,
5✔
2307
                Attempt = job.Attempt,
5✔
2308
                WorkerId = job.WorkerId,
5✔
2309
                LeaseExpiresAt = job.LeaseExpiresAt,
5✔
2310
                LastError = job.LastError,
5✔
2311
                CompletedAt = job.CompletedAt,
5✔
2312
                RecurringKey = job.RecurringKey,
5✔
2313
                TraceParent = job.TraceParent,
5✔
2314
                TraceState = job.TraceState,
5✔
2315
                ExecutionTraceId = job.ExecutionTraceId,
5✔
2316
                ExecutionSpanId = job.ExecutionSpanId,
5✔
2317
                ExecutionStartedAt = job.ExecutionStartedAt,
5✔
2318
                BatchId = job.BatchId,
5✔
2319
                RemainingDependencies = job.RemainingDependencies,
5✔
2320
                FailedDependencies = job.FailedDependencies,
5✔
2321
                ConcurrencyStamp = job.ConcurrencyStamp,
5✔
2322
        };
5✔
2323

2324
        private static JobRecord ToRecord(ImmediateJobEntity job) => new()
5✔
2325
        {
5✔
2326
                Id = job.Id,
5✔
2327
                QueueName = job.QueueName,
5✔
2328
                JobName = job.JobName,
5✔
2329
                GroupId = job.GroupId,
5✔
2330
                Payload = job.Payload,
5✔
2331
                Context = job.Context,
5✔
2332
                State = job.State,
5✔
2333
                DueAt = job.DueAt,
5✔
2334
                CreatedAt = job.CreatedAt,
5✔
2335
                Attempt = job.Attempt,
5✔
2336
                WorkerId = job.WorkerId,
5✔
2337
                LeaseExpiresAt = job.LeaseExpiresAt,
5✔
2338
                LastError = job.LastError,
5✔
2339
                CompletedAt = job.CompletedAt,
5✔
2340
                RecurringKey = job.RecurringKey,
5✔
2341
                TraceParent = job.TraceParent,
5✔
2342
                TraceState = job.TraceState,
5✔
2343
                ExecutionTraceId = job.ExecutionTraceId,
5✔
2344
                ExecutionSpanId = job.ExecutionSpanId,
5✔
2345
                ExecutionStartedAt = job.ExecutionStartedAt,
5✔
2346
                BatchId = job.BatchId,
5✔
2347
                RemainingDependencies = job.RemainingDependencies,
5✔
2348
                FailedDependencies = job.FailedDependencies,
5✔
2349
        };
5✔
2350

2351
        private static BatchStatus ToStatus(ImmediateJobBatchEntity batch) => new(
5✔
2352
                batch.Id,
5✔
2353
                batch.State,
5✔
2354
                batch.TotalJobs,
5✔
2355
                batch.SucceededCount,
5✔
2356
                batch.FailedCount,
5✔
2357
                batch.CancelledCount,
5✔
2358
                batch.PendingCount,
5✔
2359
                batch.CreatedAt,
5✔
2360
                batch.StartedAt,
5✔
2361
                batch.CompletedAt,
5✔
2362
                BatchStatus.CalculateFractionSettled(batch.TotalJobs, batch.PendingCount)
5✔
2363
        );
5✔
2364

2365
        private static JobContinuationEdge ToContinuationEdge(ImmediateJobContinuationEntity edge) => new()
5✔
2366
        {
5✔
2367
                ChildJobId = edge.ChildJobId,
5✔
2368
                ParentJobId = edge.ParentKind == ContinuationParentKind.Job ? edge.ParentId : null,
5✔
2369
                ParentBatchId = edge.ParentKind == ContinuationParentKind.Batch ? edge.ParentId : null,
5✔
2370
                Trigger = edge.Trigger,
5✔
2371
        };
5✔
2372

2373
        private static BatchGraphEdge ToGraphEdge(ImmediateJobContinuationEntity edge) => new(
5✔
2374
                edge.ChildJobId,
5✔
2375
                edge.ParentKind == ContinuationParentKind.Job ? edge.ParentId : null,
5✔
2376
                edge.ParentKind == ContinuationParentKind.Batch ? edge.ParentId : null,
5✔
2377
                edge.Trigger
5✔
2378
        );
5✔
2379

2380
        private static ImmediateRecurringJobEntity ToEntity(RecurringJobSchedule schedule) => new()
5✔
2381
        {
5✔
2382
                Name = schedule.Name,
5✔
2383
                JobName = schedule.JobName,
5✔
2384
                Cron = schedule.Cron,
5✔
2385
                TimeZone = schedule.TimeZone,
5✔
2386
                IsCodeDefined = schedule.IsCodeDefined,
5✔
2387
                IsPaused = schedule.IsPaused,
5✔
2388
                NextRunAt = schedule.NextRunAt,
5✔
2389
                LastRunAt = schedule.LastRunAt,
5✔
2390
                ConcurrencyStamp = Guid.NewGuid(),
5✔
2391
        };
5✔
2392
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc