1
1
import { ok , type Ok } from '@stacksjs/error-handling'
2
2
import { log } from '@stacksjs/logging'
3
- import { Job } from '../../../orm/src/models/Job'
3
+ import { Job , type JobModel } from '../../../orm/src/models/Job'
4
4
import { runJob } from './job'
5
5
6
6
interface QueuePayload {
@@ -12,51 +12,62 @@ interface QueuePayload {
12
12
}
13
13
14
14
export async function processJobs ( queue : string | undefined ) : Promise < Ok < string , never > > {
15
- setInterval ( async ( ) => {
16
- await executeJobs ( queue )
17
- } , 1000 )
15
+ async function process ( ) {
16
+ try {
17
+ await executeJobs ( queue )
18
+ } catch ( error ) {
19
+ log . error ( 'Error processing jobs:' , error )
20
+ }
21
+
22
+ setTimeout ( process , 1000 )
23
+ }
24
+
25
+ process ( )
18
26
19
- return ok ( 'All jobs processed successfully!' )
27
+ return ok ( 'Job processing has started successfully!' )
20
28
}
21
29
22
30
async function executeJobs ( queue : string | undefined ) : Promise < void > {
23
- const jobs = await Job . when ( queue !== undefined , ( query : any ) => {
24
- return query . where ( 'queue' , queue )
25
- } ) . get ( )
31
+ const jobs = await Job . when ( queue !== undefined , ( query : any ) => query . where ( 'queue' , queue ) ) . get ( )
26
32
27
33
for ( const job of jobs ) {
28
- if ( job . payload ) {
29
- if ( job . available_at && job . available_at > timestampNow ( ) )
30
- return
31
-
32
- const payload : QueuePayload = JSON . parse ( job . payload )
33
- const currentAttempts = job . attempts || 0
34
-
35
- log . info ( `Running ${ payload . displayName } ` )
36
-
37
- await job . update ( { attempts : currentAttempts + 1 } )
38
-
39
- try {
40
- await runJob ( payload . name , {
41
- queue : job . queue ,
42
- payload : { } ,
43
- context : '' ,
44
- maxTries : payload . maxTries ,
45
- timeout : 60 ,
46
- } )
47
-
48
- await job . delete ( )
49
- }
50
- catch ( error ) {
51
- log . info ( `${ payload . displayName } failed` )
52
- log . error ( error )
53
- }
54
-
55
- log . info ( `Successfully ran ${ payload . displayName } ` )
34
+
35
+ if ( ! job . payload ) continue
36
+
37
+ if ( job . available_at && job . available_at > timestampNow ( ) ) continue
38
+
39
+ const payload : QueuePayload = JSON . parse ( job . payload )
40
+ const currentAttempts = job . attempts || 0
41
+
42
+ log . info ( `Running job: ${ payload . displayName } ` )
43
+
44
+ await updateJobAttempts ( job , currentAttempts )
45
+
46
+ try {
47
+ await runJob ( payload . name , {
48
+ queue : job . queue ,
49
+ payload : { } ,
50
+ context : '' ,
51
+ maxTries : payload . maxTries ,
52
+ timeout : 60 ,
53
+ } )
54
+
55
+ await job . delete ( )
56
+ log . info ( `Successfully ran job: ${ payload . displayName } ` )
57
+ } catch ( error ) {
58
+ log . error ( `Job failed: ${ payload . displayName } ` , error )
56
59
}
57
60
}
58
61
}
59
62
63
+ async function updateJobAttempts ( job : any , currentAttempts : number ) : Promise < void > {
64
+ try {
65
+ await job . update ( { attempts : currentAttempts + 1 } )
66
+ } catch ( error ) {
67
+ log . error ( 'Failed to update job attempts:' , error )
68
+ }
69
+ }
70
+
60
71
function timestampNow ( ) : number {
61
72
const now = Date . now ( )
62
73
return Math . floor ( now / 1000 )
0 commit comments