Skip to content

Commit a2fba8a

Browse files
committed
chore: wip
chore: wip
1 parent e9ddfad commit a2fba8a

File tree

2 files changed

+19
-12
lines changed

2 files changed

+19
-12
lines changed

app/Scheduler.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ import { schedule } from '@stacksjs/scheduler'
99
* questions, feel free to reach out via Discord or GitHub Discussions.
1010
*/
1111
export default function () {
12-
schedule.job('name').everyFiveMinutes()
12+
schedule.job('name').everyMinute()
1313
schedule.action('name').everyFiveMinutes()
14+
schedule.command('echo "Hello, world!"').daily()
1415
}
1516

1617
process.on('SIGINT', () => {

storage/framework/core/scheduler/src/schedule.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ export class Schedule {
2222

2323
constructor(task: () => void) {
2424
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)
2528
}
2629

2730
everySecond(): Schedule {
@@ -149,28 +152,34 @@ export class Schedule {
149152
return this
150153
}
151154

152-
start(): Cron {
155+
private start(): Cron {
153156
const job = new Cron(
154157
this.cronPattern,
155-
this.options,
158+
{
159+
...this.options,
160+
timezone: this.timezone,
161+
} as CronOptions,
156162
this.task,
157163
)
158164

165+
if (this.options.name) {
166+
Schedule.jobs.set(this.options.name, job)
167+
}
168+
159169
log.info(`Scheduled task with pattern: ${this.cronPattern} in timezone: ${this.timezone}`)
160170
return job
161171
}
162172

163173
// job and action methods need to be added and they accept a path string param
164174
static job(name: string): Schedule {
165-
// Here we create a task that will run the job by name
166175
return new Schedule(async () => {
167176
log.info(`Running job: ${name}`)
168177
try {
169-
// Here you'd implement the actual job running logic
170178
await runCommand(`node path/to/jobs/${name}.js`)
171179
}
172180
catch (error) {
173181
log.error(`Job ${name} failed:`, error)
182+
throw error // This will be caught by the error handler if one is set
174183
}
175184
}).withName(name)
176185
}
@@ -179,34 +188,33 @@ export class Schedule {
179188
return new Schedule(async () => {
180189
log.info(`Running action: ${name}`)
181190
try {
182-
// Here you'd implement the actual action running logic
183191
await runCommand(`node path/to/actions/${name}.js`)
184192
}
185193
catch (error) {
186194
log.error(`Action ${name} failed:`, error)
195+
throw error
187196
}
188197
}).withName(name)
189198
}
190199

191200
static command(cmd: string): Schedule {
192-
log.info(`Executing command: ${cmd}`)
193201
return new Schedule(async () => {
194202
try {
195203
log.info(`Executing command: ${cmd}`)
196204
const result = await runCommand(cmd)
197205

198206
if (result.isErr()) {
199207
log.error(result.error)
200-
return
208+
throw result.error
201209
}
202210

203211
log.info(result.value)
204212
}
205213
catch (error) {
206214
log.error(`Command execution failed: ${error}`)
207-
throw error // This will be caught by the error handler if one is set
215+
throw error
208216
}
209-
})
217+
}).withName(`command-${cmd}`)
210218
}
211219

212220
/**
@@ -227,7 +235,6 @@ export class Schedule {
227235

228236
const shutdownPromises = []
229237

230-
// Stop all running jobs
231238
for (const [name, job] of Schedule.jobs) {
232239
log.info(`Stopping job: ${name}`)
233240
shutdownPromises.push(
@@ -238,7 +245,6 @@ export class Schedule {
238245
)
239246
}
240247

241-
// Clear the jobs map
242248
await Promise.all(shutdownPromises)
243249
Schedule.jobs.clear()
244250

0 commit comments

Comments
 (0)