| | | 1 | | // SPDX-FileCopyrightText: 2026 Alper Çelik <alper@alper-celik.dev> |
| | | 2 | | // |
| | | 3 | | // SPDX-License-Identifier: AGPL-3.0-or-later |
| | | 4 | | |
| | | 5 | | using System.Text.Json; |
| | | 6 | | using Api.Jobs.Models; |
| | | 7 | | using NodaTime; |
| | | 8 | | |
| | | 9 | | namespace Api.Jobs; |
| | | 10 | | |
| | | 11 | | /// TODO: should be scoped and use scopes transaction |
| | | 12 | | public interface IJobsInterfae |
| | | 13 | | { |
| | | 14 | | /// <param name="timeout"> if timeout is zero returns current jobs or none if there |
| | | 15 | | /// isn't </param> |
| | | 16 | | public Task<JobData[]> PeekPendingJobs( |
| | | 17 | | uint timeout, |
| | | 18 | | uint? maxJobs, |
| | | 19 | | Guid? leaserId, |
| | | 20 | | string[]? jobTypes, |
| | | 21 | | CancellationToken ct |
| | | 22 | | ); |
| | | 23 | | |
| | | 24 | | /// <param name="timeout"> if timeout is zero returns current jobs or none if there |
| | | 25 | | /// isn't </param> |
| | | 26 | | public Task<JobData[]> LeasePendingJobs( |
| | | 27 | | uint timeout, |
| | | 28 | | uint? maxJobs, |
| | | 29 | | Guid? leaserId, |
| | | 30 | | string[]? jobTypes, |
| | | 31 | | CancellationToken ct |
| | | 32 | | ); |
| | | 33 | | |
| | | 34 | | public Task<IPeekJobResult[]> PeekJobs(Guid[] jobIds); |
| | | 35 | | |
| | | 36 | | public Task<ILeaseJobResult[]> LeaseJobs(Guid[] jobIds, CancellationToken ct); |
| | | 37 | | |
| | | 38 | | public Task<IExtendLeaseResult[]> ExtendJobLeases(Guid[] jobIds); |
| | | 39 | | |
| | | 40 | | public Task<JobMarkAsCompleteResult[]> MarkJobsAsComplete( |
| | | 41 | | JobCompletionData[] jobCompletions |
| | | 42 | | ); |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// waits for completed jobs in specified <paramref name="jobTypes"/> or all jobs if it is |
| | | 46 | | /// null |
| | | 47 | | /// |
| | | 48 | | /// waits at max <paramref name="timeout"/> or indefinitely if <paramref name="timeout"/> is 0 |
| | | 49 | | ///</summary> |
| | | 50 | | /// |
| | | 51 | | /// <param name="jobIds"> limits result to specified ids does not filter by ids if null</param> |
| | | 52 | | /// |
| | | 53 | | /// <returns> returns null when <paramref name="timeout"/> is active and passes</returns> |
| | | 54 | | public Task<WaitForCompletedJobsResult?> WaitForCompletedJobsOrForSeconds( |
| | | 55 | | uint timeout, |
| | | 56 | | Instant after, |
| | | 57 | | uint? maxJobs, |
| | | 58 | | string[]? jobTypes, |
| | | 59 | | CancellationToken ct, |
| | | 60 | | Guid[]? jobIds = null |
| | | 61 | | ); |
| | | 62 | | } |
| | | 63 | | |
| | | 64 | | public record WaitForCompletedJobsResult(Guid[] JobIds, Instant LatestCompletedJobTime); |
| | | 65 | | |
| | | 66 | | public record JobCompletionData(Guid JobId, JsonDocument? CompletionData); |
| | | 67 | | |
| | | 68 | | #region PeekJobResult |
| | | 69 | | public interface IPeekJobResult; |
| | | 70 | | |
| | | 71 | | public record Success(JobData Job) : IPeekJobResult; |
| | | 72 | | |
| | | 73 | | public record JobDoesNotExists : IPeekJobResult; |
| | | 74 | | |
| | | 75 | | #endregion |
| | | 76 | | |
| | | 77 | | #region LeaseJobResult |
| | | 78 | | // TODO: switch to discriminated unions or closed interfaces |
| | | 79 | | public interface ILeaseJobResult; |
| | | 80 | | |
| | | 81 | | public partial record NewLeaseEndTime : ILeaseJobResult; |
| | | 82 | | |
| | 0 | 83 | | public record LeasingFailed(LeaseExtendFailReason Reason) : ILeaseJobResult; |
| | | 84 | | |
| | | 85 | | public enum LeaseFailReason |
| | | 86 | | { |
| | | 87 | | CurrentlyLeased, |
| | | 88 | | AlreadyCompleted, |
| | | 89 | | JobDoesNotExist, |
| | | 90 | | } |
| | | 91 | | |
| | | 92 | | #endregion |
| | | 93 | | |
| | | 94 | | |
| | | 95 | | #region IExtendLeaseResult |
| | | 96 | | |
| | | 97 | | // TODO: switch to discriminated unions or closed interfaces |
| | | 98 | | public interface IExtendLeaseResult; |
| | | 99 | | |
| | | 100 | | public partial record NewLeaseEndTime(Instant LeaseEnd) : IExtendLeaseResult; |
| | | 101 | | |
| | | 102 | | public record LeaseExtendFailed(LeaseExtendFailReason Reason) : IExtendLeaseResult; |
| | | 103 | | |
| | | 104 | | public enum LeaseExtendFailReason |
| | | 105 | | { |
| | | 106 | | NotLeasedCurrently, |
| | | 107 | | LeasedBySomeoneElse, |
| | | 108 | | LeaseExtensionLimitReached, |
| | | 109 | | JobDoesNotExist, |
| | | 110 | | } |
| | | 111 | | #endregion |
| | | 112 | | |
| | | 113 | | public enum JobMarkAsCompleteResult |
| | | 114 | | { |
| | | 115 | | Completed, |
| | | 116 | | LeasedBySomeoneElse, |
| | | 117 | | NotLeasedCurrently, |
| | | 118 | | AlreadyCompleted, |
| | | 119 | | JobDoesNotExist, |
| | | 120 | | } |
| | | 121 | | |
| | | 122 | | public enum JobStatus |
| | | 123 | | { |
| | | 124 | | Free, |
| | | 125 | | Leased, |
| | | 126 | | Completed, |
| | | 127 | | MaxRetryReached, |
| | | 128 | | } |