Function: Schedule()
Call Signature
Schedule<
ClassPrototype,Method>(schedule):MethodDecorator<ClassPrototype,Method,ControllerProperty<ClassPrototype>>
Run the controller method on a schedule, when the entrypoint has a schedule adapter (schedule: scheduleEntrypoint() of lakutata/com/entrypoint). The runs start once the application launched and the runs in progress end before it stops. Each run has its own request scope and goes through the middlewares and the access control like a request; the method receives an empty input, and this.context is a ScheduleContext (its schedule, its planned time and its run number). The value returned is ignored; an error thrown is given to the adapter's onError, or logged by the application's log component (to the console without it), and the next runs go on.
Type Parameters
ClassPrototype
ClassPrototype extends Controller
Method
Method
Parameters
schedule
string | ScheduleOptions
A duration text ('30s', '1h30m') for an interval, any other text for a cron expression ('0 */5 * * * *', '@daily'), or the schedule options.
Returns
MethodDecorator<ClassPrototype, Method, ControllerProperty<ClassPrototype>>
The method decorator.
Throws
When the class is declared, if the interval or the cron expression is invalid, or if the expression matches no date ('0 0 30 2 *').
Throws
When the class is declared, if the time zone is unknown.
Example
import {Application} from 'lakutata'
import {buildEntrypoints, Controller, ScheduleContext, scheduleEntrypoint} from 'lakutata/com/entrypoint'
import {Schedule} from 'lakutata/decorator/ctrl'
class JobController extends Controller {
//Every 30 seconds, and once as soon as the application launched
@Schedule({every: '30s', immediate: true})
public async heartbeat(): Promise<void> {
if (this.context instanceof ScheduleContext) console.log(`run ${this.context.run} of ${this.context.schedule}`)
}
//Every 5 minutes, at second 0
@Schedule('0 */5 * * * *')
public async report(): Promise<void> {
}
//At 03:00 every day in Shanghai, by one instance of the application at a time (a mutex component named "mutex")
@Schedule({cron: '0 3 * * *', timezone: 'Asia/Shanghai', exclusive: true}, {name: 'Purge the expired sessions'})
public async purge(): Promise<void> {
}
}
Application.run(() => ({
id: 'jobs.app',
name: 'Jobs',
components: {
entrypoint: buildEntrypoints({
controllers: [JobController],
schedule: scheduleEntrypoint({onError: (error: unknown): void => console.error(error)})
})
},
bootstrap: ['entrypoint']
}))Call Signature
Schedule<
ClassPrototype,Method>(schedule,actionOptions):MethodDecorator<ClassPrototype,Method,ControllerProperty<ClassPrototype>>
Run the controller method on a schedule, with action options, when the entrypoint has a schedule adapter (schedule: scheduleEntrypoint()). Each run has its own request scope and goes through the middlewares and the access control like a request; the method receives an empty input and this.context is a ScheduleContext. An error thrown is given to the adapter's onError, or logged, and the next runs go on.
Type Parameters
ClassPrototype
ClassPrototype extends Controller
Method
Method
Parameters
schedule
string | ScheduleOptions
A duration text ('30s', '1h30m') for an interval, any other text for a cron expression ('0 */5 * * * *', '@daily'), or the schedule options.
actionOptions
ActionOptions<ScheduleContext<{ }>>
The action options: name (ClassName:method by default) names the action in the logs and is the default lock key of an exclusive schedule; description and groups describe it; acl runs the access control rules (false by default); rule is an access rule of the action; middlewares run before the ones @UseMiddlewares adds to the method.
Returns
MethodDecorator<ClassPrototype, Method, ControllerProperty<ClassPrototype>>
The method decorator.
Throws
When the class is declared, if the interval or the cron expression is invalid, or if the expression matches no date ('0 0 30 2 *').
Throws
When the class is declared, if the time zone is unknown.
Throws
When the class is declared, if the action options are invalid.