@workglow/job-queue 0.0.69 → 0.0.70

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/dist/bun.js CHANGED
@@ -137,6 +137,71 @@ class Job {
137
137
  // src/job/JobQueueClient.ts
138
138
  import { JobStatus as JobStatus2 } from "@workglow/storage";
139
139
  import { EventEmitter } from "@workglow/util";
140
+
141
+ // src/job/JobStorageConverters.ts
142
+ function toDate(date) {
143
+ if (!date)
144
+ return null;
145
+ const d = new Date(date);
146
+ return isNaN(d.getTime()) ? null : d;
147
+ }
148
+ function dateToISOString(date) {
149
+ if (!date)
150
+ return null;
151
+ return isNaN(date.getTime()) ? null : date.toISOString();
152
+ }
153
+ function storageToClass(details, jobClass, options) {
154
+ const includeWorkerId = options?.includeWorkerId ?? true;
155
+ return new jobClass({
156
+ id: details.id,
157
+ jobRunId: details.job_run_id,
158
+ queueName: details.queue,
159
+ fingerprint: details.fingerprint,
160
+ input: details.input,
161
+ output: details.output,
162
+ runAfter: toDate(details.run_after),
163
+ createdAt: toDate(details.created_at),
164
+ deadlineAt: toDate(details.deadline_at),
165
+ lastRanAt: toDate(details.last_ran_at),
166
+ completedAt: toDate(details.completed_at),
167
+ progress: details.progress || 0,
168
+ progressMessage: details.progress_message || "",
169
+ progressDetails: details.progress_details ?? null,
170
+ status: details.status,
171
+ error: details.error ?? null,
172
+ errorCode: details.error_code ?? null,
173
+ runAttempts: details.run_attempts ?? 0,
174
+ maxRetries: details.max_retries ?? 10,
175
+ ...includeWorkerId ? { workerId: details.worker_id ?? null } : {}
176
+ });
177
+ }
178
+ function classToStorage(job, queueName) {
179
+ const now = new Date().toISOString();
180
+ return {
181
+ id: job.id,
182
+ job_run_id: job.jobRunId,
183
+ queue: job.queueName || queueName,
184
+ fingerprint: job.fingerprint,
185
+ input: job.input,
186
+ status: job.status,
187
+ output: job.output ?? null,
188
+ error: job.error === null ? null : String(job.error),
189
+ error_code: job.errorCode || null,
190
+ run_attempts: job.runAttempts ?? 0,
191
+ max_retries: job.maxRetries ?? 10,
192
+ run_after: dateToISOString(job.runAfter) ?? now,
193
+ created_at: dateToISOString(job.createdAt) ?? now,
194
+ deadline_at: dateToISOString(job.deadlineAt),
195
+ last_ran_at: dateToISOString(job.lastRanAt),
196
+ completed_at: dateToISOString(job.completedAt),
197
+ progress: job.progress ?? 0,
198
+ progress_message: job.progressMessage ?? "",
199
+ progress_details: job.progressDetails ?? null,
200
+ worker_id: job.workerId ?? null
201
+ };
202
+ }
203
+
204
+ // src/job/JobQueueClient.ts
140
205
  class JobQueueClient {
141
206
  queueName;
142
207
  storage;
@@ -267,7 +332,7 @@ class JobQueueClient {
267
332
  throw new JobNotFoundError("Cannot abort job run with undefined jobRunId");
268
333
  const jobs = await this.getJobsByRunId(jobRunId);
269
334
  await Promise.allSettled(jobs.map((job) => {
270
- if ([JobStatus2.PROCESSING, JobStatus2.PENDING].includes(job.status)) {
335
+ if (job.status === JobStatus2.PROCESSING || job.status === JobStatus2.PENDING) {
271
336
  return this.abort(job.id);
272
337
  }
273
338
  }));
@@ -300,6 +365,9 @@ class JobQueueClient {
300
365
  waitOn(event) {
301
366
  return this.events.waitOn(event);
302
367
  }
368
+ subscribe(event, listener) {
369
+ return this.events.subscribe(event, listener);
370
+ }
303
371
  handleJobStart(jobId) {
304
372
  this.lastKnownProgress.set(jobId, {
305
373
  progress: 0,
@@ -389,33 +457,7 @@ class JobQueueClient {
389
457
  }
390
458
  }
391
459
  storageToClass(details) {
392
- const toDate = (date) => {
393
- if (!date)
394
- return null;
395
- const d = new Date(date);
396
- return isNaN(d.getTime()) ? null : d;
397
- };
398
- return new Job({
399
- id: details.id,
400
- jobRunId: details.job_run_id,
401
- queueName: details.queue,
402
- fingerprint: details.fingerprint,
403
- input: details.input,
404
- output: details.output,
405
- runAfter: toDate(details.run_after),
406
- createdAt: toDate(details.created_at),
407
- deadlineAt: toDate(details.deadline_at),
408
- lastRanAt: toDate(details.last_ran_at),
409
- completedAt: toDate(details.completed_at),
410
- progress: details.progress || 0,
411
- progressMessage: details.progress_message || "",
412
- progressDetails: details.progress_details ?? null,
413
- status: details.status,
414
- error: details.error ?? null,
415
- errorCode: details.error_code ?? null,
416
- runAttempts: details.run_attempts ?? 0,
417
- maxRetries: details.max_retries ?? 10
418
- });
460
+ return storageToClass(details, Job, { includeWorkerId: true });
419
461
  }
420
462
  buildErrorFromJob(job) {
421
463
  return this.buildErrorFromCode(job.error || "Job failed", job.errorCode ?? undefined);
@@ -737,64 +779,10 @@ class JobQueueWorker {
737
779
  this.activeJobAbortControllers.delete(jobId);
738
780
  }
739
781
  storageToClass(details) {
740
- const toDate = (date) => {
741
- if (!date)
742
- return null;
743
- const d = new Date(date);
744
- return isNaN(d.getTime()) ? null : d;
745
- };
746
- return new this.jobClass({
747
- id: details.id,
748
- jobRunId: details.job_run_id,
749
- queueName: details.queue,
750
- fingerprint: details.fingerprint,
751
- input: details.input,
752
- output: details.output,
753
- runAfter: toDate(details.run_after),
754
- createdAt: toDate(details.created_at),
755
- deadlineAt: toDate(details.deadline_at),
756
- lastRanAt: toDate(details.last_ran_at),
757
- completedAt: toDate(details.completed_at),
758
- progress: details.progress || 0,
759
- progressMessage: details.progress_message || "",
760
- progressDetails: details.progress_details ?? null,
761
- status: details.status,
762
- error: details.error ?? null,
763
- errorCode: details.error_code ?? null,
764
- runAttempts: details.run_attempts ?? 0,
765
- maxRetries: details.max_retries ?? 10,
766
- workerId: details.worker_id ?? null
767
- });
782
+ return storageToClass(details, this.jobClass);
768
783
  }
769
784
  classToStorage(job) {
770
- const dateToISOString = (date) => {
771
- if (!date)
772
- return null;
773
- return isNaN(date.getTime()) ? null : date.toISOString();
774
- };
775
- const now = new Date().toISOString();
776
- return {
777
- id: job.id,
778
- job_run_id: job.jobRunId,
779
- queue: job.queueName || this.queueName,
780
- fingerprint: job.fingerprint,
781
- input: job.input,
782
- status: job.status,
783
- output: job.output ?? null,
784
- error: job.error === null ? null : String(job.error),
785
- error_code: job.errorCode || null,
786
- run_attempts: job.runAttempts ?? 0,
787
- max_retries: job.maxRetries ?? 10,
788
- run_after: dateToISOString(job.runAfter) ?? now,
789
- created_at: dateToISOString(job.createdAt) ?? now,
790
- deadline_at: dateToISOString(job.deadlineAt),
791
- last_ran_at: dateToISOString(job.lastRanAt),
792
- completed_at: dateToISOString(job.completedAt),
793
- progress: job.progress ?? 0,
794
- progress_message: job.progressMessage ?? "",
795
- progress_details: job.progressDetails ?? null,
796
- worker_id: job.workerId ?? null
797
- };
785
+ return classToStorage(job, this.queueName);
798
786
  }
799
787
  }
800
788
 
@@ -1038,64 +1026,10 @@ class JobQueueServer {
1038
1026
  }
1039
1027
  }
1040
1028
  storageToClass(details) {
1041
- const toDate = (date) => {
1042
- if (!date)
1043
- return null;
1044
- const d = new Date(date);
1045
- return isNaN(d.getTime()) ? null : d;
1046
- };
1047
- return new this.jobClass({
1048
- id: details.id,
1049
- jobRunId: details.job_run_id,
1050
- queueName: details.queue,
1051
- fingerprint: details.fingerprint,
1052
- input: details.input,
1053
- output: details.output,
1054
- runAfter: toDate(details.run_after),
1055
- createdAt: toDate(details.created_at),
1056
- deadlineAt: toDate(details.deadline_at),
1057
- lastRanAt: toDate(details.last_ran_at),
1058
- completedAt: toDate(details.completed_at),
1059
- progress: details.progress || 0,
1060
- progressMessage: details.progress_message || "",
1061
- progressDetails: details.progress_details ?? null,
1062
- status: details.status,
1063
- error: details.error ?? null,
1064
- errorCode: details.error_code ?? null,
1065
- runAttempts: details.run_attempts ?? 0,
1066
- maxRetries: details.max_retries ?? 10,
1067
- workerId: details.worker_id ?? null
1068
- });
1029
+ return storageToClass(details, this.jobClass);
1069
1030
  }
1070
1031
  classToStorage(job) {
1071
- const dateToISOString = (date) => {
1072
- if (!date)
1073
- return null;
1074
- return isNaN(date.getTime()) ? null : date.toISOString();
1075
- };
1076
- const now = new Date().toISOString();
1077
- return {
1078
- id: job.id,
1079
- job_run_id: job.jobRunId,
1080
- queue: job.queueName || this.queueName,
1081
- fingerprint: job.fingerprint,
1082
- input: job.input,
1083
- status: job.status,
1084
- output: job.output ?? null,
1085
- error: job.error === null ? null : String(job.error),
1086
- error_code: job.errorCode || null,
1087
- run_attempts: job.runAttempts ?? 0,
1088
- max_retries: job.maxRetries ?? 10,
1089
- run_after: dateToISOString(job.runAfter) ?? now,
1090
- created_at: dateToISOString(job.createdAt) ?? now,
1091
- deadline_at: dateToISOString(job.deadlineAt),
1092
- last_ran_at: dateToISOString(job.lastRanAt),
1093
- completed_at: dateToISOString(job.completedAt),
1094
- progress: job.progress ?? 0,
1095
- progress_message: job.progressMessage ?? "",
1096
- progress_details: job.progressDetails ?? null,
1097
- worker_id: job.workerId ?? null
1098
- };
1032
+ return classToStorage(job, this.queueName);
1099
1033
  }
1100
1034
  getWorkerIds() {
1101
1035
  return this.workers.map((worker) => worker.workerId);
@@ -1378,6 +1312,8 @@ class RateLimiter {
1378
1312
  }
1379
1313
  }
1380
1314
  export {
1315
+ storageToClass,
1316
+ classToStorage,
1381
1317
  RetryableJobError,
1382
1318
  RateLimiter,
1383
1319
  PermanentJobError,
@@ -1401,4 +1337,4 @@ export {
1401
1337
  AbortSignalJobError
1402
1338
  };
1403
1339
 
1404
- //# debugId=CD9E48A8E518D1E664756E2164756E21
1340
+ //# debugId=7F9687AF808794FF64756E2164756E21