Home > Enterprise >  node js update server without losing the schedule tasks?
node js update server without losing the schedule tasks?

Time:07-18

I am using node js for my API and it has a schedule tasks, so when I add new feature to the project I will need to restart the server from (Digital ocean) to start this new feature, so all schedules will be gone. so How I update the project without losing them? is there's any way?

and also what is (node js pm2 restart) I can use pm2 to avoid losing schedules? or there's another ways?.

I am beginning in backend development so it's little confusing me

CodePudding user response:

You have several different approaches, but most need to use some persistent store where a newly started server can find out what tasks need to be scheduled:

  1. Store all scheduled tasks in some persistent store (JSON file, database, CSV file, etc...) so anytime your server starts, it can read the previously scheduled tasks from the file at startup and reschedule them with the newly started server process. This will also have the benefit of not losing the scheduled tasks if your server crashes unintentionally and restarts also. The downside is that you have to regularly maintain the persistent store so that it is always up to date as new tasks are scheduled or as existing tasks complete and are done.

  2. Have a way to command the server to save the currently scheduled tasks to some persistent store before you shut down your server so the server can then read them upon next startup. This could be an admin function in your server, for example.

  3. Use a scheduling module that already uses a persistent store for scheduled tasks.

  4. Be able to recreate what tasks need to be run from some other data at your server startup. You don't say what exactly your scheduled tasks are or how they come to be, but some types of schedules such as calendar reminders have an underlying store that can be queried to find upcoming tasks from scratch so you can recreate the tasks that need to be scheduled.

  5. Move the tasks to a separate process that can run on its own so you are free to restart the main server without affecting the task scheduling.

  • Related