@@ -26,61 +26,33 @@ export class QueueStack {
26
26
const jobsDir = path . jobsPath ( )
27
27
28
28
try {
29
- const files = await fs . readdir ( jobsDir )
30
-
31
- for ( const file of files ) {
32
- if ( file . endsWith ( '.ts' ) ) {
33
- const filePath = path . jobsPath ( file )
34
-
35
- // Await the loading of the job module
36
- const jobModule = await this . loadJobModule ( filePath )
37
-
38
- // Now you can safely access jobModule.default.rate
39
- const rate = jobModule . default ?. rate
40
-
41
- // if no rate or job is disabled, no need to schedule, skip
42
- if ( ! rate || jobModule . default ?. enabled === false )
43
- continue
44
-
45
- // Convert the rate to a Schedule object
46
- const schedule = Schedule . cron ( this . cronScheduleFromRate ( rate ) )
47
-
48
- const id = `QueueRule${ pascalCase ( file . replace ( '.ts' , '' ) ) } `
49
-
50
- // Perform operations with the jobModule.default as needed
51
- const rule = new Rule ( this . scope , id , {
52
- // schedule to run every second
53
- ruleName : `${ this . props . appName } -${ this . props . appEnv } -queue-rule-${ slug ( file . replace ( '.ts' , '' ) ) } ` ,
54
- schedule,
55
- } )
56
-
57
- rule . addTarget ( new EcsTask ( {
58
- cluster : this . props . cluster ,
59
- taskDefinition : this . props . taskDefinition ,
60
- containerOverrides : [
61
- {
62
- containerName : `${ this . props . appName } -${ this . props . appEnv } -api` ,
63
- environment : [
64
- {
65
- name : 'QUEUE_WORKER' ,
66
- value : 'true' ,
67
- } ,
68
- {
69
- name : 'JOB' ,
70
- value : file ,
71
- } ,
72
- ] ,
73
- } ,
74
- ] ,
75
-
76
- retryAttempts : jobModule . default . tries || 3 ,
77
-
78
- subnetSelection : {
79
- subnetType : ec2 . SubnetType . PUBLIC , // SubnetType.PRIVATE_WITH_EGRESS
80
- } ,
81
- } ) )
82
- }
29
+ const jobFiles = await fs . readdir ( jobsDir )
30
+ const actionFiles = await fs . readdir ( actionsDir )
31
+ const jobs = [ ]
32
+
33
+ // then, need to loop through all app/Jobs/*.ts and create a rule for each, potentially overwriting the Schedule.ts jobs
34
+ for ( const file of jobFiles ) {
35
+ if ( ! file . endsWith ( '.ts' ) )
36
+ continue
37
+
38
+ const filePath = path . jobsPath ( file )
39
+
40
+ // Await the loading of the job module
41
+ jobs . push ( await this . loadModule ( filePath ) )
42
+ }
43
+
44
+ for ( const file of actionFiles ) {
45
+ if ( ! file . endsWith ( '.ts' ) )
46
+ continue
47
+
48
+ const filePath = path . actionsPath ( file )
49
+
50
+ // Await the loading of the job module
51
+ jobs . push ( await this . loadModule ( filePath ) )
83
52
}
53
+
54
+ for ( const job of jobs )
55
+ await this . createQueueRule ( job )
84
56
}
85
57
catch ( err ) {
86
58
console . error ( 'Error reading the jobs directory:' , err )
@@ -100,9 +72,73 @@ export class QueueStack {
100
72
}
101
73
}
102
74
103
- async loadJobModule ( filePath : string ) {
75
+ async loadModule ( filePath : string ) {
104
76
const jobModule = await import ( filePath )
105
77
106
78
return jobModule
107
79
}
80
+
81
+ createQueueRule ( job : any ) {
82
+ // Now you can safely access job.default.rate
83
+ const rate = job . default ?. rate
84
+
85
+ // if no rate or job is disabled, no need to schedule, skip
86
+ if ( ! rate || job . default ?. enabled === false )
87
+ return
88
+
89
+ // Convert the rate to a Schedule object
90
+ const schedule = Schedule . cron ( this . cronScheduleFromRate ( rate ) )
91
+
92
+ const id = `QueueRule${ pascalCase ( file . replace ( '.ts' , '' ) ) } `
93
+
94
+ // Perform operations with the jobModule.default as needed
95
+ const rule = new Rule ( this . scope , id , {
96
+ // schedule to run every second
97
+ ruleName : `${ this . props . appName } -${ this . props . appEnv } -queue-rule-${ slug ( file . replace ( '.ts' , '' ) ) } ` ,
98
+ schedule,
99
+ } )
100
+
101
+ rule . addTarget ( new EcsTask ( {
102
+ cluster : this . props . cluster ,
103
+ taskDefinition : this . props . taskDefinition ,
104
+ containerOverrides : [
105
+ {
106
+ containerName : `${ this . props . appName } -${ this . props . appEnv } -api` ,
107
+ environment : [
108
+ {
109
+ name : 'QUEUE_WORKER' ,
110
+ value : 'true' ,
111
+ } ,
112
+ {
113
+ name : 'JOB' ,
114
+ value : file ,
115
+ } ,
116
+ {
117
+ name : 'JOB_BACKOFF_FACTOR' ,
118
+ value : jobModule . default ?. backoffFactor ,
119
+ } ,
120
+ {
121
+ name : 'JOB_RETRIES' ,
122
+ value : jobModule . default ?. tries ,
123
+ } ,
124
+ {
125
+ name : 'JOB_INITIAL_DELAY' ,
126
+ value : jobModule . default ?. initialDelay ,
127
+ } ,
128
+ {
129
+ name : 'JOB_JITTER' ,
130
+ value : jobModule . default ?. jitter ,
131
+ } ,
132
+ ] ,
133
+ } ,
134
+ ] ,
135
+
136
+ retryAttempts : 1 , // we utilize a custom retry mechanism in the job itself
137
+ // retryAttempts: jobModule.default.tries || 3,
138
+
139
+ subnetSelection : {
140
+ subnetType : ec2 . SubnetType . PUBLIC , // SubnetType.PRIVATE_WITH_EGRESS
141
+ } ,
142
+ } ) )
143
+ }
108
144
}
0 commit comments