Services
Any class that extends ApplicationBackgroundedService
provides the background
and backgroundWithDelay
methods that are used to background methods on the class. For instance, let's say you have a service that syncs your user data to a service like intercom, twilio, salesforce, helpdesk, etc...
class IntercomSync {
public static async syncUser(user: User) {
// ...sync user to intercom
}
}
You can easily make this a backgroundable service. Simply extend ApplicationBackgroundedService
, and now you can background:
class IntercomSync extends ApplicationBackgroundedService {
public static async syncUser(user: User) {
await this.background('_syncUser', user.id)
}
public static async _syncUser(id: IdType) {
const user = await User.find(id)
if (!user) return
// ...sync user to intercom
}
}
Now, from anywhere in your app you can safely call syncUser
without tying up resources on the api request!