title: Rescheduling cron jobs in nodejs on server restart
body: <p>I have node js application where a user can schedule 4-5 different cron jobs. A user selects a start_date, end_date, start_time, and end_time, and accordingly, the cron functions are scheduled and some campaigns are generated to connect with leads (or other users). I am using npm package <a href="https://www.npmjs.com/package/node-schedule" rel="nofollow noreferrer">node-schedule</a> to achieve the desired cron-job execution but the issue arises if my web application crashes in between. </p>
  
  <p>As per the official documentation of <code>node-schedule</code>, </p>
  
  <blockquote>
    <p>Note that Node Schedule is designed for in-process scheduling, i.e. scheduled jobs will only fire as long as your script is running, and the schedule will disappear when execution completes.</p>
  </blockquote>
  
  <p>The solution I can come up to resolve this is to call the cron functions using server.js file which restarts every time the server script is executed. But I am unable to resolve some of the following issues:</p>
  
  <ol>
  <li>A scheduler is an individual function, not campaign-specific, so how will the application know which cron-function to start if it has not been completed?</li>
  <li>A scheduler is a single function, that will be called every time as soon as the server is restarted. If 6 instances of a server are created, it will be called 6 times. </li>
  </ol>
  
  <p>Suppose, my scheduler is due to execute between 28th May 11:00 AM to 31st May 12:00 AM. The scheduler starts its job and some of the campaigns are executed in between. If my server goes down on 30th May, how can I pick up from the remaining date-time and campaigns and complete it from there?</p>
  
  <p>So I am concerned about how to schedule or handle the cron job functions for the campaigns? If any of the campaign cron jobs is crashed in between if the server is stopped, how should I be able to re-schedule or execute that cron function from the time or date that has been left of it?</p>
  
  <p>I am not much experienced with cron jobs, hoping for some handy solutions.</p>