@elysiumjs/heracles 0.4.4 → 0.5.0

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/src/transport.ts DELETED
@@ -1,518 +0,0 @@
1
- // Copyright (c) 2025-present Workbud Technologies Inc. All rights reserved.
2
- //
3
- // Licensed under the Apache License, Version 2.0 (the "License");
4
- // you may not use this file except in compliance with the License.
5
- // You may obtain a copy of the License at
6
- //
7
- // http://www.apache.org/licenses/LICENSE-2.0
8
- //
9
- // Unless required by applicable law or agreed to in writing, software
10
- // distributed under the License is distributed on an "AS IS" BASIS,
11
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- // See the License for the specific language governing permissions and
13
- // limitations under the License.
14
-
15
- import type { JobDispatchOptions } from './queue';
16
-
17
- /**
18
- * Discriminated union of all events carried by a transport channel.
19
- */
20
- export type TransportEvent =
21
- | TransportProcessMessage
22
- | TransportCancelMessage
23
- | TransportCancelAllMessage
24
- | TransportJobStatusMessage
25
- | TransportWorkerStatusMessage
26
- | TransportJobResultMessage
27
- | TransportWorkerRegistrationMessage
28
- | TransportWorkerReadyMessage
29
- | TransportJobUpdateMessage;
30
-
31
- /**
32
- * Message sent to process a job.
33
- */
34
- export type TransportProcessMessage = {
35
- /**
36
- * The type of message.
37
- */
38
- type: 'job:process';
39
-
40
- /**
41
- * The name of the job to process. It should match the name of the job class,
42
- * or the alias in the Service container.
43
- */
44
- job: string;
45
-
46
- /**
47
- * Arguments to pass to the job constructor.
48
- */
49
- args: unknown[];
50
-
51
- /**
52
- * The name of the queue to process the job.
53
- */
54
- queue: string;
55
-
56
- /**
57
- * The ID of the job.
58
- * When creating a new job, this will be generated if not provided.
59
- */
60
- jobId: string;
61
-
62
- /**
63
- * The dispatch ID of the job.
64
- * This is unique for each dispatch, even if the job ID is the same.
65
- */
66
- dispatchId: string;
67
-
68
- /**
69
- * Optional job dispatch configuration.
70
- * Note: scheduledFor is transmitted as ISO string instead of Date object.
71
- */
72
- options?: Omit<JobDispatchOptions, 'scheduledFor'> & {
73
- /**
74
- * When the job should be scheduled to run (ISO string).
75
- */
76
- scheduledFor?: string;
77
- };
78
- };
79
-
80
- /**
81
- * Message sent to cancel a job (by ID).
82
- */
83
- export type TransportCancelMessage = {
84
- /**
85
- * The type of message.
86
- */
87
- type: 'job:cancel';
88
-
89
- /**
90
- * The ID of the job to cancel.
91
- */
92
- jobId: string;
93
-
94
- /**
95
- * The ID of the dispatch.
96
- */
97
- dispatchId: string;
98
-
99
- /**
100
- * The name of the queue where the job is located.
101
- */
102
- queue: string;
103
- };
104
-
105
- /**
106
- * Message sent to cancel all jobs.
107
- */
108
- export type TransportCancelAllMessage = {
109
- /**
110
- * The type of message.
111
- */
112
- type: 'job:cancelAll';
113
-
114
- /**
115
- * The name of the queue to cancel all jobs in.
116
- */
117
- queue: string;
118
- };
119
-
120
- /**
121
- * Message with job status information.
122
- */
123
- export type TransportJobStatusMessage = {
124
- /**
125
- * The type of message.
126
- */
127
- type: 'job:status';
128
-
129
- /**
130
- * The ID of the job.
131
- */
132
- jobId: string;
133
-
134
- /**
135
- * The ID of the dispatch that created the job.
136
- */
137
- dispatchId: string;
138
-
139
- /**
140
- * The name of the queue the job belongs to.
141
- */
142
- queue: string;
143
-
144
- /**
145
- * The current status of the job.
146
- */
147
- status: string;
148
-
149
- /**
150
- * The error message if the job failed.
151
- */
152
- error?: string;
153
-
154
- /**
155
- * The number of retries attempted.
156
- */
157
- retries?: number;
158
-
159
- /**
160
- * Timestamp when the job started.
161
- */
162
- startedAt?: string;
163
-
164
- /**
165
- * Timestamp when the job completed or failed.
166
- */
167
- completedAt?: string;
168
-
169
- /**
170
- * Timestamp when this status update was created.
171
- */
172
- updatedAt?: string;
173
-
174
- /**
175
- * Timestamp when the job was created.
176
- */
177
- createdAt?: string;
178
- };
179
-
180
- /**
181
- * Message with worker status information.
182
- */
183
- export type TransportWorkerStatusMessage = {
184
- /**
185
- * The type of message.
186
- */
187
- type: 'worker:status';
188
-
189
- /**
190
- * The ID of the worker.
191
- */
192
- workerId: string;
193
-
194
- /**
195
- * The queues this worker is handling.
196
- */
197
- queues: string[];
198
-
199
- /**
200
- * The current status of the worker.
201
- */
202
- status: 'active' | 'paused' | 'draining' | 'stopped';
203
-
204
- /**
205
- * The number of jobs currently being processed by this worker.
206
- */
207
- processing: number;
208
-
209
- /**
210
- * The number of jobs waiting in this worker's queues.
211
- */
212
- waiting: number;
213
- };
214
-
215
- /**
216
- * Message with job results.
217
- */
218
- export type TransportJobResultMessage = {
219
- /**
220
- * The type of message.
221
- */
222
- type: 'job:result';
223
-
224
- /**
225
- * The ID of the job.
226
- */
227
- jobId: string;
228
-
229
- /**
230
- * The ID of the dispatch that created the job.
231
- */
232
- dispatchId: string;
233
-
234
- /**
235
- * The name of the queue the job belongs to.
236
- */
237
- queue: string;
238
-
239
- /**
240
- * The final status of the job.
241
- */
242
- status: string;
243
-
244
- /**
245
- * The error message if the job failed.
246
- */
247
- error?: string;
248
-
249
- /**
250
- * Timestamp when the job completed or failed.
251
- */
252
- completedAt: string;
253
-
254
- /**
255
- * Timestamp when this update was created.
256
- */
257
- updatedAt?: string;
258
-
259
- /**
260
- * The number of retries that were attempted.
261
- */
262
- retries?: number;
263
- };
264
-
265
- /**
266
- * Message for worker registration.
267
- */
268
- export type TransportWorkerRegistrationMessage = {
269
- /**
270
- * The type of message.
271
- */
272
- type: 'worker:register' | 'worker:unregister';
273
-
274
- /**
275
- * The ID of the worker.
276
- */
277
- workerId: string;
278
-
279
- /**
280
- * The queues the worker handles (for registration).
281
- */
282
- queues?: string[];
283
- };
284
-
285
- /**
286
- * Message indicating a worker is ready.
287
- */
288
- export type TransportWorkerReadyMessage = {
289
- /**
290
- * The type of message.
291
- */
292
- type: 'worker:ready';
293
-
294
- /**
295
- * The ID of the worker.
296
- */
297
- id: string;
298
-
299
- /**
300
- * The queues the worker handles.
301
- */
302
- queues: string[];
303
-
304
- /**
305
- * The timestamp when the worker became ready.
306
- */
307
- timestamp?: number;
308
-
309
- /**
310
- * The worker status.
311
- */
312
- status?: string;
313
- };
314
-
315
- /**
316
- * Job status information.
317
- */
318
- export interface JobStatusInfo {
319
- /**
320
- * The ID of the job.
321
- */
322
- jobId: string;
323
-
324
- /**
325
- * The dispatch ID of the job.
326
- * This uniquely identifies a specific dispatch of a job, even if the job ID is the same.
327
- */
328
- dispatchId: string;
329
-
330
- /**
331
- * The name of the queue the job belongs to.
332
- */
333
- queue: string;
334
-
335
- /**
336
- * The current status of the job.
337
- */
338
- status: string;
339
-
340
- /**
341
- * The error message if the job failed.
342
- */
343
- error?: string;
344
-
345
- /**
346
- * The number of retries attempted.
347
- */
348
- retries: number;
349
-
350
- /**
351
- * Timestamp when the job was created.
352
- */
353
- createdAt: string;
354
-
355
- /**
356
- * Timestamp when the job started.
357
- */
358
- startedAt?: string;
359
-
360
- /**
361
- * Timestamp when the job completed or failed.
362
- */
363
- completedAt?: string;
364
-
365
- /**
366
- * Timestamp when this status was last updated.
367
- */
368
- updatedAt?: string;
369
-
370
- /**
371
- * The original message ID in the Redis stream (internal use).
372
- */
373
- messageId?: string;
374
- }
375
-
376
- /**
377
- * Indicates what mode a Transport is operating in.
378
- */
379
- export enum TransportMode {
380
- /**
381
- * Listens for jobs/events from the transport (e.g., worker).
382
- */
383
- CONSUMER = 'consumer',
384
-
385
- /**
386
- * Actively pushes jobs/events to the transport (e.g., dispatcher).
387
- */
388
- PRODUCER = 'producer'
389
- }
390
-
391
- /**
392
- * Message specific to job status updates.
393
- */
394
- export type TransportJobUpdateMessage = {
395
- /**
396
- * The type of message.
397
- */
398
- type: 'job:update';
399
-
400
- /**
401
- * The ID of the job.
402
- */
403
- jobId: string;
404
-
405
- /**
406
- * The ID of the dispatch.
407
- */
408
- dispatchId: string;
409
-
410
- /**
411
- * The name of the queue the job belongs to.
412
- */
413
- queue: string;
414
-
415
- /**
416
- * The current status of the job.
417
- */
418
- status: string;
419
-
420
- /**
421
- * Updates to the job status fields.
422
- */
423
- updates: Partial<JobStatusInfo>;
424
-
425
- /**
426
- * Whether to create a new entry if the job doesn't exist.
427
- */
428
- createIfNotExist?: boolean;
429
- };
430
-
431
- /**
432
- * Interface for any transport layer that delivers job events between queue and worker.
433
- */
434
- export interface Transport {
435
- /**
436
- * Starts the transport.
437
- *
438
- * This method should initialize the transport and mark it as ready to receive messages.
439
- */
440
- start(): Promise<void> | void;
441
-
442
- /**
443
- * Stops the transport.
444
- *
445
- * This method should stop the transport and mark it as stopped.
446
- */
447
- stop(): Promise<void> | void;
448
-
449
- /**
450
- * Sends a message over the transport.
451
- *
452
- * @param message The message to send.
453
- * @returns A promise that resolves when the message has been sent.
454
- */
455
- send(message: TransportEvent): Promise<void> | void;
456
-
457
- /**
458
- * Registers a callback for receiving messages from the transport.
459
- *
460
- * @param handler The handler function to call when a message is received.
461
- */
462
- onMessage(handler: (message: TransportEvent) => void | Promise<void>): void;
463
-
464
- /**
465
- * Gets the current status of a job.
466
- *
467
- * @param jobId The ID of the job to get the status for.
468
- * @param dispatchId The ID generated for the dispatch of the job.
469
- * @param queueName The name of the queue the job belongs to.
470
- * @returns A promise that resolves with the current job status.
471
- */
472
- getJobStatus(jobId: string, dispatchId: string, queueName: string): Promise<JobStatusInfo>;
473
-
474
- /**
475
- * Registers a worker with the transport.
476
- *
477
- * @param workerId The ID of the worker.
478
- * @param queues The queues the worker handles.
479
- * @returns A promise that resolves when the worker is registered.
480
- */
481
- registerWorker(workerId: string, queues: string[]): Promise<void>;
482
-
483
- /**
484
- * Unregisters a worker from the transport.
485
- *
486
- * @param workerId The ID of the worker to unregister.
487
- * @returns A promise that resolves when the worker is unregistered.
488
- */
489
- unregisterWorker(workerId: string): Promise<void>;
490
-
491
- /**
492
- * Updates a job status directly without creating a new message.
493
- *
494
- * @param jobId The ID of the job to update.
495
- * @param queueName The name of the queue the job belongs to.
496
- * @param updates The status updates to apply.
497
- * @returns A promise that resolves when the job is updated.
498
- */
499
- updateJobStatus(
500
- jobId: string,
501
- dispatchId: string,
502
- queueName: string,
503
- updates: Partial<JobStatusInfo>
504
- ): Promise<void>;
505
- }
506
-
507
- /**
508
- * Type signature for a transport constructor.
509
- */
510
- export type TransportClass<T extends Transport = Transport> = {
511
- /**
512
- * Creates a new transport instance.
513
- *
514
- * @param mode The mode of the transport (consumer or producer).
515
- * @param options The options to pass to the transport.
516
- */
517
- new (mode: TransportMode, options?: Record<string, any>): T;
518
- };