Skip to main content

Scheduled Services

In your application, you may find yourself needing to run hourly, weekly, daily jobs, etc... In these contexts, you can leverage your ApplicationScheduledService class to schedule using a beautiful, cron-driven api, courtesy of bullmq under the hood.

import ApplicationScheduledService from '../ApplicationScheduledService'
import HourProcessor from './HourProcessor'

export default class ScheduledJobs extends ApplicationScheduledService {
public static async scheduleAllJobs() {
await this.schedule('0 * * * *', 'processHour')
}

public static async processHour() {
await HourProcessor.process(DateTime.now())
}
}

A service class should be either scheduled or backgrounded, not both. Scheduled services work best as thin orchestrators: select the work due now, then fan out to backgrounded services for the expensive or retryable work.

export default class ScheduledJobs extends ApplicationScheduledService {
public static async scheduleAllJobs() {
await this.schedule('0 * * * *', 'processHour')
}

public static async processHour() {
const userIds = await User.where({ syncEnabled: true }).pluck('id')
for (const userId of userIds) {
await IntercomSync.syncUser(userId)
}
}
}