Scheduled Tasks
Although the HTTP Server we developed using the framework is a request-response model, there are still many scenarios that need to execute some scheduled tasks, for example:
- Regularly report server application status.
- Regularly update the local cache from the remote interface.
- Regularly split files, delete temporary files.
The framework provides a mechanism to make the development and maintenance of scheduled tasks more elegant.
# Develop Scheduled Tasks
All scheduled tasks should be placed in directory app/schedule
. Each file is an independent scheduled task that could configure the properties and the detail methods to be executed.
A simple example, to define a scheduled task to update the remote data to the memory cache, we can create a update_cache.js
file in the directory 'app/schedule`
const Subscription = require('egg').Subscription; |
Can also be abbreviated as
module.exports = { |
This scheduled task will be executed every 1 minute on every worker process, the requested remote data will be mounted back to app.cache
.
# Task
task
orsubscribe
is compatible withgenerator function
andasync function
.- The parameter of
task
isctx
, anonymous Context instance, we could callservice
and others via it.
# Schedule Mode
Schedule tasks can specify interval
or cron
two different schedule modes.
# interval
Configure the scheduled tasks by schedule.interval
, scheduled tasks will be executed every specified time interval. interval
can be configured as
- Integer type, the unit is milliseconds, e.g
5000
. - String type, will be transformed to milliseconds by ms, e.g
5s
.
module.exports = { |
# cron
Configure the scheduled tasks by schedule.cron
, scheduled tasks will be executed at specified timing according to the cron expressions. cron expressions are parsed by cron-parser.
Note: cron-parser supports second (which don't support by linux crontab).
* * * * * * |
module.exports = { |
# Type
The framework scheduled tasks support two types by default, worker and all. Both worker and all support the above two schedule modes, except when it comes time to execute, the worker who executes the scheduled tasks is different:
worker
type: only one worker per machine executes this scheduled task, the worker to execute is random.all
type: each worker on each machine executes this scheduled task.
# Other Parameters
In addition to the parameters just introduced, scheduled task also supports these parameters:
cronOptions
: configure cron time zone and so on, reference cron-parserimmediate
: when this parameter is set to true, this scheduled task will be executed immediately after the application is started and ready.disable
: when this parameter is set to true, this scheduled task will not be executed.env
: env list to decide whether start this task at current env.
# Logging
Schedule log will be written to ${appInfo.root}/logs/{app_name}/egg-schedule.log
, but won't be logged to terminal by default, you could customize via config.customLogger.scheduleLogger
.
// config/config.default.js |
# Dynamically Configure Scheduled Tasks
Sometimes we need to configure the parameters of scheduled tasks. Scheduled tasks support another development style:
module.exports = app => { |
# Manually Execute Scheduled Tasks
We can run a scheduled task via app.runSchedule(schedulePath)
. app.runSchedule
reads a scheduled task file path (either a relative path in app/schedule
or a complete absolute path), executes the corresponding scheduled task, and returns a Promise.
There are some scenarios we may need to manually execute scheduled tasks, for example
- Executing scheduled tasks manually for more elegant unit testing of scheduled tasks.
const mm = require('egg-mock'); |
When the application starts up, manually perform the scheduled tasks for system initialization, waiting for the initialization finished and then starting the application. See chapter Application Startup Configuration, we can implement initialization logic in app.js
.
module.exports = app => { |
# Extend Scheduled Task Type
The framework scheduled tasks only support single worker execution and all worker execution, in some cases, our services are not deployed on a single machine, one of the worker processes in the cluster may execute a scheduled task.
The framework does not provide this functionality directly, but developers can extend the new type of scheduled tasks themselves in the upper framework.
Inherit agent.ScheduleStrategy
in agent.js
and register it with agent.schedule.use()
:
module.exports = agent => { |
ScheduleStrategy
base class provides:
schedule
- Properties of schedule tasks,disable
is supported by default, other configurations can be parsed by developers.this.sendOne(...args)
- Notice worker to execute the task randomly,args
will pass tosubscribe(...args)
ortask(ctx, ...args)
.this.sendAll(...args)
- Notice all worker to execute the task.