@@ -22,6 +22,9 @@ export class Schedule {
22
22
23
23
constructor ( task : ( ) => void ) {
24
24
this . task = task
25
+ // Start job automatically when constructor is called
26
+ // We need to use setTimeout to ensure all chain methods are called first
27
+ setTimeout ( ( ) => this . start ( ) , 0 )
25
28
}
26
29
27
30
everySecond ( ) : Schedule {
@@ -149,28 +152,34 @@ export class Schedule {
149
152
return this
150
153
}
151
154
152
- start ( ) : Cron {
155
+ private start ( ) : Cron {
153
156
const job = new Cron (
154
157
this . cronPattern ,
155
- this . options ,
158
+ {
159
+ ...this . options ,
160
+ timezone : this . timezone ,
161
+ } as CronOptions ,
156
162
this . task ,
157
163
)
158
164
165
+ if ( this . options . name ) {
166
+ Schedule . jobs . set ( this . options . name , job )
167
+ }
168
+
159
169
log . info ( `Scheduled task with pattern: ${ this . cronPattern } in timezone: ${ this . timezone } ` )
160
170
return job
161
171
}
162
172
163
173
// job and action methods need to be added and they accept a path string param
164
174
static job ( name : string ) : Schedule {
165
- // Here we create a task that will run the job by name
166
175
return new Schedule ( async ( ) => {
167
176
log . info ( `Running job: ${ name } ` )
168
177
try {
169
- // Here you'd implement the actual job running logic
170
178
await runCommand ( `node path/to/jobs/${ name } .js` )
171
179
}
172
180
catch ( error ) {
173
181
log . error ( `Job ${ name } failed:` , error )
182
+ throw error // This will be caught by the error handler if one is set
174
183
}
175
184
} ) . withName ( name )
176
185
}
@@ -179,34 +188,33 @@ export class Schedule {
179
188
return new Schedule ( async ( ) => {
180
189
log . info ( `Running action: ${ name } ` )
181
190
try {
182
- // Here you'd implement the actual action running logic
183
191
await runCommand ( `node path/to/actions/${ name } .js` )
184
192
}
185
193
catch ( error ) {
186
194
log . error ( `Action ${ name } failed:` , error )
195
+ throw error
187
196
}
188
197
} ) . withName ( name )
189
198
}
190
199
191
200
static command ( cmd : string ) : Schedule {
192
- log . info ( `Executing command: ${ cmd } ` )
193
201
return new Schedule ( async ( ) => {
194
202
try {
195
203
log . info ( `Executing command: ${ cmd } ` )
196
204
const result = await runCommand ( cmd )
197
205
198
206
if ( result . isErr ( ) ) {
199
207
log . error ( result . error )
200
- return
208
+ throw result . error
201
209
}
202
210
203
211
log . info ( result . value )
204
212
}
205
213
catch ( error ) {
206
214
log . error ( `Command execution failed: ${ error } ` )
207
- throw error // This will be caught by the error handler if one is set
215
+ throw error
208
216
}
209
- } )
217
+ } ) . withName ( `command- ${ cmd } ` )
210
218
}
211
219
212
220
/**
@@ -227,7 +235,6 @@ export class Schedule {
227
235
228
236
const shutdownPromises = [ ]
229
237
230
- // Stop all running jobs
231
238
for ( const [ name , job ] of Schedule . jobs ) {
232
239
log . info ( `Stopping job: ${ name } ` )
233
240
shutdownPromises . push (
@@ -238,7 +245,6 @@ export class Schedule {
238
245
)
239
246
}
240
247
241
- // Clear the jobs map
242
248
await Promise . all ( shutdownPromises )
243
249
Schedule . jobs . clear ( )
244
250
0 commit comments