javascriptnode.jscronnestjs

Will Nest.js dynamic cron jobs be deleted after a restart or shutdown?


So I developed a system with Nest.js which is able to create a dynamic cronjob from a user's input in the frontend application, I store this data in my database and at the same time I create the job in the server with the Dynamic schedule module API. Today I was wondering what would happen to my cronjobs if my server was shutdown or if it restarted itself, since my jobs aren't declarative and they are created at runtime I think that maybe when my server starts I should create the cronjobs again? I'm not sure if this get stored in memory or something since it's not in the documentation.

My concern, in fewer words, is:

Should I recreate my jobs using the information from the database once the server starts itself? Why yes or why not?


Solution

  • Once the server restarts, you'll have to restart your CRON tasks.

    The reason for this is that your NestJS application is not really adding CRON expressions to a crontab on the machine but instead scheduling and running these tasks in a CRON expression capable task runner (package example: node-cron) Once the server stops, the scheduler (that runs on the same process) also stops.

    You've mentioned you already store the tasks in a database, so you should be able to get the information you need to recreate the task on server init.

    I assume you have a Service that handles CRON tasks and has access to the DB. Otherwise you can just create a dedicated one

    You can modify it to implement the OnModuleInit interface and execute logic that would read all the existing tasks that should run and start them:

    @Injectable()
    class CronService implements OnModuleInit {
      constructor(private readonly cronRepository: CronRepository) {}
      
      /*
        ...
        CRON IMPLEMENTATION
        ...
      */
    
      private async restartSavedCronTasks(): Promise<void> {
        // get data from DB and start all relevant tasks
      }
    
      async onModuleInit: Promise<void> {
        await this.restartSavedCronTasks();
      }
    }