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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
|
import {
getStdout,
type Mapper,
memoize,
Either,
ErrorSource,
type IActivity,
type IEither,
type ITraceable,
jsonModel,
JsonResponse,
LogLevel,
Metric,
PenguenoError,
type PenguenoRequest,
type ServerTrace,
TraceUtil,
validateExecutionEntries,
} from "@emprespresso/pengueno";
import { isJob, type Job } from "@emprespresso/ci_model";
// -- <job.hook> --
const wellFormedJobMetric = Metric.fromName("Job.WellFormed");
const jobJsonTransformer = (
j: ITraceable<unknown, ServerTrace>,
): IEither<PenguenoError, Job> =>
j
.bimap(TraceUtil.withMetricTrace(wellFormedJobMetric))
.map((tJson) => {
if (!isJob(tJson) || !validateExecutionEntries(tJson)) {
const err = "seems like a pwetty mawfomed job (-.-)";
tJson.trace.addTrace(LogLevel.WARN).trace(err);
return Either.left<PenguenoError, Job>(new PenguenoError(err, 400));
}
return Either.right<PenguenoError, Job>(tJson);
})
.peek((tJob) =>
tJob.trace.trace(
tJob
.get()
.fold(({ isLeft }) =>
isLeft ? wellFormedJobMetric.failure : wellFormedJobMetric.success,
),
),
)
.get();
export interface IJobHookActivity {
processHook: IActivity;
}
const jobHookRequestMetric = Metric.fromName("JobHook.process");
export class JobHookActivityImpl implements IJobHookActivity {
constructor(
private readonly queuer: IJobQueuer<ITraceable<Job, ServerTrace>>,
) {}
private trace(r: ITraceable<PenguenoRequest, ServerTrace>) {
return r
.bimap(TraceUtil.withClassTrace(this))
.bimap(TraceUtil.withMetricTrace(jobHookRequestMetric));
}
public processHook(r: ITraceable<PenguenoRequest, ServerTrace>) {
return this.trace(r)
.map(jsonModel(jobJsonTransformer))
.map(async (tEitherJobJson) => {
const eitherJob = await tEitherJobJson.get();
return eitherJob.flatMapAsync(async (job) => {
const eitherQueued = await tEitherJobJson
.move(job)
.map((job) => this.queuer.queue(job))
.get();
return eitherQueued.mapLeft((e) => new PenguenoError(e.message, 500));
});
})
.peek(
TraceUtil.promiseify((tJob) =>
tJob.get().fold(({ isRight, value }) => {
if (isRight) {
tJob.trace.trace(jobHookRequestMetric.success);
tJob.trace.trace(`all queued up and weady to go :D !! ${value}`);
return;
}
tJob.trace.trace(
value.source === ErrorSource.SYSTEM
? jobHookRequestMetric.failure
: jobHookRequestMetric.warn,
);
tJob.trace.addTrace(value.source).trace(`${value}`);
}),
),
)
.map(
TraceUtil.promiseify(
(tEitherQueuedJob) =>
new JsonResponse(r, tEitherQueuedJob.get(), {
status: tEitherQueuedJob
.get()
.fold(({ isRight, value }) => (isRight ? 200 : value.status)),
}),
),
)
.get();
}
}
// -- </job.hook> --
// -- <job.queuer> --
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)),
);
public queue(j: ITraceable<Job, ServerTrace>) {
const { type: jobType } = j.get();
const trace = LaminarJobQueuer.GetJobTypeTrace(jobType);
const metric = LaminarJobQueuer.JobTypeMetrics(trace);
return j
.bimap(TraceUtil.withTrace(trace))
.bimap(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(getStdout)
.peek(
TraceUtil.promiseify((q) =>
q.trace.trace(
q
.get()
.fold(({ isLeft }) => (isLeft ? metric.failure : metric.success)),
),
),
)
.map(
TraceUtil.promiseify((q) =>
q.get().fold(({ isLeft, value }) => {
if (isLeft) {
q.trace.addTrace(LogLevel.ERROR).trace(value.toString());
return Either.left<Error, string>(value);
}
q.trace.addTrace(LogLevel.DEBUG).trace(`stdout ${value}`);
const [jobName, jobId] = value.split(":");
const jobUrl = `${this.queuePositionPrefix}/jobs/${jobName}/${jobId}`;
q.trace.trace(`all queued up and weady to go~ (˘ω˘) => ${jobUrl}`);
return Either.right<Error, string>(jobUrl);
}),
),
)
.get();
}
}
// -- </job.queuer> --
|