Repository navigation
Expand file tree
/
Copy pathindex.ts
More file actions
1314 lines (1183 loc) · 52.7 KB
/
Copy pathindex.ts
File metadata and controls
1314 lines (1183 loc) · 52.7 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { AdminForthPlugin, Filters, Sorts } from "adminforth";
import type { IAdminForth, IHttpServer, AdminForthResource, AdminUser, AdminForthComponentDeclarationFull } from "adminforth";
import type { PluginOptions } from './types.js';
import { afLogger } from "adminforth";
import pLimit from 'p-limit';
import { Level } from 'level';
import fs from 'fs/promises';
import { Mutex } from 'async-mutex';
import { z } from "zod";
const jobIdBodySchema = z.object({
jobId: z.union([z.string(), z.number()]),
}).strict();
const getTasksBodySchema = z.object({
jobId: z.union([z.string(), z.number()]),
limit: z.number().int().positive().max(100),
offset: z.number().int().nonnegative(),
fieldsToReturn: z.array(z.string()).optional(),
}).strict();
type TaskStatus = 'SCHEDULED' | 'IN_PROGRESS' | 'DONE' | 'FAILED' | 'DELETED';
type JobStatus = 'QUEUED' | 'IN_PROGRESS' | 'DONE' | 'DONE_WITH_ERRORS' | 'CANCELLED';
const TERMINAL_JOB_STATUSES: JobStatus[] = ['DONE', 'DONE_WITH_ERRORS', 'CANCELLED'];
const DEFAULT_PARALLEL_LIMIT = 3;
const DEFAULT_CONCURRENCY_LIMIT = 1;
const LEVEL_DB_IDLE_CLOSE_MS = 1000 * 60 * 60 * 4; // 4 hours
type setStateFieldParams = {
(fieldName: string, value: any): Promise<void>;
(state: Record<string, any>): Promise<void>;
};
type getStateFieldParams = {
(fieldName: string): Promise<any>;
(): Promise<Record<string, any>>;
};
type getStateParams = () => Promise<Record<string, any>>;
type taskHandlerType = ( { jobId, setTaskStateField, getTaskStateField, getState }: { jobId: string; setTaskStateField: setStateFieldParams; getTaskStateField: getStateFieldParams; getState: getStateParams } ) => Promise<void>;
type allTasksDoneStatusType = {
jobId: string;
failedTasks: number;
succeededTasks: number;
};
type beforeJobFinishStatusType = allTasksDoneStatusType & {
finishAttemptNumber: number;
};
type onAllTasksDoneType = (status: allTasksDoneStatusType) => Promise<void> | void;
type beforeJobFinishType = (status: beforeJobFinishStatusType) => Promise<void> | void;
type taskType = {
skip?: boolean;
state: Record<string, any>;
}
type indexedTaskType = taskType & {
taskIndex: number;
}
type startNewJobOptions = {
/**
* When true the job is always created in QUEUED status, even when its queue has a free concurrency slot.
* Without it the job is created in QUEUED status only when the queue is busy.
*/
queued?: boolean;
/**
* When false a job which landed in QUEUED status stays there until something else drains the queue
* (startNextQueuedJob, a finishing job of the same queue, or an application restart). Default: true.
*/
autoStart?: boolean;
}
/**
* Everything needed to run the tasks of a single job. Kept as one object so it can be passed through the
* recursive task processing without a long positional argument list.
*/
type jobRunContext = {
jobHandlerName: string;
handleTask: taskHandlerType;
parrallelLimit: number;
onAllTasksDone?: onAllTasksDoneType;
beforeJobFinish?: beforeJobFinishType;
}
function encodeStateFieldName(fieldName: string): string {
return encodeURIComponent(fieldName);
}
export default class BackgroundJobsPlugin extends AdminForthPlugin {
options: PluginOptions;
private taskHandlers: Record<string, taskHandlerType> = {};
private onAllTasksDoneHandlers: Partial<Record<string, onAllTasksDoneType>> = {};
private beforeJobFinishHandlers: Partial<Record<string, beforeJobFinishType>> = {};
private jobCustomComponents: Record<string, AdminForthComponentDeclarationFull> = {};
private jobParallelLimits: Record<string, number> = {};
private jobConcurrencyLimits: Record<string, number> = {};
private levelDbInstances: Record<string, Level> = {};
private levelDbJobIds = new WeakMap<Level, string>();
private levelDbLastAccessAt: Record<string, number> = {};
private levelDbCloseTimers: Record<string, ReturnType<typeof setTimeout>> = {};
private jobStateMutexes: Record<string, Mutex> = {};
private jobQueueMutexes: Record<string, Mutex> = {};
private jobTasksMutexes: Record<string, Mutex> = {};
private addTasksToRunningJob: Record<string, (firstTaskIndex: number, tasks: taskType[]) => void> = {};
private deprecatedWarningsShown = new Set<string>();
constructor(options: PluginOptions) {
super(options, import.meta.url);
this.options = options;
this.shouldHaveSingleInstancePerWholeApp = () => true;
}
private getResourcePk(): string {
const resourcePk = this.resourceConfig.columns.find(c => c.primaryKey)?.name;
return resourcePk;
}
private getResourceId(): string {
return this.resourceConfig.resourceId;
}
async modifyResourceConfig(adminforth: IAdminForth, resourceConfig: AdminForthResource) {
super.modifyResourceConfig(adminforth, resourceConfig);
if (!adminforth.config.customization?.globalInjections?.header) {
adminforth.config.customization.globalInjections.header = [];
}
(adminforth.config.customization.globalInjections.header).push({
file: this.componentPath('NavbarJobs.vue'),
meta: {
pluginInstanceId: this.pluginInstanceId,
}
});
// Global API injection: exposes OpenJobInfoPopup(jobId) to open job details from anywhere
(adminforth.config.customization.globalInjections.header).push({
file: this.componentPath('GlobalJobApi.vue'),
meta: {
pluginInstanceId: this.pluginInstanceId,
}
});
if (!this.adminforth.config.componentsToExplicitRegister) {
this.adminforth.config.componentsToExplicitRegister = [];
}
this.adminforth.config.componentsToExplicitRegister.push(
{
file: this.componentPath('StateToIcon.vue')
}
);
if (!this.resourceConfig.hooks) {
this.resourceConfig.hooks = {};
}
if (!this.resourceConfig.hooks.delete) {
this.resourceConfig.hooks.delete = {};
}
if (!this.resourceConfig.hooks.delete.beforeSave) {
this.resourceConfig.hooks.delete.beforeSave = [];
}
this.resourceConfig.hooks.delete.beforeSave.push(async ({record, recordId}: {record: any, recordId: any}) => {
const levelDbPath = `${this.options.levelDbPath || './background-jobs-dbs/'}job_${recordId}`;
const jobLevelDb = this.levelDbInstances[recordId];
//close level db instance if it's open and delete the level db folder for the job
if (jobLevelDb) {
await jobLevelDb.close();
this.forgetLevelDb(recordId);
}
// cleanup per-job mutexes as well
delete this.jobTasksMutexes[recordId];
//delete level db folder for the job
await fs.rm(levelDbPath, {
recursive: true,
force: true,
});
return {ok: true};
})
}
private cleanupJobMutexIfTerminalStatus(jobId: string, status: JobStatus) {
// Keep mutex while job is active to preserve atomicity between concurrent tasks.
if (TERMINAL_JOB_STATUSES.includes(status)) {
// safe to drop even while held: the terminal status is already stored, so a caller who creates a new
// mutex for this job reads that status and rejects the new tasks
delete this.jobTasksMutexes[jobId];
}
}
private checkIfFieldInResource(resourceConfig: AdminForthResource, fieldName: string, fieldString?: string) {
if (!fieldName) {
throw new Error(`Field name for ${fieldString} is not provided. Please check your plugin options.`);
}
const fieldInConfig = resourceConfig.columns.find(f => f.name === fieldName);
if (!fieldInConfig) {
throw new Error(`Field ${fieldName} not found in resource config. Please check your plugin options.`);
}
}
private async createLevelDbTaskRecord(levelDb: Level, taskId: string, initialState: Record<string, any>) {
//create record in level db with task id as key and initial state as value and status SCHEDULED
this.touchLevelDb(levelDb);
await levelDb.put(taskId, JSON.stringify({ state: initialState, status: 'SCHEDULED' }));
}
private async setLevelDbTaskStateField(levelDb: Level, taskId: string, state: Record<string, any>) {
//update record in level db with task id as key and new state as value
this.touchLevelDb(levelDb);
const status = await this.getLevelDbTaskStatusField(levelDb, taskId);
await levelDb.del(taskId);
await levelDb.put(taskId, JSON.stringify({ state, status }));
}
private async setLevelDbTaskStatusField(levelDb: Level, taskId: string, status: TaskStatus) {
this.touchLevelDb(levelDb);
const state = await this.getLevelDbTaskStateField(levelDb, taskId);
await levelDb.del(taskId);
await levelDb.put(taskId, JSON.stringify({ state, status }));
}
private async getLevelDbTaskStateField(levelDb: Level, taskId: string): Promise<Record<string, any>> {
//get record from level db with task id as key and return the value of the key in the state
this.touchLevelDb(levelDb);
const state = await levelDb.get(taskId);
if (state) {
const parsedState = JSON.parse(state);
return parsedState.state;
}
return Promise.resolve(null);
}
private async getLevelDbTaskStatusField(levelDb: Level, taskId: string): Promise<TaskStatus> {
this.touchLevelDb(levelDb);
const state = await levelDb.get(taskId);
if (state) {
const parsedState = JSON.parse(state);
return parsedState.status;
}
return Promise.resolve(null);
}
private async getTotalTasksInLevelDb(levelDb: Level): Promise<number> {
this.touchLevelDb(levelDb);
const count = await levelDb.get('_meta:count');
return count ? parseInt(count, 10) : 0;
}
private async getAllTasksDoneStatus(levelDb: Level): Promise<Omit<allTasksDoneStatusType, 'jobId'>> {
const totalTasks = await this.getTotalTasksInLevelDb(levelDb);
let failedTasks = 0;
let succeededTasks = 0;
for (let taskIndex = 0; taskIndex < totalTasks; taskIndex++) {
const status = await this.getLevelDbTaskStatusField(levelDb, taskIndex.toString());
if (status === 'FAILED') {
failedTasks++;
} else if (status === 'DONE') {
succeededTasks++;
}
}
return { failedTasks, succeededTasks };
}
private async getLevelDbForTheJob(jobId: string): Promise<Level> {
const levelDbPath = `${this.options.levelDbPath || './background-jobs-dbs/'}job_${jobId}`;
let jobLevelDb: Level;
if (this.levelDbInstances[jobId]) {
jobLevelDb = this.levelDbInstances[jobId];
} else {
try {
jobLevelDb = new Level(levelDbPath, { valueEncoding: 'json' });
this.levelDbInstances[jobId] = jobLevelDb;
this.levelDbJobIds.set(jobLevelDb, jobId);
} catch (error) {
throw new Error(`Failed to access task storage for job with id ${jobId}.`);
}
}
this.touchLevelDb(jobLevelDb);
this.scheduleLevelDbClose(jobId);
return jobLevelDb;
}
/**
* Marks the LevelDB of a job as used right now, so the idle close timer does not close a db which is
* still being read/written (for example by a job running longer than the idle window).
*/
private touchLevelDb(levelDb: Level) {
const jobId = this.levelDbJobIds.get(levelDb);
if (jobId) {
this.levelDbLastAccessAt[jobId] = Date.now();
}
}
private scheduleLevelDbClose(jobId: string, delay: number = LEVEL_DB_IDLE_CLOSE_MS) {
if (this.levelDbCloseTimers[jobId]) {
clearTimeout(this.levelDbCloseTimers[jobId]);
}
this.levelDbCloseTimers[jobId] = setTimeout(() => { this.closeLevelDbIfIdle(jobId); }, delay);
}
private async closeLevelDbIfIdle(jobId: string) {
const jobLevelDb = this.levelDbInstances[jobId];
if (!jobLevelDb) {
this.forgetLevelDb(jobId);
return;
}
const idleFor = Date.now() - (this.levelDbLastAccessAt[jobId] ?? 0);
if (idleFor < LEVEL_DB_IDLE_CLOSE_MS) {
// db was used after this timer was scheduled, so wait for the rest of the idle window
this.scheduleLevelDbClose(jobId, LEVEL_DB_IDLE_CLOSE_MS - idleFor);
return;
}
try {
await jobLevelDb.close();
} catch (error) {
afLogger.error(`Failed to close LevelDB for job ${jobId}: ${error instanceof Error ? error.message : String(error)}`);
// keep the instance and try again after another idle window
this.scheduleLevelDbClose(jobId);
return;
}
this.forgetLevelDb(jobId);
}
private forgetLevelDb(jobId: string) {
if (this.levelDbCloseTimers[jobId]) {
clearTimeout(this.levelDbCloseTimers[jobId]);
}
delete this.levelDbCloseTimers[jobId];
delete this.levelDbLastAccessAt[jobId];
delete this.levelDbInstances[jobId];
}
private publishJobStateField(jobId: string, fieldName: string, value: any) {
this.adminforth.websocket.publish(`/background-jobs-state-update/${jobId}/${encodeStateFieldName(fieldName)}`, {
jobId,
fieldName,
value,
});
}
private publishJobUpdate(data: Record<string, any>) {
this.adminforth.websocket.publish('/background-jobs-job-update', data);
}
private async getJobById(jobId: string | number): Promise<Record<string, any> | null> {
return this.adminforth.resource(this.getResourceId()).get(Filters.EQ(this.getResourcePk(), jobId));
}
private publishTaskStateFields(jobId: string, taskIndex: number, state: Record<string, any>) {
for (const [fieldName, value] of Object.entries(state)) {
this.adminforth.websocket.publish(`/background-jobs-task-state-update/${jobId}/${encodeStateFieldName(fieldName)}`, {
jobId,
taskIndex,
fieldName,
value,
});
}
}
private warnDeprecatedOnce(key: string, message: string) {
if (this.deprecatedWarningsShown.has(key)) {
return;
}
this.deprecatedWarningsShown.add(key);
afLogger.warn(message);
}
private async triggerOnAllTasksDone(onAllTasksDone: onAllTasksDoneType | undefined, levelDb: Level, jobId: string) {
if (!onAllTasksDone) {
return;
}
try {
const status = await this.getAllTasksDoneStatus(levelDb);
await onAllTasksDone({ jobId, ...status });
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
afLogger.error(`Error in onAllTasksDone callback for job ${jobId}: ${errorMessage}`);
}
}
private async triggerBeforeJobFinish(beforeJobFinish: beforeJobFinishType | undefined, levelDb: Level, jobId: string, finishAttemptNumber: number) {
if (!beforeJobFinish) {
return;
}
try {
const status = await this.getAllTasksDoneStatus(levelDb);
await beforeJobFinish({ jobId, ...status, finishAttemptNumber });
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
afLogger.error(`Error in beforeJobFinish callback for job ${jobId}: ${errorMessage}`);
throw error;
}
}
public registerTaskHandler({ jobHandlerName, handler, parallelLimit = DEFAULT_PARALLEL_LIMIT, concurrencyLimit = DEFAULT_CONCURRENCY_LIMIT, onAllTasksDone, beforeJobFinish,
}:{jobHandlerName: string, handler: taskHandlerType, parallelLimit?: number, concurrencyLimit?: number, onAllTasksDone?: onAllTasksDoneType, beforeJobFinish?: beforeJobFinishType}) {
if (!Number.isInteger(concurrencyLimit) || concurrencyLimit < 1) {
throw new Error(`concurrencyLimit for jobHandler ${jobHandlerName} must be an integer greater than 0, got ${concurrencyLimit}.`);
}
//register the handler in a map with jobHandlerName as key and handler as value
this.taskHandlers[jobHandlerName] = handler;
this.jobParallelLimits[jobHandlerName] = parallelLimit;
this.jobConcurrencyLimits[jobHandlerName] = concurrencyLimit;
if (onAllTasksDone) {
this.onAllTasksDoneHandlers[jobHandlerName] = onAllTasksDone;
} else {
delete this.onAllTasksDoneHandlers[jobHandlerName];
}
if (beforeJobFinish) {
this.beforeJobFinishHandlers[jobHandlerName] = beforeJobFinish;
} else {
delete this.beforeJobFinishHandlers[jobHandlerName];
}
}
public registerTaskDetailsComponent({
jobHandlerName,
component,
}:{jobHandlerName: string, component: AdminForthComponentDeclarationFull}) {
this.jobCustomComponents[jobHandlerName] = component;
}
private buildJobRunContext(jobHandlerName: string): jobRunContext | null {
const handleTask: taskHandlerType = this.taskHandlers[jobHandlerName];
if (!handleTask) {
return null;
}
return {
jobHandlerName,
handleTask,
parrallelLimit: this.jobParallelLimits[jobHandlerName] || DEFAULT_PARALLEL_LIMIT,
onAllTasksDone: this.onAllTasksDoneHandlers[jobHandlerName],
beforeJobFinish: this.beforeJobFinishHandlers[jobHandlerName],
};
}
private async canStartJobImmediately(jobHandlerName: string): Promise<boolean> {
const concurrencyLimit = this.jobConcurrencyLimits[jobHandlerName] || DEFAULT_CONCURRENCY_LIMIT;
const runningJobsCount = await this.getRunningJobsCount(jobHandlerName);
if (runningJobsCount >= concurrencyLimit) {
return false;
}
const [queuedJob] = await this.listJobsByStatus(jobHandlerName, 'QUEUED', 1);
return !queuedJob;
}
public async startNewJob(
jobName: string,
adminUser: AdminUser | null,
tasks: taskType[],
jobHandlerName: string,
initialState: Record<string, any> = {},
options: startNewJobOptions = {},
): Promise<string> {
const jobRunContext = this.buildJobRunContext(jobHandlerName);
if (!jobRunContext) {
throw new Error(`No handler registered for jobHandler ${jobHandlerName}. Please register a handler using the registerTaskHandler method before starting a job with this jobHandler.`);
}
const customComponent = this.jobCustomComponents[jobHandlerName];
const { parrallelLimit } = jobRunContext;
const startedBy = adminUser?.pk ?? null;
const { createdRecord, initialStatus, jobLevelDb } = await this.getQueueMutex(jobHandlerName).runExclusive(async () => {
const initialStatus: JobStatus = !options.queued && await this.canStartJobImmediately(jobHandlerName)
? 'IN_PROGRESS'
: 'QUEUED';
//create a record for the job in the database with status in progress (or queued, when the queue is busy)
const objectToSave = {
[this.options.nameField]: jobName,
[this.options.startedByField]: startedBy,
[this.options.progressField]: 0,
[this.options.statusField]: initialStatus,
[this.options.jobHandlerField]: jobHandlerName,
[this.options.stateField]: initialState
}
const creationResult = await this.adminforth.resource(this.getResourceId()).create(objectToSave);
if (creationResult.ok !== true) {
throw new Error(`Failed to create a record for the job. Error: ${creationResult.error}`);
}
const createdRecord = creationResult.createdRecord as Record<string, any>;
// tasks are stored before the queue mutex is released, otherwise startNextQueuedJob could take the QUEUED job
// while it has no tasks yet and finish it right away, leaving the tasks written after that unprocessed
//create a level db instance for the job with name as jobId
const jobLevelDb = await this.getLevelDbForTheJob(createdRecord[this.getResourcePk()]);
await jobLevelDb.put('_meta:count', `${tasks.length}`);
const limit2 = pLimit(parrallelLimit);
const createTaskRecordsPromises = tasks.map((task, index) => {
return limit2(() => this.createLevelDbTaskRecord(jobLevelDb, index.toString(), task.state));
});
await Promise.all(createTaskRecordsPromises);
return { createdRecord, initialStatus, jobLevelDb };
});
const jobId = createdRecord[this.getResourcePk()];
this.publishJobUpdate({
jobId,
status: initialStatus,
name: jobName,
progress: 0,
createdAt: createdRecord[this.options.createdAtField],
customComponent,
});
if (initialStatus === 'IN_PROGRESS') {
this.runProcessingTasks(tasks, jobLevelDb, jobId, jobRunContext).catch((error) => {
void this.handleJobProcessingFailure(jobId, jobHandlerName, error);
});
return jobId;
}
if (options.autoStart !== false) {
await this.startQueuedJobs(jobHandlerName);
}
return jobId;
}
public async queueNewJob(
jobName: string,
adminUser: AdminUser | null,
tasks: taskType[],
jobHandlerName: string,
initialState: Record<string, any> = {},
options: Omit<startNewJobOptions, 'queued'> = {},
): Promise<string> {
return this.startNewJob(jobName, adminUser, tasks, jobHandlerName, initialState, { ...options, queued: true });
}
private getQueueMutex(jobHandlerName: string): Mutex {
let mutex = this.jobQueueMutexes[jobHandlerName];
if (!mutex) {
mutex = new Mutex();
this.jobQueueMutexes[jobHandlerName] = mutex;
}
return mutex;
}
/**
* Taken by addNewTasksToExistingJob and by the last unfinished-task scan with the terminal status write of a job,
* so a task is either added before that scan and processed, or rejected because the job is already finished.
*/
private getJobTasksMutex(jobId: string): Mutex {
let mutex = this.jobTasksMutexes[jobId];
if (!mutex) {
mutex = new Mutex();
this.jobTasksMutexes[jobId] = mutex;
}
return mutex;
}
private async listJobsByStatus(
jobHandlerName: string,
status: JobStatus,
limit: number | null = null,
): Promise<Record<string, any>[]> {
return this.adminforth.resource(this.getResourceId()).list(
Filters.AND(
Filters.EQ(this.options.jobHandlerField, jobHandlerName),
Filters.EQ(this.options.statusField, status),
),
limit,
0,
Sorts.ASC(this.options.createdAtField),
);
}
private async getRunningJobsCount(jobHandlerName: string): Promise<number> {
const runningJobs = await this.listJobsByStatus(jobHandlerName, 'IN_PROGRESS');
return runningJobs.length;
}
public async startNextQueuedJob(jobHandlerName: string): Promise<string | null> {
return this.getQueueMutex(jobHandlerName).runExclusive(async () => {
const jobRunContext = this.buildJobRunContext(jobHandlerName);
if (!jobRunContext) {
afLogger.error(`No handler registered for jobHandler ${jobHandlerName}. Cannot start queued jobs for this jobHandler.`);
return null;
}
const concurrencyLimit = this.jobConcurrencyLimits[jobHandlerName] || DEFAULT_CONCURRENCY_LIMIT;
const runningJobsCount = await this.getRunningJobsCount(jobHandlerName);
if (runningJobsCount >= concurrencyLimit) {
return null;
}
const [oldestQueuedJob] = await this.listJobsByStatus(jobHandlerName, 'QUEUED', 1);
if (!oldestQueuedJob) {
return null;
}
const jobId = oldestQueuedJob[this.getResourcePk()];
const updateResult = await this.adminforth.resource(this.getResourceId()).update(jobId, {
[this.options.statusField]: 'IN_PROGRESS',
});
if (updateResult?.ok === false) {
afLogger.error(`Failed to start queued job ${jobId}: ${updateResult.error}`);
return null;
}
this.publishJobUpdate({
jobId,
status: 'IN_PROGRESS',
name: oldestQueuedJob[this.options.nameField],
progress: oldestQueuedJob[this.options.progressField],
createdAt: oldestQueuedJob[this.options.createdAtField],
customComponent: this.jobCustomComponents[jobHandlerName],
});
// not awaited on purpose: processing runs until the whole job is finished, while the queue mutex
// only has to cover taking the slot
this.runProcessingJobTasksFromStorage(oldestQueuedJob, jobRunContext).catch((error) => {
void this.handleJobProcessingFailure(
jobId,
jobHandlerName,
error,
);
});
return jobId;
});
}
public async startQueuedJobs(jobHandlerName: string): Promise<string[]> {
const startedJobIds: string[] = [];
while (true) {
const startedJobId = await this.startNextQueuedJob(jobHandlerName);
if (!startedJobId) {
return startedJobIds;
}
startedJobIds.push(startedJobId);
}
}
private async startQueuedJobsSafely(jobHandlerName: string) {
if (!jobHandlerName) {
return;
}
try {
await this.startQueuedJobs(jobHandlerName);
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
afLogger.error(`Failed to start queued jobs for jobHandler ${jobHandlerName}: ${errorMessage}`);
}
}
public async addNewTasksToExistingJob(
jobId: string,
tasks: taskType[],
) {
await this.getJobTasksMutex(jobId).runExclusive(async () => {
const jobRecord = await this.adminforth.resource(this.getResourceId()).get(Filters.EQ(this.getResourcePk(), jobId));
if (!jobRecord) {
throw new Error(`Job with id ${jobId} not found.`);
}
const jobStatus = jobRecord[this.options.statusField];
if (jobStatus !== 'IN_PROGRESS' && jobStatus !== 'QUEUED') {
throw new Error(`Cannot add tasks to a job with status ${jobStatus}. Only jobs with status IN_PROGRESS or QUEUED can be added new tasks.`);
}
const jobLevelDb = await this.getLevelDbForTheJob(jobId);
const currentTotalTasks = await this.getTotalTasksInLevelDb(jobLevelDb);
const newTotalTasks = currentTotalTasks + tasks.length;
await jobLevelDb.put('_meta:count', `${newTotalTasks}`);
const createTaskRecordsPromises = tasks.map((task, index) => {
return this.createLevelDbTaskRecord(jobLevelDb, (currentTotalTasks + index).toString(), task.state);
});
await Promise.all(createTaskRecordsPromises);
// a running job takes the new tasks into free slots of its parallel limit right away, otherwise they are
// picked up from storage when the job starts or rescans its unfinished tasks
this.addTasksToRunningJob[jobId]?.(currentTotalTasks, tasks);
});
}
public async deleteTasksFromExistingJob(
jobId: string,
taskIndex: number,
): Promise<void> {
if (taskIndex < 0) {
throw new Error(`Invalid task index ${taskIndex}.`);
}
const jobRecord = await this.adminforth.resource(this.getResourceId()).get(Filters.EQ(this.getResourcePk(), jobId));
if (!jobRecord) {
throw new Error(`Job with id ${jobId} not found.`);
}
const jobStatus = jobRecord[this.options.statusField];
if (jobStatus !== 'IN_PROGRESS' && jobStatus !== 'QUEUED') {
throw new Error(`Cannot delete tasks from a job with status ${jobStatus}. Only jobs with status IN_PROGRESS or QUEUED can have tasks deleted.`);
}
const jobLevelDb = await this.getLevelDbForTheJob(jobId);
const currentTotalTasks = await this.getTotalTasksInLevelDb(jobLevelDb);
if (taskIndex >= currentTotalTasks) {
throw new Error(`Invalid task index ${taskIndex}.`);
}
const taskStatus = await this.getLevelDbTaskStatusField(jobLevelDb, taskIndex.toString());
if (taskStatus === 'IN_PROGRESS') {
throw new Error(`Cannot delete task ${taskIndex} because it is already in progress.`);
}
// running workers address their task by its index, so the task is only marked as deleted and keeps its
// index, removing it would shift the following tasks under the workers which are processing them
await this.setLevelDbTaskStatusField(jobLevelDb, taskIndex.toString(), 'DELETED');
this.adminforth.websocket.publish(`/background-jobs-task-update/${jobId}`, { taskIndex, status: 'DELETED' });
}
private async getUnfinishedTasksFromLevelDb(levelDb: Level): Promise<indexedTaskType[]> {
const totalTasks = await this.getTotalTasksInLevelDb(levelDb);
const unfinishedTasks: indexedTaskType[] = [];
for (let taskIndex = 0; taskIndex < totalTasks; taskIndex++) {
const status = await this.getLevelDbTaskStatusField(levelDb, taskIndex.toString());
if (status === 'IN_PROGRESS' || status === 'SCHEDULED') {
const state = await this.getLevelDbTaskStateField(levelDb, taskIndex.toString());
unfinishedTasks.push({ state, taskIndex });
}
}
return unfinishedTasks;
}
private async buildTasksToReprocess(levelDb: Level, unfinishedTasks: indexedTaskType[]): Promise<taskType[]> {
const totalTasks = await this.getTotalTasksInLevelDb(levelDb);
const tasksToReprocess: taskType[] = Array.from(
{ length: totalTasks },
() => ({ skip: true, state: {} }),
);
for (const { taskIndex, state } of unfinishedTasks) {
tasksToReprocess[taskIndex] = { state };
}
return tasksToReprocess;
}
private async runProcessingTasks(
tasks: taskType[],
jobLevelDb: Level,
jobId: string,
jobRunContext: jobRunContext,
finishAttemptNumber = 0,
) {
const { jobHandlerName, handleTask, parrallelLimit, onAllTasksDone, beforeJobFinish } = jobRunContext;
let totalTasks = tasks.length;
let completedTasks = 0;
let lastJobStatus = 'IN_PROGRESS';
const progressMutex = new Mutex();
const finishTask = (wasTaskSkipped: boolean = false) => progressMutex.runExclusive(async () => {
completedTasks = await this.handleFinishTask(completedTasks, totalTasks, jobId, wasTaskSkipped);
});
const taskHandler = async ( taskIndex: number, task: taskType ) => {
totalTasks = await this.getTotalTasksInLevelDb(jobLevelDb);
if (task.skip) {
await finishTask(true);
return;
}
if (lastJobStatus === 'CANCELLED') {
afLogger.debug(`Job ${jobId} was cancelled. Skipping task ${taskIndex}.`);
return;
}
const currentJobStatus = await this.getLastJobStatus(jobId);
if (currentJobStatus === 'CANCELLED') {
lastJobStatus = currentJobStatus;
afLogger.debug(`Job ${jobId} was cancelled. Skipping task ${taskIndex}.`);
return;
}
// the task can be deleted while it waits for a free slot
const taskStatus = await this.getLevelDbTaskStatusField(jobLevelDb, taskIndex.toString());
if (taskStatus === 'DELETED') {
afLogger.debug(`Task ${taskIndex} of job ${jobId} was deleted. Skipping processing.`);
// a deleted task stays in the total task count, so it has to be counted as finished for the progress
await finishTask();
return;
}
const getState = async () => {
return await this.getLevelDbTaskStateField(jobLevelDb, taskIndex.toString());
}
const setTaskStateField: setStateFieldParams = async (fieldNameOrState: string | Record<string, any>, value?: any) => {
if (typeof fieldNameOrState === 'string') {
const state = await getState();
const updatedState = {
...state,
[fieldNameOrState]: value,
};
await this.setLevelDbTaskStateField(jobLevelDb, taskIndex.toString(), updatedState);
this.publishTaskStateFields(jobId, taskIndex, { [fieldNameOrState]: value });
return;
}
this.warnDeprecatedOnce(
'setTaskStateField-object',
'BackgroundJobsPlugin: setTaskStateField(stateObject) is deprecated and will be removed soon. Use setTaskStateField(fieldName: string, value: any) instead. Use getState() when you need the full task state.',
);
await this.setLevelDbTaskStateField(jobLevelDb, taskIndex.toString(), fieldNameOrState);
this.publishTaskStateFields(jobId, taskIndex, fieldNameOrState);
}
const getTaskStateField: getStateFieldParams = async (fieldName?: string) => {
const state = await getState();
if (typeof fieldName === 'string') {
return state[fieldName];
}
this.warnDeprecatedOnce(
'getTaskStateField-no-args',
'BackgroundJobsPlugin: getTaskStateField() without a field name is deprecated and will be removed soon. Use getTaskStateField(fieldName: string) for one field, or getState() for the full task state.',
);
return state;
}
await this.setLevelDbTaskStatusField(jobLevelDb, taskIndex.toString(), 'IN_PROGRESS');
this.adminforth.websocket.publish(`/background-jobs-task-update/${jobId}`, { taskIndex, status: "IN_PROGRESS" });
//handling the task
try {
await handleTask({ jobId, setTaskStateField, getTaskStateField, getState });
//Set task status to completed in level db
await this.setLevelDbTaskStatusField(jobLevelDb, taskIndex.toString(), 'DONE');
this.adminforth.websocket.publish(`/background-jobs-task-update/${jobId}`, { taskIndex, status: "DONE" });
} catch (error) {
const errorMessage = error?.message || 'Unknown error';
afLogger.error(`Error in handling task ${taskIndex} of job ${jobId}: ${errorMessage}`, );
await this.setJobStateField(jobId, 'error', errorMessage);
// persist the error inside the task state so it is returned when tasks are fetched later
const taskState = await this.getLevelDbTaskStateField(jobLevelDb, taskIndex.toString());
await this.setLevelDbTaskStateField(jobLevelDb, taskIndex.toString(), { ...taskState, error: errorMessage });
await this.setLevelDbTaskStatusField(jobLevelDb, taskIndex.toString(), 'FAILED');
this.adminforth.websocket.publish(`/background-jobs-task-update/${jobId}`, { taskIndex, status: "FAILED", error: errorMessage });
return;
} finally {
//Update progress
const currentJobStatus = await this.getLastJobStatus(jobId);
if (currentJobStatus === 'CANCELLED') {
lastJobStatus = currentJobStatus;
afLogger.debug(`Job ${jobId} was cancelled during processing of task ${taskIndex}. Progress will not be updated.`);
return;
}
await finishTask();
}
}
const limit = pLimit(parrallelLimit);
let pendingTasks: Promise<void>[] = [];
const enqueueTask = (taskIndex: number, task: taskType) => {
pendingTasks.push(limit(() => taskHandler(taskIndex, task)));
};
tasks.forEach((task, taskIndex) => enqueueTask(taskIndex, task));
this.addTasksToRunningJob[jobId] = (firstTaskIndex, newTasks) => {
newTasks.forEach((task, offset) => {
const taskIndex = firstTaskIndex + offset;
// a run built from storage after the task was saved already has it
if (taskIndex >= tasks.length) {
enqueueTask(taskIndex, task);
}
});
};
try {
// tasks enqueued while a batch runs form the next batch, so the loop ends only when nothing is left
while (pendingTasks.length > 0) {
const tasksBatch = pendingTasks;
pendingTasks = [];
await Promise.all(tasksBatch);
}
} catch (error) {
// tasks enqueued after the failed batch are not awaited anymore, so their rejections must not go unhandled
pendingTasks.forEach((pendingTask) => pendingTask.catch(() => {}));
throw error;
} finally {
delete this.addTasksToRunningJob[jobId];
}
if (lastJobStatus === 'CANCELLED') {
this.cleanupJobMutexIfTerminalStatus(jobId, 'CANCELLED');
await this.startQueuedJobsSafely(jobHandlerName);
return;
}
const jobStatusAfterExecution = await this.getLastJobStatus(jobId);
if (jobStatusAfterExecution === 'CANCELLED') {
this.cleanupJobMutexIfTerminalStatus(jobId, 'CANCELLED');
await this.startQueuedJobsSafely(jobHandlerName);
return;
}
const unfinishedTasks = await this.getUnfinishedTasksFromLevelDb(jobLevelDb);
if (unfinishedTasks.length > 0) {
const tasksToReprocess = await this.buildTasksToReprocess(jobLevelDb, unfinishedTasks);
await this.runProcessingTasks(tasksToReprocess, jobLevelDb, jobId, jobRunContext, finishAttemptNumber);
} else {
const nextFinishAttemptNumber = finishAttemptNumber + 1;
await this.triggerBeforeJobFinish(beforeJobFinish, jobLevelDb, jobId, nextFinishAttemptNumber);
const currentJobStatusAfterFinishCallback = await this.getLastJobStatus(jobId);
if (currentJobStatusAfterFinishCallback === 'CANCELLED') {
this.cleanupJobMutexIfTerminalStatus(jobId, 'CANCELLED');
await this.startQueuedJobsSafely(jobHandlerName);
return;
}
// the last scan and the terminal status write hold the job tasks mutex, so addNewTasksToExistingJob can not
// store a task between them which nobody would process
const { unfinishedTasksAfterFinishCallback, finalStatus } = await this.getJobTasksMutex(jobId).runExclusive(async () => {
const unfinishedTasksAfterFinishCallback = await this.getUnfinishedTasksFromLevelDb(jobLevelDb);
if (unfinishedTasksAfterFinishCallback.length > 0) {
return { unfinishedTasksAfterFinishCallback, finalStatus: null };
}
const allTasksDoneStatus = await this.getAllTasksDoneStatus(jobLevelDb);
const finalStatus: JobStatus = allTasksDoneStatus.failedTasks === 0 ? 'DONE' : 'DONE_WITH_ERRORS';
await this.adminforth.resource(this.getResourceId()).update(jobId, {
[this.options.statusField]: finalStatus,
[this.options.finishedAtField]: (new Date()).toISOString(),
})
return { unfinishedTasksAfterFinishCallback, finalStatus };
});
if (unfinishedTasksAfterFinishCallback.length > 0) {
const tasksToReprocess = await this.buildTasksToReprocess(jobLevelDb, unfinishedTasksAfterFinishCallback);
await this.runProcessingTasks(tasksToReprocess, jobLevelDb, jobId, jobRunContext, nextFinishAttemptNumber);
return;
}
if (finalStatus === 'DONE') {
this.publishJobUpdate({ jobId, status: 'DONE', finishedAt: (new Date()).toISOString() });
this.cleanupJobMutexIfTerminalStatus(jobId, 'DONE');
await this.triggerOnAllTasksDone(onAllTasksDone, jobLevelDb, jobId);
} else {
const jobError = await this.getJobStateField(jobId, 'error');
this.publishJobUpdate({ jobId, status: 'DONE_WITH_ERRORS', finishedAt: (new Date()).toISOString(), error: jobError });
this.cleanupJobMutexIfTerminalStatus(jobId, 'DONE_WITH_ERRORS');
await this.triggerOnAllTasksDone(onAllTasksDone, jobLevelDb, jobId);
}
await this.startQueuedJobsSafely(jobHandlerName);
}
}
private async getLastJobStatus(jobId: string): Promise<string> {
const currentJobRecord = await this.adminforth.resource(this.getResourceId()).get(Filters.EQ(this.getResourcePk(), jobId));
const currentJobStatus = currentJobRecord[this.options.statusField];
return currentJobStatus;
}
private async handleFinishTask(completedTasks: number, totalTasks: number, jobId: string, wasTaskSkipped: boolean) {
completedTasks++;
if (wasTaskSkipped) {
return completedTasks;
}
const progress = Math.round((completedTasks / totalTasks) * 100);
await this.adminforth.resource(this.getResourceId()).update(jobId, {
[this.options.progressField]: progress,
})
this.publishJobUpdate({ jobId, progress });
return completedTasks;
}
private async handleJobProcessingFailure(
jobId: string,
jobHandlerName: string,
error: unknown,
) {
const errorMessage = error instanceof Error ? error.message : String(error);
afLogger.error(`Error while processing tasks of job ${jobId}: ${errorMessage}`);
try {
const currentStatus = await this.getLastJobStatus(jobId);
if (TERMINAL_JOB_STATUSES.includes(currentStatus as JobStatus)) {
return;
}
await this.setJobStateField(jobId, 'error', errorMessage);
const finishedAt = new Date().toISOString();
await this.adminforth.resource(this.getResourceId()).update(jobId, {
[this.options.statusField]: 'DONE_WITH_ERRORS',
[this.options.finishedAtField]: finishedAt,
});
this.publishJobUpdate({ jobId, status: 'DONE_WITH_ERRORS', finishedAt, error: errorMessage });
this.cleanupJobMutexIfTerminalStatus(jobId, 'DONE_WITH_ERRORS');
await this.startQueuedJobsSafely(jobHandlerName);
} catch (failureHandlingError) {
const failureMessage = failureHandlingError instanceof Error ? failureHandlingError.message : String(failureHandlingError);
afLogger.error(`Failed to mark job ${jobId} as DONE_WITH_ERRORS: ${failureMessage}`);
}
}
private async runProcessingJobTasksFromStorage(
job: Record<string, any>,
knownJobRunContext?: jobRunContext,
) {
const jobId = job[this.getResourcePk()];
const jobLevelDb = await this.getLevelDbForTheJob(jobId);
const jobHandlerName = job[this.options.jobHandlerField];
const jobRunContext = knownJobRunContext || this.buildJobRunContext(jobHandlerName);
if (!jobRunContext) {
afLogger.error(`No handler registered for jobHandler ${jobHandlerName}. Cannot process unfinished tasks for job ${jobId}.`);