| | | 1 | | // SPDX-FileCopyrightText: 2026 Alper Çelik <[email protected]> |
| | | 2 | | // |
| | | 3 | | // SPDX-License-Identifier: AGPL-3.0-or-later |
| | | 4 | | |
| | | 5 | | using System.Diagnostics; |
| | | 6 | | using Api.Auth.Handlers; |
| | | 7 | | using Api.Auth.Utils; |
| | | 8 | | using Api.Database; |
| | | 9 | | using Api.Database.Utils; |
| | | 10 | | using Api.Jobs.DTOs; |
| | | 11 | | using Api.Utils; |
| | | 12 | | using FluentValidation; |
| | | 13 | | using HotChocolate.Authorization; |
| | | 14 | | using Microsoft.EntityFrameworkCore; |
| | | 15 | | |
| | | 16 | | namespace Api.Jobs.Mutations; |
| | | 17 | | |
| | | 18 | | public record LeasePendingJobsInput(string[] JobTypes, int MaxJobs); |
| | | 19 | | |
| | 0 | 20 | | public record LeasePendingJobsPayload(ILeasedJob[] Jobs); |
| | | 21 | | |
| | | 22 | | [MutationType] |
| | | 23 | | public static partial class LeaseJobMutations |
| | | 24 | | { |
| | | 25 | | [Authorize(PermissionPolicyProvider.RemoteServicePolicyName)] |
| | | 26 | | // TODO: remove once the Jobs slice is wired: there is no IJobsInterfae implementation and no |
| | | 27 | | // object type implementing ILeasedJob yet, which breaks schema validation and the generated |
| | | 28 | | // ZeroQL client. Re-add the [InterfaceType] attributes and validate the input explicitly |
| | | 29 | | // (validator.ValidateOrThrowInputAsync) at that point. |
| | | 30 | | [GraphQLIgnore] |
| | | 31 | | public static async Task<LeasePendingJobsPayload> LeasePendingJobs( |
| | | 32 | | [Service] ICurrentServiceId idGetter, |
| | | 33 | | [Service] IEFTransactionDIAccessorService tx, |
| | | 34 | | [Service] IJobsInterfae jobsInterfae, |
| | | 35 | | [Service] JobDTOBuilder j, |
| | | 36 | | LeasePendingJobsInput input, |
| | | 37 | | CancellationToken ct |
| | | 38 | | ) |
| | | 39 | | { |
| | | 40 | | await tx.BeginOrGetTransactionAsync(); |
| | | 41 | | var jobDatas = await jobsInterfae.LeasePendingJobs( |
| | | 42 | | 0, |
| | | 43 | | (uint)input.MaxJobs, |
| | | 44 | | idGetter.Id ?? throw new ArgumentNullException(nameof(idGetter)), |
| | | 45 | | input.JobTypes, |
| | | 46 | | ct |
| | | 47 | | ); |
| | | 48 | | var jobs = jobDatas |
| | | 49 | | .Select(jd => |
| | | 50 | | ( |
| | | 51 | | j.CreateFrom(jd.JobType, jd) |
| | | 52 | | ?? throw new UnreachableException( |
| | | 53 | | "There must not exist a job with a unregistered type" |
| | | 54 | | ) |
| | | 55 | | ).AsLeased() |
| | | 56 | | ) |
| | | 57 | | .ToArray(); |
| | | 58 | | |
| | | 59 | | await tx.SaveAndCommitTX(ct); |
| | | 60 | | |
| | | 61 | | return new LeasePendingJobsPayload(jobs); |
| | | 62 | | } |
| | | 63 | | } |
| | | 64 | | |
| | | 65 | | public class LeasePendingJobsInputValidator : AbstractValidator<LeasePendingJobsInput> |
| | | 66 | | { |
| | | 67 | | public LeasePendingJobsInputValidator( |
| | | 68 | | PGContext db, |
| | | 69 | | IEFTransactionDIAccessorService tx, |
| | | 70 | | ICurrentServiceId idGetter |
| | | 71 | | ) |
| | | 72 | | { |
| | | 73 | | RuleFor(i => i).BeginTransaction(tx); |
| | | 74 | | |
| | | 75 | | RuleFor(i => i.MaxJobs).GreaterThan(0).LessThan(128); |
| | | 76 | | |
| | | 77 | | RuleFor(i => i.JobTypes) |
| | | 78 | | .MustAsync( |
| | | 79 | | async (_, jobTypes, ctx, ct) => |
| | | 80 | | await db |
| | | 81 | | .RemoteServices.Where(s => s.Id == idGetter.Id) |
| | | 82 | | .Select(s => s.AllowedJobs) |
| | | 83 | | .Select(allowedTypes => |
| | | 84 | | jobTypes.All(jt => allowedTypes.Contains(jt)) |
| | | 85 | | ) |
| | | 86 | | .FirstOrDefaultAsync(ct) |
| | | 87 | | ); |
| | | 88 | | } |
| | | 89 | | } |