nestjs / schedule

Schedule module for Nest framework (node.js) ⏰

Home Page:https://nestjs.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[discussion]: Provide common cron patterns

jeffminsungkim opened this issue · comments

I'm submitting a...


[ ] Regression 
[ ] Bug report
[x] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

import { Injectable, Logger } from '@nestjs/common';
import { Cron } from '@nestjs/schedule';

@Injectable()
export class TasksService {
  private readonly logger = new Logger(TasksService.name);

  @Cron('45 * * * * *')
  handleCron() {
    this.logger.debug('Called when the current second is 45');
  }
}

Expected behavior

Create string enums that provide a common cron syntax. For example,

export enum TaskSchedule {
  EVERY_SECOND = '* * * * * *',
  EVERY_10_SECONDS = '*/10 * * * * *',
  EVERY_30_SECONDS = '*/30 * * * * *',
  EVERY_MINUTE = '* * * * *',
  EVERY_5_MINUTES = '0 */5 * * * *',
  EVERY_10_MINUTES = '0 */10 * * * *',
  EVERY_30_MINUTES_FROM_9AM_TO_6PM = '0 */30 9-18 * * *',
  EVERY_HOUR = '0 * * * *',
  EVERY_2_HOURS = '0 */2 * * *',
  EVERY_DAY_AT_3_PM = '0 15 * * *',
  EVERY_DAY_AT_6_PM = '0 18 * * *',
  EVERY_DAY_MIDNIGHT = '0 0 0 * * *',

  ...
}

Would it be nice to define a set of widely used cron patterns?

import { Injectable, Logger } from '@nestjs/common';
import { Cron, TaskSchedule } from '@nestjs/schedule';

@Injectable()
export class TasksService {
  private readonly logger = new Logger(TasksService.name);

  @Cron(TaskSchedule.EVERY_30_SECONDS)
  handleCron() {
    this.logger.debug('Call every 30 seconds');
  }
}

Minimal reproduction of the problem with instructions

What is the motivation / use case for changing the behavior?

I believe whenever most people, including me, when constructing cronjobs, searching for a simple pattern on https://crontab.guru/, that sometimes hard to remember is very cumbersome.

Sounds like a nice addition! Would you like to create a PR for this?

@kamilmysliwiec Sure, I'd love to ❤️