Skip to content

Commit

Permalink
chore: wip
Browse files Browse the repository at this point in the history
chore: wip
  • Loading branch information
chrisbbreuer committed Dec 28, 2024
1 parent e9ddfad commit a2fba8a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
3 changes: 2 additions & 1 deletion app/Scheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import { schedule } from '@stacksjs/scheduler'
* questions, feel free to reach out via Discord or GitHub Discussions.
*/
export default function () {
schedule.job('name').everyFiveMinutes()
schedule.job('name').everyMinute()
schedule.action('name').everyFiveMinutes()
schedule.command('echo "Hello, world!"').daily()
}

process.on('SIGINT', () => {
Expand Down
28 changes: 17 additions & 11 deletions storage/framework/core/scheduler/src/schedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ export class Schedule {

constructor(task: () => void) {
this.task = task
// Start job automatically when constructor is called
// We need to use setTimeout to ensure all chain methods are called first
setTimeout(() => this.start(), 0)
}

everySecond(): Schedule {
Expand Down Expand Up @@ -149,28 +152,34 @@ export class Schedule {
return this
}

start(): Cron {
private start(): Cron {
const job = new Cron(
this.cronPattern,
this.options,
{
...this.options,
timezone: this.timezone,
} as CronOptions,
this.task,
)

if (this.options.name) {
Schedule.jobs.set(this.options.name, job)
}

log.info(`Scheduled task with pattern: ${this.cronPattern} in timezone: ${this.timezone}`)
return job
}

// job and action methods need to be added and they accept a path string param
static job(name: string): Schedule {
// Here we create a task that will run the job by name
return new Schedule(async () => {
log.info(`Running job: ${name}`)
try {
// Here you'd implement the actual job running logic
await runCommand(`node path/to/jobs/${name}.js`)
}
catch (error) {
log.error(`Job ${name} failed:`, error)
throw error // This will be caught by the error handler if one is set
}
}).withName(name)
}
Expand All @@ -179,34 +188,33 @@ export class Schedule {
return new Schedule(async () => {
log.info(`Running action: ${name}`)
try {
// Here you'd implement the actual action running logic
await runCommand(`node path/to/actions/${name}.js`)
}
catch (error) {
log.error(`Action ${name} failed:`, error)
throw error
}
}).withName(name)
}

static command(cmd: string): Schedule {
log.info(`Executing command: ${cmd}`)
return new Schedule(async () => {
try {
log.info(`Executing command: ${cmd}`)
const result = await runCommand(cmd)

if (result.isErr()) {
log.error(result.error)
return
throw result.error
}

log.info(result.value)
}
catch (error) {
log.error(`Command execution failed: ${error}`)
throw error // This will be caught by the error handler if one is set
throw error
}
})
}).withName(`command-${cmd}`)
}

/**
Expand All @@ -227,7 +235,6 @@ export class Schedule {

const shutdownPromises = []

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

// Clear the jobs map
await Promise.all(shutdownPromises)
Schedule.jobs.clear()

Expand Down

0 comments on commit a2fba8a

Please sign in to comment.