summaryrefslogtreecommitdiff
path: root/hooks/queuer.ts
blob: 146180921152df8d66fc4c89d28e38c2b81b942a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import type { IEither, ITraceable, ITraceableLogger } from "@liz-ci/utils";
import type { Job } from "@liz-ci/model";

type QueuePosition = string;
export class QueueError extends Error {}
export interface IJobQueuer<TJob> {
  queue: <L extends ITraceableLogger<L>>(
    job: ITraceable<TJob, L>,
  ) => Promise<IEither<QueueError, QueuePosition>>;
}

export class LaminarJobQueuer implements IJobQueuer<Job> {
  constructor(
    private readonly queuePositionPrefix: string,
  ) {}

  public async queue({ item: job, logger }: Traceable<Job>) {
    const laminarCommand = [
      "laminarc",
      "queue",
      job.type,
      ...Object.entries(job.arguments).map(([key, val]) => `"${key}"="${val}"`),
    ];

    logger.log(
      `im so excited to see how this queue job will end!! (>ᴗ<)`,
      laminarCommand,
    );

    return (await getStdout(laminarCommand)).mapBoth(
      (e) => {
        const err = `we bwoke oh noesss D:`;
        logger.error(err, e);
        return Either.left<QueueError, QueuePosition>(e);
      },
      (stdout) => {
        logger.log(stdout);

        const [jobName, jobId] = stdout.split(":");
        const jobUrl = `${this.queuePositionPrefix}/jobs/${jobName}/${jobId}`;

        logger.log(`all queued up and weady to go~ (˘ω˘) => ${jobUrl}\n`);
        return Either.right<QueueError, QueuePosition>(jobUrl);
      },
    );
  }
}