EggJS provides Schedule mechanism to perform some scheduled tasks

EggBornJS has been enhanced on the basis of EggJS Schedule, which is mainly reflected in two aspects:

  1. Can be compiled
  2. Easy to develop

Next, we will develop a Schedule, which print schedule invoked: Hello World on the console every 3 seconds

Declaration of Schedule


module.exports = appInfo => {
  const config = {};

  // schedules
  config.schedules = {
    scheduleDemo: {
      interval: '3s',
      type: 'worker',
      path: 'test/schedule',
      instance: true,

  return config;
Name Description
scheduleDemo Schedule Name
interval Timing Interval,also supporting cron
type all, worker
path API Route to be performed
instance Whether to inject instance object into the context when the API Route performed

What is the nature of attribute instance?

  • We mentioned earlier that EggBornJS supports multiple instances. Because schedule is triggered by the system, there is no instance information in the context
  • If instance set to false, the API Route needs to handle the logic related to multiple instances by itself
  • If instance set to true, the system will automatically loop multiple instances, perform API Route in turn, and inject instance object into the context environment, thus simplifying the development workload of API Route

Declaration of API Route


{ method: 'post', path: 'test/schedule', controller: test, middlewares: 'inner',
  meta: { auth: { enable: false } },
Name Description
middlewares Specify the middleware inner, only allowed performed inner access
auth.enable Disable the middleware auth

Why disable the global middleware auth?

  • Because Schedule is triggered by the system, there is no user information in its context

Controller Action


async schedule() {
  console.log('schedule invoked: ', 'Hello World');