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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
import {
getStdout,
type Mapper,
memoize,
type IEither,
type ITraceable,
LogLevel,
Metric,
type ServerTrace,
TraceUtil,
PenguenoError,
} from '@emprespresso/pengueno';
import { type Job } from '@emprespresso/ci_model';
type QueuePosition = string;
export class QueueError extends Error {}
export interface IJobQueuer<TJob> {
queue: Mapper<TJob, Promise<IEither<QueueError, QueuePosition>>>;
}
export class LaminarJobQueuer implements IJobQueuer<ITraceable<Job, ServerTrace>> {
constructor(private readonly queuePositionPrefix: string) {}
private static GetJobTypeTrace = (jobType: string) => `LaminarJobQueue.Queue.${jobType}`;
private static JobTypeMetrics = memoize((jobType: string) =>
Metric.fromName(LaminarJobQueuer.GetJobTypeTrace(jobType)).asResult(),
);
public queue(j: ITraceable<Job, ServerTrace>) {
const { type: jobType } = j.get();
const trace = LaminarJobQueuer.GetJobTypeTrace(jobType);
const metric = LaminarJobQueuer.JobTypeMetrics(jobType);
return j
.flatMap(TraceUtil.withTrace(trace))
.flatMap(TraceUtil.withMetricTrace(metric))
.map((j) => {
const { type: jobType, arguments: args } = j.get();
const laminarCommand = [
'laminarc',
'queue',
jobType,
...Object.entries(args).map(([key, val]) => `"${key}"="${val}"`),
];
return laminarCommand;
})
.peek((c) =>
c.trace.trace(`im so excited to see how this queue job will end!! (>ᴗ<): ${c.get().toString()}`),
)
.map((c) => getStdout(c))
.peek(TraceUtil.promiseify(TraceUtil.traceResultingEither(metric)))
.map(
TraceUtil.promiseify((q) =>
q.get().mapBoth(
(err) => {
q.trace.traceScope(LogLevel.ERROR).trace(err);
return err;
},
(ok) => {
q.trace.traceScope(LogLevel.DEBUG).trace(`stdout ${ok}`);
const [jobName, jobId] = ok.split(':');
const jobUrl = `${this.queuePositionPrefix}/jobs/${jobName}/${jobId}`;
q.trace.trace(`all queued up and weady to go~ (˘ω˘) => ${jobUrl}`);
return jobUrl;
},
),
),
)
.get();
}
}
|