Task Scheduling

Entropy provides a simple way to schedule tasks with CRON or interval. The task scheduling is based on simple controller methods decorated with Schedule, Interval and Timeout decorators.

Defining Scheduled CRON Tasks

The example implementation of CRON-scheduled task you can see below:

      

src/newsletters/newsletter.controller.ts

import { Schedule } from '@entropy/scheduler'; @Controller() export class NewsletterController extends Controller { // ... @Schedule('0 30 9 * * 1-5') // Execute from Monday to Friday at 9:30am public async sendEmails() { await this.mailer.send(...); // ... } }

This task will be executed every Monday to Friday at 9:30am.

Interval Tasks

To schedule a task to be executed every n milliseconds, you can use Interval decorator:

      

src/newsletters/newsletter.controller.ts

import { Interval, TimeUnit } from '@entropy/scheduler'; @Controller() export class NewsletterController extends Controller { // ... @Interval(60 * TimeUnit.Second) // Execute every 60 seconds public checkActivity() { // Check users activity... } }

Timeout Server Startup Tasks

In order to execute a task after a certain amount of time, you can use Timeout decorator:

      

src/root.controller.ts

import { Timeout, TimeUnit } from '@entropy/scheduler'; @Controller() export class RootController extends Controller { // ... // Run 7 seconds after server startup (if controller is registered in module) @Timeout(7 * TimeUnit.Second) public setupSystem() { // ... } }
CSRF Protection Utilities