augejs.github.io

Task scheduling allows you to schedule arbitrary code (methods/functions) to execute with cron expressions which integrates with the popular node-cron package.

cron expressions

*    *    *    *    *    *
┬    ┬    ┬    ┬    ┬    ┬
│    │    │    │    │    |
│    │    │    │    │    └ day of week (0 - 7) (0 or 7 is Sun)
│    │    │    │    └───── month (1 - 12)
│    │    │    └────────── day of month (1 - 31)
│    │    └─────────────── hour (0 - 23)
│    └──────────────────── minute (0 - 59)
└───────────────────────── second (0 - 59, optional)

Installation

To begin using it, we first install the required dependencies.

npm install --save @augejs/schedule

Usage

import { 
  Application, 
  Logger, 
  ILogger, 
  ConsoleLogTransport, 
  boot, 
  GetLogger, 
  Config} from '@augejs/module-core';

import { ScheduleModule, Schedule } from './main';

Logger.addTransport(new ConsoleLogTransport());

@Application({
  subModules: [
    ScheduleModule,
  ]
})
@Config({
  every5Sec: '*/5 * * * * *'
})
class AppModule {

  @GetLogger()
  logger!:ILogger;

  @Schedule('*/20 * * * * *')
  async every20Sec() {
    this.logger.info('every20Sec tick');
  }

  @Schedule(config => config.every5Sec)
  async every5SecFromConfig() {
    this.logger.info('every5SecFromConfig tick');
  }

  @Schedule(`*/4 * * * * *`)
  async every4SecWithLongTime() {
    await new Promise(resolve => {
      setTimeout(resolve, 8000);
    });
  }

  async onInit() {
    this.logger.info('app on onInit');
  }

  async onAppWillReady() {
    this.logger.info('app on onAppWillReady');
  }

  async onAppDidReady() {
    this.logger.info('app on onAppDidReady');
  }
}

async function main() {
  await boot(AppModule);
}

main();

export const Highlight = ({children, color}) => ( <span style=>{children}</span> );

The @Schedule() decorator supports all standard cron patterns: