Scheduled jobs
@navios/schedule runs methods of a service on a cron schedule. It is built on the cron package and uses the application's DI container, so a job injects the same services your handlers do.
The package depends on @navios/core only. In an HTTP application the jobs run next to the server; a worker that does nothing but run jobs needs no adapter at all; see Other transports.
Declaring jobs
@Schedulable() marks a class that contains jobs and registers it as a service; do not add @Injectable() to the same class. @Cron marks a job method: a public instance method, synchronous or async, whose result is ignored. It takes one @Cron(); a second one on the same method throws. A subclass of a schedulable class that is decorated itself inherits its jobs and may reschedule one by decorating its override.
Running them with an application
List the classes in the module they belong to, and give the application the schedule transport:
An application has one schedule transport. Without it jobs is ignored, so a web process and a worker can share their modules.
The jobs follow the application:
app.init() and app.inject() start nothing, so tests run no jobs. Neither does an application that fails to start.
close() waits for running jobs at most shutdownTimeout milliseconds (10 seconds by default), then logs the names of those it gave up on. The services a job uses are shut down after that, not before. A job may call app.close() itself, for a worker that runs once and exits; its own run is not waited for.
A class that is only known at run time is registered through SchedulerService, from onModuleInit or later:
A class registered after the application has started has its jobs started at once. Two mistakes are logged as warnings: registering a class in an application without the transport, and injecting a schedulable class that is listed nowhere.
What a job is called with
A job method may take one argument, the JobRun:
A run that ends with the AbortError of its signal is logged as interrupted, not as failed.
Overlapping runs
When a run takes longer than the interval, the ticks that come while it is still going are skipped. The first of them is logged as a warning. Give a job whose runs may overlap @Cron(Schedule.EveryMinute, { overlap: 'allow' }).
This holds within one process. Two instances of an application both run every job; a job that must run once in a cluster has to guard itself, for example with a lock in the database.
Cron expressions
An expression has five fields, or six when it starts with seconds:
Times are in the time zone of the process, unless the job names an IANA time zone, @Cron('0 4 * * *', { timeZone: 'Europe/Kyiv' }), or the transport does, scheduleTransport({ timeZone: 'UTC' }). When the clocks change, a daily job whose time does not exist runs at the end of the gap, and one whose time happens twice runs once.
The Schedule enum names the common ones:
A DI scope per run
Every run of a job happens in its own DI scope, created when the run starts and disposed when it ends, whether it succeeded or threw.
A @Schedulable() class is a singleton by default: one instance handles every run and may keep state between runs. Give it the request scope to get a fresh instance, and fresh request-scoped dependencies, for each run:
A singleton schedulable class cannot inject a request-scoped service; that is a scope mismatch error. Singleton dependencies are shared between runs in both cases. Scopes are explained in Scopes.
A job is not an HTTP request. There is no RequestContext in a run, also when a request started the job, so context keys, request values and ctx.use() are not available; put per-run state in request-scoped services as above. Guards and interceptors, app.intercept() included, do not apply to jobs.
Managing jobs at run time
SchedulerService is injectable anywhere, and app.get(SchedulerService) returns it from outside.
A job has these members:
A disabled job is how you declare something that an operator, a feature flag or an admin endpoint switches on.
Errors
An error thrown by a job is caught and logged by the scheduler with the job's name, such as MaintenanceJobs.purgeSessions(). The job stays on its schedule and other jobs are not affected. Nothing retries the failed run; the next run is the next tick. To report it elsewhere as well:
The run counts as in progress until a promise that onError returns has settled. The error of a run started with run() goes to its caller instead, and is neither logged nor passed to onError.
register() throws when the class has no @Schedulable() decorator. It also throws, naming the class and the method, when a cron expression or a time zone is invalid or empty, or when the expression never occurs, and then registers none of the jobs of that class. For classes that modules list, that is an error of app.init().
Without an application
The scheduler needs only a DI container. Without an application nothing starts or stops it, so do it yourself:
Projects compiled with experimentalDecorators import Schedulable and Cron from @navios/schedule/legacy-compat; see Legacy decorators.