@angular-devkit/architect 0.1500.0-next.4 → 0.1500.0-next.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/node/BUILD.bazel +30 -0
- package/node/index.d.ts +2 -0
- package/node/index.js +15 -0
- package/node/jobs/job-registry.d.ts +20 -0
- package/node/jobs/job-registry.js +59 -0
- package/package.json +3 -3
- package/src/api.d.ts +3 -7
- package/src/architect.d.ts +4 -4
- package/src/architect.js +10 -11
- package/src/create-builder.js +4 -5
- package/src/index.d.ts +2 -0
- package/src/index.js +15 -1
- package/src/internal.d.ts +5 -4
- package/src/jobs/README.md +508 -0
- package/src/jobs/api.d.ts +332 -0
- package/src/jobs/api.js +73 -0
- package/src/jobs/architecture.md +260 -0
- package/src/jobs/create-job-handler.d.ts +46 -0
- package/src/jobs/create-job-handler.js +134 -0
- package/src/jobs/dispatcher.d.ts +31 -0
- package/src/jobs/dispatcher.js +50 -0
- package/src/jobs/exception.d.ts +15 -0
- package/src/jobs/exception.js +23 -0
- package/src/jobs/fallback-registry.d.ts +19 -0
- package/src/jobs/fallback-registry.js +27 -0
- package/src/jobs/index.d.ts +15 -0
- package/src/jobs/index.js +31 -0
- package/src/jobs/simple-registry.d.ts +44 -0
- package/src/jobs/simple-registry.js +77 -0
- package/src/jobs/simple-scheduler.d.ts +77 -0
- package/src/jobs/simple-scheduler.js +382 -0
- package/src/jobs/strategy.d.ts +28 -0
- package/src/jobs/strategy.js +99 -0
- package/src/jobs/types.d.ts +15 -0
- package/src/jobs/types.js +9 -0
- package/src/schedule-by-name.d.ts +4 -5
- package/src/schedule-by-name.js +3 -10
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright Google LLC All Rights Reserved.
|
|
4
|
+
*
|
|
5
|
+
* Use of this source code is governed by an MIT-style license that can be
|
|
6
|
+
* found in the LICENSE file at https://angular.io/license
|
|
7
|
+
*/
|
|
8
|
+
import { JsonObject, JsonValue, schema } from '@angular-devkit/core';
|
|
9
|
+
import { Observable, Observer } from 'rxjs';
|
|
10
|
+
import { DeepReadonly } from './types';
|
|
11
|
+
/**
|
|
12
|
+
* A job name is just a string (needs to be serializable).
|
|
13
|
+
*/
|
|
14
|
+
export declare type JobName = string;
|
|
15
|
+
/**
|
|
16
|
+
* The job handler function, which is a method that's executed for the job.
|
|
17
|
+
*/
|
|
18
|
+
export interface JobHandler<ArgT extends JsonValue, InputT extends JsonValue, OutputT extends JsonValue> {
|
|
19
|
+
(argument: ArgT, context: JobHandlerContext<ArgT, InputT, OutputT>): Observable<JobOutboundMessage<OutputT>>;
|
|
20
|
+
jobDescription: Partial<JobDescription>;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* The context in which the job is run.
|
|
24
|
+
*/
|
|
25
|
+
export interface JobHandlerContext<MinimumArgumentValueT extends JsonValue = JsonValue, MinimumInputValueT extends JsonValue = JsonValue, MinimumOutputValueT extends JsonValue = JsonValue> {
|
|
26
|
+
readonly description: JobDescription;
|
|
27
|
+
readonly scheduler: Scheduler<JsonValue, JsonValue, JsonValue>;
|
|
28
|
+
readonly dependencies: Job<JsonValue, JsonValue, JsonValue>[];
|
|
29
|
+
readonly inboundBus: Observable<JobInboundMessage<MinimumInputValueT>>;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Metadata associated with a job.
|
|
33
|
+
*/
|
|
34
|
+
export interface JobDescription extends JsonObject {
|
|
35
|
+
readonly name: JobName;
|
|
36
|
+
readonly argument: DeepReadonly<schema.JsonSchema>;
|
|
37
|
+
readonly input: DeepReadonly<schema.JsonSchema>;
|
|
38
|
+
readonly output: DeepReadonly<schema.JsonSchema>;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Messages that can be sent TO a job. The job needs to listen to those.
|
|
42
|
+
*/
|
|
43
|
+
export declare enum JobInboundMessageKind {
|
|
44
|
+
Ping = "ip",
|
|
45
|
+
Stop = "is",
|
|
46
|
+
Input = "in"
|
|
47
|
+
}
|
|
48
|
+
/** Base interface for the all job inbound messages. */
|
|
49
|
+
export interface JobInboundMessageBase extends JsonObject {
|
|
50
|
+
/**
|
|
51
|
+
* The kind of message this is.
|
|
52
|
+
*/
|
|
53
|
+
readonly kind: JobInboundMessageKind;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* A ping to the job. The job should reply with a pong as soon as possible.
|
|
57
|
+
*/
|
|
58
|
+
export interface JobInboundMessagePing extends JobInboundMessageBase {
|
|
59
|
+
readonly kind: JobInboundMessageKind.Ping;
|
|
60
|
+
/**
|
|
61
|
+
* An ID that should be returned in the corresponding Pong.
|
|
62
|
+
*/
|
|
63
|
+
readonly id: number;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Stop the job. This is handled by the job itself and jobs might not handle it. It will also
|
|
67
|
+
* unsubscribe from the Observable<>.
|
|
68
|
+
* This is equivalent to SIGTERM.
|
|
69
|
+
*/
|
|
70
|
+
export interface JobInboundMessageStop extends JobInboundMessageBase {
|
|
71
|
+
readonly kind: JobInboundMessageKind.Stop;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* A Job wants to send a message to a channel. This can be marshaled, and the Job object
|
|
75
|
+
* has helpers to transform this into an observable. The context also can create RxJS subjects that
|
|
76
|
+
* marshall messages through a channel.
|
|
77
|
+
*/
|
|
78
|
+
export interface JobInboundMessageInput<InputT extends JsonValue> extends JobInboundMessageBase {
|
|
79
|
+
readonly kind: JobInboundMessageKind.Input;
|
|
80
|
+
/**
|
|
81
|
+
* The input being sent to the job.
|
|
82
|
+
*/
|
|
83
|
+
readonly value: InputT;
|
|
84
|
+
}
|
|
85
|
+
export declare type JobInboundMessage<InputT extends JsonValue> = JobInboundMessagePing | JobInboundMessageStop | JobInboundMessageInput<InputT>;
|
|
86
|
+
/**
|
|
87
|
+
* Kind of messages that can be outputted from a job.
|
|
88
|
+
*/
|
|
89
|
+
export declare enum JobOutboundMessageKind {
|
|
90
|
+
OnReady = "c",
|
|
91
|
+
Start = "s",
|
|
92
|
+
End = "e",
|
|
93
|
+
Pong = "p",
|
|
94
|
+
Output = "o",
|
|
95
|
+
ChannelCreate = "cn",
|
|
96
|
+
ChannelMessage = "cm",
|
|
97
|
+
ChannelError = "ce",
|
|
98
|
+
ChannelComplete = "cc"
|
|
99
|
+
}
|
|
100
|
+
/** Base interface for the all job messages. */
|
|
101
|
+
export interface JobOutboundMessageBase {
|
|
102
|
+
/**
|
|
103
|
+
* The job description.
|
|
104
|
+
*/
|
|
105
|
+
readonly description: JobDescription;
|
|
106
|
+
/**
|
|
107
|
+
* The kind of message this is.
|
|
108
|
+
*/
|
|
109
|
+
readonly kind: JobOutboundMessageKind;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* The job has been created and will validate its input.
|
|
113
|
+
*/
|
|
114
|
+
export interface JobOutboundMessageOnReady extends JobOutboundMessageBase {
|
|
115
|
+
readonly kind: JobOutboundMessageKind.OnReady;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* The job started. This is done by the job itself.
|
|
119
|
+
*/
|
|
120
|
+
export interface JobOutboundMessageStart extends JobOutboundMessageBase {
|
|
121
|
+
readonly kind: JobOutboundMessageKind.Start;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* An output value is available.
|
|
125
|
+
*/
|
|
126
|
+
export interface JobOutboundMessageOutput<OutputT extends JsonValue> extends JobOutboundMessageBase {
|
|
127
|
+
readonly kind: JobOutboundMessageKind.Output;
|
|
128
|
+
/**
|
|
129
|
+
* The message being outputted from the job.
|
|
130
|
+
*/
|
|
131
|
+
readonly value: OutputT;
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Base interface for all job message related to channels.
|
|
135
|
+
*/
|
|
136
|
+
export interface JobOutboundMessageChannelBase extends JobOutboundMessageBase {
|
|
137
|
+
/**
|
|
138
|
+
* The name of the channel.
|
|
139
|
+
*/
|
|
140
|
+
readonly name: string;
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* A job wants to send a message to a channel. This can be marshaled, and the Job object
|
|
144
|
+
* has helpers to transform this into an observable. The context also can create RxJS subjects that
|
|
145
|
+
* marshall messages through a channel.
|
|
146
|
+
*/
|
|
147
|
+
export interface JobOutboundMessageChannelMessage extends JobOutboundMessageChannelBase {
|
|
148
|
+
readonly kind: JobOutboundMessageKind.ChannelMessage;
|
|
149
|
+
/**
|
|
150
|
+
* The message being sent to the channel.
|
|
151
|
+
*/
|
|
152
|
+
readonly message: JsonValue;
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* A job wants to send an error to one of its channel. This is the equivalent of throwing through
|
|
156
|
+
* an Observable. The side channel will not receive any more messages after this, and will not
|
|
157
|
+
* complete.
|
|
158
|
+
*/
|
|
159
|
+
export interface JobOutboundMessageChannelError extends JobOutboundMessageChannelBase {
|
|
160
|
+
readonly kind: JobOutboundMessageKind.ChannelError;
|
|
161
|
+
/**
|
|
162
|
+
* The error message being sent to the channel.
|
|
163
|
+
*/
|
|
164
|
+
readonly error: JsonValue;
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* A job wants to create a new channel.
|
|
168
|
+
*/
|
|
169
|
+
export interface JobOutboundMessageChannelCreate extends JobOutboundMessageChannelBase {
|
|
170
|
+
readonly kind: JobOutboundMessageKind.ChannelCreate;
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* A job wants to close the channel, as completed. This is done automatically when the job ends,
|
|
174
|
+
* or can be done from the job to close it. A closed channel might be reopened, but the user
|
|
175
|
+
* need to recall getChannel().
|
|
176
|
+
*/
|
|
177
|
+
export interface JobOutboundMessageChannelComplete extends JobOutboundMessageChannelBase {
|
|
178
|
+
readonly kind: JobOutboundMessageKind.ChannelComplete;
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* OnEnd of the job run.
|
|
182
|
+
*/
|
|
183
|
+
export interface JobOutboundMessageEnd extends JobOutboundMessageBase {
|
|
184
|
+
readonly kind: JobOutboundMessageKind.End;
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* A pong response from a ping input. The id is the same as the one passed in.
|
|
188
|
+
*/
|
|
189
|
+
export interface JobOutboundMessagePong extends JobOutboundMessageBase {
|
|
190
|
+
readonly kind: JobOutboundMessageKind.Pong;
|
|
191
|
+
/**
|
|
192
|
+
* The ID that was passed in the `Ping` messages.
|
|
193
|
+
*/
|
|
194
|
+
readonly id: number;
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Generic message type.
|
|
198
|
+
*/
|
|
199
|
+
export declare type JobOutboundMessage<OutputT extends JsonValue> = JobOutboundMessageOnReady | JobOutboundMessageStart | JobOutboundMessageOutput<OutputT> | JobOutboundMessageChannelCreate | JobOutboundMessageChannelMessage | JobOutboundMessageChannelError | JobOutboundMessageChannelComplete | JobOutboundMessageEnd | JobOutboundMessagePong;
|
|
200
|
+
/**
|
|
201
|
+
* The state of a job. These are changed as the job reports a new state through its messages.
|
|
202
|
+
*/
|
|
203
|
+
export declare enum JobState {
|
|
204
|
+
/**
|
|
205
|
+
* The job was queued and is waiting to start.
|
|
206
|
+
*/
|
|
207
|
+
Queued = "queued",
|
|
208
|
+
/**
|
|
209
|
+
* The job description was found, its dependencies (see "Synchronizing and Dependencies")
|
|
210
|
+
* are done running, and the job's argument is validated and the job's code will be executed.
|
|
211
|
+
*/
|
|
212
|
+
Ready = "ready",
|
|
213
|
+
/**
|
|
214
|
+
* The job has been started. The job implementation is expected to send this as soon as its
|
|
215
|
+
* work is starting.
|
|
216
|
+
*/
|
|
217
|
+
Started = "started",
|
|
218
|
+
/**
|
|
219
|
+
* The job has ended and is done running.
|
|
220
|
+
*/
|
|
221
|
+
Ended = "ended",
|
|
222
|
+
/**
|
|
223
|
+
* An error occured and the job stopped because of internal state.
|
|
224
|
+
*/
|
|
225
|
+
Errored = "errored"
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* A Job instance, returned from scheduling a job. A Job instance is _not_ serializable.
|
|
229
|
+
*/
|
|
230
|
+
export interface Job<ArgumentT extends JsonValue = JsonValue, InputT extends JsonValue = JsonValue, OutputT extends JsonValue = JsonValue> {
|
|
231
|
+
/**
|
|
232
|
+
* Description of the job. Resolving the job's description can be done asynchronously, so this
|
|
233
|
+
* is an observable that will resolve when it's ready.
|
|
234
|
+
*/
|
|
235
|
+
readonly description: Observable<JobDescription>;
|
|
236
|
+
/**
|
|
237
|
+
* Argument sent when scheduling the job. This is a copy of the argument.
|
|
238
|
+
*/
|
|
239
|
+
readonly argument: ArgumentT;
|
|
240
|
+
/**
|
|
241
|
+
* The input to the job. This goes through the input channel as messages.
|
|
242
|
+
*/
|
|
243
|
+
readonly input: Observer<InputT>;
|
|
244
|
+
/**
|
|
245
|
+
* Outputs of this job.
|
|
246
|
+
*/
|
|
247
|
+
readonly output: Observable<OutputT>;
|
|
248
|
+
/**
|
|
249
|
+
* The current state of the job.
|
|
250
|
+
*/
|
|
251
|
+
readonly state: JobState;
|
|
252
|
+
/**
|
|
253
|
+
* Get a channel that validates against the schema. Messages will be filtered by the schema.
|
|
254
|
+
* @param name The name of the channel.
|
|
255
|
+
* @param schema A schema to use to validate messages.
|
|
256
|
+
*/
|
|
257
|
+
getChannel<T extends JsonValue>(name: string, schema?: schema.JsonSchema): Observable<T>;
|
|
258
|
+
/**
|
|
259
|
+
* Pings the job and wait for the resulting Pong before completing.
|
|
260
|
+
*/
|
|
261
|
+
ping(): Observable<never>;
|
|
262
|
+
/**
|
|
263
|
+
* Stops the job from running. This is different than unsubscribing from the output as in it
|
|
264
|
+
* sends the JobInboundMessageKind.Stop raw input to the job.
|
|
265
|
+
*/
|
|
266
|
+
stop(): void;
|
|
267
|
+
/**
|
|
268
|
+
* The JobInboundMessage messages TO the job.
|
|
269
|
+
*/
|
|
270
|
+
readonly inboundBus: Observer<JobInboundMessage<InputT>>;
|
|
271
|
+
/**
|
|
272
|
+
* The JobOutboundMessage FROM the job.
|
|
273
|
+
*/
|
|
274
|
+
readonly outboundBus: Observable<JobOutboundMessage<OutputT>>;
|
|
275
|
+
}
|
|
276
|
+
/**
|
|
277
|
+
* Options for scheduling jobs.
|
|
278
|
+
*/
|
|
279
|
+
export interface ScheduleJobOptions {
|
|
280
|
+
/**
|
|
281
|
+
* Jobs that need to finish before scheduling this job. These dependencies will be passed
|
|
282
|
+
* to the job itself in its context.
|
|
283
|
+
*/
|
|
284
|
+
dependencies?: Job | Job[];
|
|
285
|
+
}
|
|
286
|
+
export interface Registry<MinimumArgumentValueT extends JsonValue = JsonValue, MinimumInputValueT extends JsonValue = JsonValue, MinimumOutputValueT extends JsonValue = JsonValue> {
|
|
287
|
+
/**
|
|
288
|
+
* Get a job handler.
|
|
289
|
+
* @param name The name of the job to get a handler from.
|
|
290
|
+
*/
|
|
291
|
+
get<A extends MinimumArgumentValueT, I extends MinimumInputValueT, O extends MinimumOutputValueT>(name: JobName): Observable<JobHandler<A, I, O> | null>;
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* An interface that can schedule jobs.
|
|
295
|
+
*/
|
|
296
|
+
export interface Scheduler<MinimumArgumentValueT extends JsonValue = JsonValue, MinimumInputValueT extends JsonValue = JsonValue, MinimumOutputValueT extends JsonValue = JsonValue> {
|
|
297
|
+
/**
|
|
298
|
+
* Get a job description for a named job.
|
|
299
|
+
*
|
|
300
|
+
* @param name The name of the job.
|
|
301
|
+
* @returns A description, or null if no description is available for this job.
|
|
302
|
+
*/
|
|
303
|
+
getDescription(name: JobName): Observable<JobDescription | null>;
|
|
304
|
+
/**
|
|
305
|
+
* Returns true if the job name has been registered.
|
|
306
|
+
* @param name The name of the job.
|
|
307
|
+
* @returns True if the job exists, false otherwise.
|
|
308
|
+
*/
|
|
309
|
+
has(name: JobName): Observable<boolean>;
|
|
310
|
+
/**
|
|
311
|
+
* Pause the scheduler, temporary queueing _new_ jobs. Returns a resume function that should be
|
|
312
|
+
* used to resume execution. If multiple `pause()` were called, all their resume functions must
|
|
313
|
+
* be called before the Scheduler actually starts new jobs. Additional calls to the same resume
|
|
314
|
+
* function will have no effect.
|
|
315
|
+
*
|
|
316
|
+
* Jobs already running are NOT paused. This is pausing the scheduler only.
|
|
317
|
+
*
|
|
318
|
+
* @returns A function that can be run to resume the scheduler. If multiple `pause()` calls
|
|
319
|
+
* were made, all their return function must be called (in any order) before the
|
|
320
|
+
* scheduler can resume.
|
|
321
|
+
*/
|
|
322
|
+
pause(): () => void;
|
|
323
|
+
/**
|
|
324
|
+
* Schedule a job to be run, using its name.
|
|
325
|
+
* @param name The name of job to be run.
|
|
326
|
+
* @param argument The argument to send to the job when starting it.
|
|
327
|
+
* @param options Scheduling options.
|
|
328
|
+
* @returns The job being run.
|
|
329
|
+
*/
|
|
330
|
+
schedule<A extends MinimumArgumentValueT, I extends MinimumInputValueT, O extends MinimumOutputValueT>(name: JobName, argument: A, options?: ScheduleJobOptions): Job<A, I, O>;
|
|
331
|
+
}
|
|
332
|
+
export declare function isJobHandler<A extends JsonValue, I extends JsonValue, O extends JsonValue>(value: unknown): value is JobHandler<A, I, O>;
|
package/src/jobs/api.js
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @license
|
|
4
|
+
* Copyright Google LLC All Rights Reserved.
|
|
5
|
+
*
|
|
6
|
+
* Use of this source code is governed by an MIT-style license that can be
|
|
7
|
+
* found in the LICENSE file at https://angular.io/license
|
|
8
|
+
*/
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
exports.isJobHandler = exports.JobState = exports.JobOutboundMessageKind = exports.JobInboundMessageKind = void 0;
|
|
11
|
+
/**
|
|
12
|
+
* Messages that can be sent TO a job. The job needs to listen to those.
|
|
13
|
+
*/
|
|
14
|
+
var JobInboundMessageKind;
|
|
15
|
+
(function (JobInboundMessageKind) {
|
|
16
|
+
JobInboundMessageKind["Ping"] = "ip";
|
|
17
|
+
JobInboundMessageKind["Stop"] = "is";
|
|
18
|
+
// Channel specific messages.
|
|
19
|
+
JobInboundMessageKind["Input"] = "in";
|
|
20
|
+
// Input channel does not allow completion / error. Erroring this will just close the Subject
|
|
21
|
+
// but not notify the job.
|
|
22
|
+
})(JobInboundMessageKind = exports.JobInboundMessageKind || (exports.JobInboundMessageKind = {}));
|
|
23
|
+
/**
|
|
24
|
+
* Kind of messages that can be outputted from a job.
|
|
25
|
+
*/
|
|
26
|
+
var JobOutboundMessageKind;
|
|
27
|
+
(function (JobOutboundMessageKind) {
|
|
28
|
+
// Lifecycle specific messages.
|
|
29
|
+
JobOutboundMessageKind["OnReady"] = "c";
|
|
30
|
+
JobOutboundMessageKind["Start"] = "s";
|
|
31
|
+
JobOutboundMessageKind["End"] = "e";
|
|
32
|
+
JobOutboundMessageKind["Pong"] = "p";
|
|
33
|
+
// Feedback messages.
|
|
34
|
+
JobOutboundMessageKind["Output"] = "o";
|
|
35
|
+
// Channel specific messages.
|
|
36
|
+
JobOutboundMessageKind["ChannelCreate"] = "cn";
|
|
37
|
+
JobOutboundMessageKind["ChannelMessage"] = "cm";
|
|
38
|
+
JobOutboundMessageKind["ChannelError"] = "ce";
|
|
39
|
+
JobOutboundMessageKind["ChannelComplete"] = "cc";
|
|
40
|
+
})(JobOutboundMessageKind = exports.JobOutboundMessageKind || (exports.JobOutboundMessageKind = {}));
|
|
41
|
+
/**
|
|
42
|
+
* The state of a job. These are changed as the job reports a new state through its messages.
|
|
43
|
+
*/
|
|
44
|
+
var JobState;
|
|
45
|
+
(function (JobState) {
|
|
46
|
+
/**
|
|
47
|
+
* The job was queued and is waiting to start.
|
|
48
|
+
*/
|
|
49
|
+
JobState["Queued"] = "queued";
|
|
50
|
+
/**
|
|
51
|
+
* The job description was found, its dependencies (see "Synchronizing and Dependencies")
|
|
52
|
+
* are done running, and the job's argument is validated and the job's code will be executed.
|
|
53
|
+
*/
|
|
54
|
+
JobState["Ready"] = "ready";
|
|
55
|
+
/**
|
|
56
|
+
* The job has been started. The job implementation is expected to send this as soon as its
|
|
57
|
+
* work is starting.
|
|
58
|
+
*/
|
|
59
|
+
JobState["Started"] = "started";
|
|
60
|
+
/**
|
|
61
|
+
* The job has ended and is done running.
|
|
62
|
+
*/
|
|
63
|
+
JobState["Ended"] = "ended";
|
|
64
|
+
/**
|
|
65
|
+
* An error occured and the job stopped because of internal state.
|
|
66
|
+
*/
|
|
67
|
+
JobState["Errored"] = "errored";
|
|
68
|
+
})(JobState = exports.JobState || (exports.JobState = {}));
|
|
69
|
+
function isJobHandler(value) {
|
|
70
|
+
const job = value;
|
|
71
|
+
return (typeof job == 'function' && typeof job.jobDescription == 'object' && job.jobDescription !== null);
|
|
72
|
+
}
|
|
73
|
+
exports.isJobHandler = isJobHandler;
|
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
# Overview
|
|
2
|
+
|
|
3
|
+
Jobs is a high-order API that adds inputs, runtime type checking, sequencing, and other
|
|
4
|
+
functionality on top of RxJS' `Observable`s.
|
|
5
|
+
|
|
6
|
+
# Background
|
|
7
|
+
|
|
8
|
+
An `Observable` (at a higher level) is a function that receives a `Subscriber`, and outputs
|
|
9
|
+
multiple values, and finishes once it calls the `Subscriber.prototype.complete()` method (in
|
|
10
|
+
JavaScript):
|
|
11
|
+
|
|
12
|
+
```javascript
|
|
13
|
+
const output1To10EverySecond = function (subscriber) {
|
|
14
|
+
let t = 0;
|
|
15
|
+
const i = setInterval(() => {
|
|
16
|
+
t++;
|
|
17
|
+
subscriber.next(t);
|
|
18
|
+
if (t === 10) {
|
|
19
|
+
subscriber.complete(t);
|
|
20
|
+
}
|
|
21
|
+
}, 1000);
|
|
22
|
+
return () => clearInterval(i);
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const stream$ = new Observable(output1To10EverySecond);
|
|
26
|
+
// Start the function, and output 1 to 100, once per line.
|
|
27
|
+
stream$.subscribe((x) => console.log(x));
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
This, of course, can be typed in TypeScript, but those types are not enforced at runtime.
|
|
31
|
+
|
|
32
|
+
# Glossary
|
|
33
|
+
|
|
34
|
+
- `job handler`. The function that implements the job's logic.
|
|
35
|
+
- `raw input`. The input observable sending messages to the job. These messages are of type
|
|
36
|
+
`JobInboundMessage`.
|
|
37
|
+
- `raw output`. The output observer returned from the `job handler`. Messages on this observable
|
|
38
|
+
are of type `JobOutboundMessage`.
|
|
39
|
+
|
|
40
|
+
# Description
|
|
41
|
+
|
|
42
|
+
A `JobHandler`, similar to observables, is a function that receives an argument and a context, and
|
|
43
|
+
returns an `Observable` of messages, which can include outputs that are typed at runtime (using a
|
|
44
|
+
Json Schema):
|
|
45
|
+
|
|
46
|
+
```javascript
|
|
47
|
+
const output1ToXEverySecond = function (x, context) {
|
|
48
|
+
return new Observable((subscriber) => {
|
|
49
|
+
let t = 0;
|
|
50
|
+
|
|
51
|
+
// Notify our users that the actual work is started.
|
|
52
|
+
subscriber.next({ kind: JobOutboundMessageKind.Start });
|
|
53
|
+
const i = setInterval(() => {
|
|
54
|
+
t++;
|
|
55
|
+
subscriber.next({ kind: JobOutboundMessageKind.Output, value: t });
|
|
56
|
+
if (t === x) {
|
|
57
|
+
subscriber.next({ kind: JobOutboundMessageKind.End });
|
|
58
|
+
subscriber.complete();
|
|
59
|
+
}
|
|
60
|
+
}, 1000);
|
|
61
|
+
|
|
62
|
+
return () => {
|
|
63
|
+
clearInterval(i);
|
|
64
|
+
};
|
|
65
|
+
});
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
// For now, jobs can not be called without a registry and scheduler.
|
|
69
|
+
const registry = new SimpleJobRegistry();
|
|
70
|
+
registry.register('output-from-1-to-x', output1ToXEverySecond, {
|
|
71
|
+
argument: { type: 'number' },
|
|
72
|
+
output: { type: 'number' },
|
|
73
|
+
});
|
|
74
|
+
const scheduler = new SimpleScheduler(registry);
|
|
75
|
+
|
|
76
|
+
// Need to keep the same name that the registry would understand.
|
|
77
|
+
// Count from 1 to 10.
|
|
78
|
+
const job = scheduler.schedule('output-from-1-to-x', 10);
|
|
79
|
+
|
|
80
|
+
// A Job<> instance has more members, but we only want the output values here.
|
|
81
|
+
job.output.subscribe((x) => console.log(x));
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
This seems like a lot of boilerplate in comparison, but there are a few advantages;
|
|
85
|
+
|
|
86
|
+
1. lifecycle. Jobs can tell when they start doing work and when work is done.
|
|
87
|
+
1. everything is typed, even at runtime.
|
|
88
|
+
1. the context also contains an input Observable that receives typed input messages, including
|
|
89
|
+
input values, and stop requests.
|
|
90
|
+
1. jobs can also schedule other jobs and wait for them, even if they don't know if a job is
|
|
91
|
+
implemented in the system.
|
|
92
|
+
|
|
93
|
+
## Diagram
|
|
94
|
+
|
|
95
|
+
A simpler way to think about jobs in contrast to observables is that job are closer to a Unix
|
|
96
|
+
process. It has an argument (command line flags), receive inputs (STDIN and interrupt signals),
|
|
97
|
+
and output values (STDOUT) as well as diagnostic (STDERR). They can be plugged one into another
|
|
98
|
+
(piping), and can be transformed, synchronized and scheduled (fork, exec, cron).
|
|
99
|
+
|
|
100
|
+
```plain
|
|
101
|
+
- given A the type of the argument
|
|
102
|
+
- given I the type of the input
|
|
103
|
+
- given O the type of the output
|
|
104
|
+
|
|
105
|
+
,______________________
|
|
106
|
+
JobInboundMessage<I> --> | handler(argument: A) | --> JobOutboundMessage<O>
|
|
107
|
+
- JobOutboundMessageKind.Output
|
|
108
|
+
- ...
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
`JobInboundMessage` includes:
|
|
112
|
+
|
|
113
|
+
1. `JobInboundMessageKind.Ping`. A simple message that should be answered with
|
|
114
|
+
`JobOutboundMessageKind.Pong` when the job is responsive. The `id` field of the message should
|
|
115
|
+
be used when returning `Pong`.
|
|
116
|
+
1. `JobInboundMessageKind.Stop`. The job should be stopped. This is used when
|
|
117
|
+
cancelling/unsubscribing from the `output` (or by calling `stop()`). Any inputs or outputs
|
|
118
|
+
after this message will be ignored.
|
|
119
|
+
1. `JobInboundMessageKind.Input` is used when sending inputs to a job. These correspond to the
|
|
120
|
+
`next` methods of an `Observer` and are reported to the job through its `context.input`
|
|
121
|
+
Observable. There is no way to communicate an error to the job.
|
|
122
|
+
|
|
123
|
+
`JobOutboundMessage` includes:
|
|
124
|
+
|
|
125
|
+
1. `JobOutboundMessageKind.Ready`. The `Job<>` was created, its dependencies are done, and the
|
|
126
|
+
library is validating Argument and calling the internal job code.
|
|
127
|
+
1. `JobOutboundMessageKind.Start`. The job code itself should send that message when started.
|
|
128
|
+
`createJobHandler()` will do it automatically.
|
|
129
|
+
1. `JobOutboundMessageKind.End`. The job has ended. This is done by the job itself and should
|
|
130
|
+
always be sent when completed. The scheduler will listen to this message to set the state and
|
|
131
|
+
unblock dependent jobs. `createJobHandler()` automatically send this message.
|
|
132
|
+
1. `JobOutboundMessageKind.Pong`. The job should answer a `JobInboundMessageKind.Ping` message with
|
|
133
|
+
this. Automatically done by `createJobHandler()`.
|
|
134
|
+
1. `JobOutboundMessageKind.Output`. An `Output` has been generated by the job.
|
|
135
|
+
1. `JobOutboundMessageKind.ChannelMessage`, `JobOutboundMessageKind.ChannelError` and
|
|
136
|
+
`JobOutboundMessageKind.ChannelComplete` are used for output channels. These correspond to
|
|
137
|
+
the `next`, `error` and `complete` methods of an `Observer` and are available to the callee
|
|
138
|
+
through the `job.channels` map of Observable.
|
|
139
|
+
|
|
140
|
+
Utilities should have some filtering and dispatching to separate observables, as a convenience for
|
|
141
|
+
the user. An example of this would be the `Job.prototype.output` observable which only contains
|
|
142
|
+
the value contained by messages of type `JobOutboundMessageKind.Output`.
|
|
143
|
+
|
|
144
|
+
# Higher Order Jobs
|
|
145
|
+
|
|
146
|
+
Because jobs are expected to be pure functions, they can be composed or transformed to create
|
|
147
|
+
more complex behaviour, similar to how RxJS operators can transform observables.
|
|
148
|
+
|
|
149
|
+
```javascript
|
|
150
|
+
// Runs a job on the hour, every hour, regardless of how long the job takes.
|
|
151
|
+
// This creates a job function that can be registered by itself.
|
|
152
|
+
function scheduleJobOnTheHour(jobFunction) {
|
|
153
|
+
return function (argument, context) {
|
|
154
|
+
return new Observable((observer) => {
|
|
155
|
+
let timeout = 0;
|
|
156
|
+
|
|
157
|
+
function _timeoutToNextHour() {
|
|
158
|
+
// Just wait until the next hour.
|
|
159
|
+
const t = new Date();
|
|
160
|
+
const secondsToNextHour = 3600 - t.getSeconds() - t.getMinutes() * 60;
|
|
161
|
+
timeout = setTimeout(_scheduleJobAndWaitAnHour, secondsToNextHour);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function _scheduleJobAndWaitAnHour() {
|
|
165
|
+
jobFunction(argument, context).subscribe(
|
|
166
|
+
(message) => observer.next(message),
|
|
167
|
+
(error) => observer.error(error),
|
|
168
|
+
// Do not forward completion, but use it to schedule the next job run.
|
|
169
|
+
() => {
|
|
170
|
+
_timeoutToNextHour();
|
|
171
|
+
},
|
|
172
|
+
);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// Kick off by waiting for next hour.
|
|
176
|
+
_timeoutToNextHour();
|
|
177
|
+
|
|
178
|
+
return () => clearTimeout(timeout);
|
|
179
|
+
});
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
Another way to compose jobs is to schedule jobs based on their name, from other jobs.
|
|
185
|
+
|
|
186
|
+
```javascript
|
|
187
|
+
// Runs a job on the hour, every hour, regardless of how long the job takes.
|
|
188
|
+
// This creates a high order job by getting a job name and an argument, and scheduling the job
|
|
189
|
+
// every hour.
|
|
190
|
+
function scheduleJobOnTheHour(job, context) {
|
|
191
|
+
const { name, argument } = job; // Destructure our input.
|
|
192
|
+
|
|
193
|
+
return new Observable((observer) => {
|
|
194
|
+
let timeout = 0;
|
|
195
|
+
|
|
196
|
+
function _timeoutToNextHour() {
|
|
197
|
+
// Just wait until the next hour.
|
|
198
|
+
const t = new Date();
|
|
199
|
+
const secondsToNextHour = 3600 - t.getSeconds() - t.getMinutes() * 60;
|
|
200
|
+
timeout = setTimeout(_scheduleJobAndWaitAnHour, secondsToNextHour);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
function _scheduleJobAndWaitAnHour() {
|
|
204
|
+
const subJob = context.scheduler.schedule(name, argument);
|
|
205
|
+
// We do not forward the input to the sub-job but that would be a valid example as well.
|
|
206
|
+
subJob.outboundBus.subscribe(
|
|
207
|
+
(message) => observer.next(message),
|
|
208
|
+
(error) => observer.error(error),
|
|
209
|
+
// Do not forward completion, but use it to schedule the next job run.
|
|
210
|
+
() => {
|
|
211
|
+
_timeoutToNextHour();
|
|
212
|
+
},
|
|
213
|
+
);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// Kick off by waiting for next hour.
|
|
217
|
+
_timeoutToNextHour();
|
|
218
|
+
|
|
219
|
+
return () => clearTimeout(timeout);
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
const registry = new SimpleJobRegistry();
|
|
224
|
+
registry.register('schedule-job-on-the-hour', scheduleJobOnTheHour, {
|
|
225
|
+
argument: {
|
|
226
|
+
properties: {
|
|
227
|
+
name: { type: 'string' },
|
|
228
|
+
argument: { type: true },
|
|
229
|
+
},
|
|
230
|
+
},
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
// Implementation left to the reader.
|
|
234
|
+
registry.register('copy-files-from-a-to-b', require('some-package/copy-job'));
|
|
235
|
+
|
|
236
|
+
const scheduler = new SimpleScheduler(registry);
|
|
237
|
+
|
|
238
|
+
// A rudimentary backup system.
|
|
239
|
+
const job = scheduler.schedule('schedule-job-on-the-hour', {
|
|
240
|
+
name: 'copy-files-from-a-to-b',
|
|
241
|
+
argument: {
|
|
242
|
+
from: '/some-directory/to/backup',
|
|
243
|
+
to: '/volumes/usb-key',
|
|
244
|
+
},
|
|
245
|
+
});
|
|
246
|
+
job.output.subscribe((x) => console.log(x));
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
# Limitations
|
|
250
|
+
|
|
251
|
+
Jobs input, output and argument must be serializable to JSONs. This is a big limitation in usage,
|
|
252
|
+
but comes with the benefit that jobs can be serialized and called across memory boundaries. An
|
|
253
|
+
example would be an operator that takes a module path and run the job from that path in a separate
|
|
254
|
+
process. Or even a separate server, using HTTP calls.
|
|
255
|
+
|
|
256
|
+
Another limitation is that the boilerplate is complex. Manually managing start/end life cycle, and
|
|
257
|
+
other messages such as ping/pong, etc. is tedious and requires a lot of code. A good way to keep
|
|
258
|
+
this limitation under control is to provide helpers to create `JobHandler`s which manage those
|
|
259
|
+
messages for the developer. A simple handler could be to get a `Promise` and return the output of
|
|
260
|
+
that promise automatically.
|