@@ -23,20 +23,14 @@ export abstract class Job {
2323 this . timeout = options . timeout ;
2424 }
2525
26- abstract run ( ) : any | Promise < any > ;
26+ abstract run ( ...args : any [ ] ) : any | Promise < any > ;
27+
28+ static config ( this : { new ( ...args : any [ ] ) : Job } , jobOptions : JobOptions ) {
29+ return new JobBuilder ( this ) . config ( jobOptions ) ;
30+ }
2731
2832 static enqueue ( this : { new ( ...args : any [ ] ) : Job } , ...args : any [ ] ) : JobData | Promise < JobData > {
29- const job = new this ( ...args ) ;
30- const backend = Sidequest . getBackend ( ) ;
31- const jobData : JobData = {
32- queue : job . queue ,
33- script : job . script ,
34- class : job . class ,
35- args : args ,
36- attempt : 0 ,
37- max_attempts : 5
38- }
39- return backend . insertJob ( jobData ) ;
33+ return new JobBuilder ( this ) . enqueue ( args ) ;
4034 }
4135}
4236
@@ -52,4 +46,36 @@ function buildPath() {
5246 }
5347
5448 throw new Error ( 'Could not determine the task path' ) ;
49+ }
50+
51+ class JobBuilder {
52+ JobClass : new ( ...args : any [ ] ) => Job ;
53+ job ?: Job ;
54+
55+ constructor ( JobClass : { new ( ...args : any [ ] ) : Job } ) {
56+ this . JobClass = JobClass ;
57+ }
58+
59+ config ( options : JobOptions ) {
60+ this . job = new this . JobClass ( options ) ;
61+ return this ;
62+ }
63+
64+ enqueue ( ...args : any [ ] ) {
65+ if ( ! this . job ) {
66+ this . job = new this . JobClass ( { queue : 'default' } ) ;
67+ }
68+
69+ const backend = Sidequest . getBackend ( ) ;
70+ const jobData : JobData = {
71+ queue : this . job . queue ,
72+ script : this . job . script ,
73+ class : this . job . class ,
74+ args : args ,
75+ attempt : 0 ,
76+ max_attempts : 5 ,
77+ timeout : this . job . timeout
78+ }
79+ return backend . insertJob ( jobData ) ;
80+ }
5581}
0 commit comments