| name | stacks-jobs |
| description | Use when creating background job classes in app/Jobs/ — job structure, the handle method, job configuration (queue, tries, backoff, timeout, rate), dispatching patterns (dispatch, dispatchIf, dispatchAfter, dispatchNow), or the Every schedule constants. For the queue system internals (workers, batching, events, drivers, testing), see stacks-queue. |
| license | MIT |
| compatibility | Bun >= 1.3.0, TypeScript |
| allowed-tools | Read Edit Write Bash Grep Glob |
Stacks Jobs
Background job classes defined in app/Jobs/.
Key Paths
- Application jobs:
app/Jobs/
- Queue config:
config/queue.ts
Creating a Job
import { Job } from '@stacksjs/queue'
export default new Job({
name: 'SendWelcomeEmail',
description: 'Send welcome email to new user',
queue: 'emails',
tries: 3,
backoff: 10,
timeout: 30,
enabled: true,
async handle(payload: { email: string; name: string }) {
console.log(`Sending to ${payload.email}`)
return { sent: true }
}
})
Job Configuration Options
{
name: string
description?: string
queue?: string
tries?: number
backoff?: number
backoffConfig?: {
strategy: 'fixed' | 'exponential' | 'linear'
initialDelay: number
factor: number
maxDelay: number
jitter?: { enabled: boolean, factor: number }
}
timeout?: number
rate?: string
enabled?: boolean
handle: (payload?) => any
}
Dispatching Jobs
await SendWelcomeEmail.dispatch({ email: 'user@example.com', name: 'John' })
await SendWelcomeEmail.dispatchIf(isNewUser, { email, name })
await SendWelcomeEmail.dispatchUnless(isExistingUser, { email, name })
await SendWelcomeEmail.dispatchAfter(60, { email, name })
await SendWelcomeEmail.dispatchNow({ email, name })
Fluent Job Builder
import { job } from '@stacksjs/queue'
await job('SendWelcomeEmail', { email, name })
.onQueue('emails')
.delay(60)
.tries(5)
.timeout(30)
.backoff([10, 30, 60])
.dispatch()
Scheduled Jobs
Use the rate property for automatic scheduling:
import { Every } from '@stacksjs/enums'
export default new Job({
name: 'CleanupExpiredTokens',
rate: Every.Hour,
handle() {
}
})
Register in app/Scheduler.ts:
import { schedule } from '@stacksjs/scheduler'
export default function() {
schedule.job('CleanupExpiredTokens').hourly().setTimeZone('America/New_York')
}
CLI Commands
buddy make:job [name]
buddy queue
Gotchas
- Jobs must export
default new Job({...}) — not a plain object
- The
handle() method receives the payload passed to dispatch()
dispatchNow() runs immediately in the current process — no queue involved
- Default queue driver is
sync — jobs run immediately unless changed to database or redis
- Jobs with
rate are auto-discovered by the scheduler
- Backoff array
[10, 30, 60] means: retry after 10s, then 30s, then 60s
- Jobs should be idempotent — safe to retry on failure
- For queue workers, batching, events, and testing, see the
stacks-queue skill