Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

217
Views
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?

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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 date from DB and start all relevant tasks
  }

  async onModuleInit: Promise<void> {
    await this.restartSavedCronTasks();
  }
}
about 4 years ago · Santiago Trujillo Report

0

The answer is yes, you should recreate the cron jobs after the server restarts. It's best that once the user's creates the cron job, you'll store it in the DB or use the DB you already use for Frontend.

about 4 years ago · Santiago Trujillo Report

0

In general, you could have two levels of persistence of this data in case of necessity.

Scenario 1

You very seldom restart your servers. In this case, store to something persistent like a json file in an s3 bucket (not public) or to a sql or mongo database if you have it connected.

Recreate the "unhandled" or relevant cron jobs on server warmup.

Scenario 2

You often restart your servers/services. In this case, perhaps store to an in-memory db like Redis as well as a more persistent type (in case your Redis db falls and you need to repopulate it - provided that this data is important).

Recreate the "unhandled" or relevant cron jobs on server warmup.

Storing to Redis in general instead of storing in-memory on a specific server instance will also allow your general architecture to be more scalable as a different server can read from Redis as well (same "source of truth")

I hope this helps you plan your architecture accordingly :)

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!