From ecd5f5d13936dd097b12766cb95a568ad5b073a9 Mon Sep 17 00:00:00 2001 From: Melodi <62381002+Melodi17@users.noreply.github.com> Date: Tue, 27 Oct 2026 23:04:59 +1000 Subject: feat: lots of stuff --- .../Controllers/Api/DispatchController.cs | 69 ++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 src/marlin.web/Controllers/Api/DispatchController.cs (limited to 'src/marlin.web/Controllers/Api/DispatchController.cs') diff --git a/src/marlin.web/Controllers/Api/DispatchController.cs b/src/marlin.web/Controllers/Api/DispatchController.cs new file mode 100644 index 0000000..076c4dd --- /dev/null +++ b/src/marlin.web/Controllers/Api/DispatchController.cs @@ -0,0 +1,69 @@ +namespace marlin.web.Controllers.Api; + +using marlin.core.models; +using marlin.core.util; +using marlin.web.Data; +using marlin.web.Models; +using marlin.web.Util; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; + +[Route("api/dispatch")] +[ApiController] +[Authorize(Policy = "Agent")] +public class DispatchController(ApplicationDbContext dbContext) : ControllerBase +{ + [HttpPost("handshake")] + public async Task> Handshake(AgentStatus status, CancellationToken ct) + { + var agentId = this.User.RequireAgentId(); + + var agent = await dbContext.Agents + .FirstAsync(x => x.Id == agentId, ct); + + foreach (var capability in status.CapabilitySet.Items) + agent.Capabilities.Items[capability.Key] = capability.Value; + + await dbContext.SaveChangesAsync(ct); + + return new AgentConfiguration( + Name: agent.Name, + MaxConcurrentJobs: agent.MaxConcurrentJobs + ); + } + + [HttpGet("queue")] + public async Task> Queue(CancellationToken ct) + { + var agentId = this.User.RequireAgentId(); + + var nextJob = await dbContext + .Jobs + .Include(x => x.Source) + .Include(x => x.Branch) + .Include(x => x.Commit) + .Where(x => x.Status == JobStatus.Pending && x.AgentId == null) + .OrderBy(x => x.CreatedAt) + .FirstOrDefaultAsync(ct); + + if (nextJob == null) + return (DispatchJob?)null; + + string claimToken = SecureTokenGenerator.CreateClaimToken(); + nextJob.ClaimToken = claimToken; + nextJob.AgentId = agentId; + + await dbContext.SaveChangesAsync(ct); + + return new DispatchJob( + Id: nextJob.Id, + Token: claimToken, + Vcs: nextJob.Source.UpstreamUrl, + Ref: nextJob.Branch.Name, + Commit: nextJob.Commit.Hash, + Recipe: nextJob.Recipe + ); + } +} + -- cgit v1.3.1