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 e11b8c9 commit e6a13de
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
2 changes: 2 additions & 0 deletions storage/framework/core/queue/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ export class Job {
}
}

export { runJob } from './run'

export { DeferQueue } from '@poppinss/defer'
43 changes: 43 additions & 0 deletions storage/framework/core/queue/src/run.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { runAction } from '@stacksjs/actions'
import { log } from '@stacksjs/cli'
import { projectPath } from '@stacksjs/path'

interface JobConfig {
name?: string
description?: string
tries?: number
backoff?: number
rate?: string
handle?: () => void | Promise<void>
action?: string | (() => void | Promise<void>)
}

export async function runJob(name: string): Promise<void> {
log.info(`Running job: ${name}`)
try {
const jobModule = await import(projectPath(`Jobs/${name}.ts`))
const job = jobModule.default as JobConfig

if (job.action) {
// If action is a string, run it via runAction
if (typeof job.action === 'string') {
await runAction(job.action)
}
// If action is a function, execute it directly
else if (typeof job.action === 'function') {
await job.action()
}
}
// If handle is defined, execute it
else if (job.handle) {
await job.handle()
}
else {
throw new Error(`Job ${name} must define either a handle function or an action`)
}
}
catch (error) {
log.error(`Job ${name} failed:`, error)
throw error
}
}
1 change: 1 addition & 0 deletions storage/framework/core/scheduler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"croner": "^9.0.0"
},
"devDependencies": {
"@stacksjs/actions": "workspace:*",
"@stacksjs/development": "workspace:*"
}
}
6 changes: 4 additions & 2 deletions storage/framework/core/scheduler/src/schedule.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type { CatchCallbackFn, CronOptions } from './'
import type { Timezone } from './types'
import { runAction } from '@stacksjs/actions'
import { log, runCommand } from '@stacksjs/cli'
import { runJob } from '@stacksjs/queue'
import { Cron } from './'

// Base interface for common methods
Expand Down Expand Up @@ -212,7 +214,7 @@ export class Schedule implements UntimedSchedule {
return new Schedule(async () => {
log.info(`Running job: ${name}`)
try {
await runCommand(`node path/to/jobs/${name}.js`)
await runJob(name)
}
catch (error) {
log.error(`Job ${name} failed:`, error)
Expand All @@ -225,7 +227,7 @@ export class Schedule implements UntimedSchedule {
return new Schedule(async () => {
log.info(`Running action: ${name}`)
try {
await runCommand(`node path/to/actions/${name}.js`)
await runAction(name)
}
catch (error) {
log.error(`Action ${name} failed:`, error)
Expand Down

0 comments on commit e6a13de

Please sign in to comment.