0

Im building a node.js script/app/process (new to node and its definitions) that should run at a certain hour (from a conf file) every day. Im using the Node Schedule package to schedule the main function to run at that appointed time everyday. But the thing is this package does an "in-process scheduling", which means my script must keep running in the background for the scheduling to work. So basically my code should look like this at the end:

  1. global consts initializing and dependencies (requires)
  2. job scheduling
  3. defining all functions necessary
  4. logic to make script run permanently.

This node.js app runs inside a docker on an ec2 machine in AWS. How can I accomplish the task of running it permanently? I need a programatic solution, something (a package, a design pattern) I can embed inside my code.

Might be important to note that inside my code I have only a few lines of "requires" and const initializations, and then 1 main function that invokes everything else, if it helps somehow. Also, because im using that Node Scheduler I mentioned, restarting the script when it ends (for example through docker if its possible) is a bad idea, because the scheduling job would be lost and the script itself does nothing except initializations and scheduling the main function to the desired time, so it would restart possibly every few milliseconds, which I guess is a bad practice.

k0pernikus
  • 60,309
  • 67
  • 216
  • 347
Gibor
  • 1,695
  • 6
  • 20
  • Create a `net` server on a random port. It'll prevent your app from shutting down. – edin-m Jul 09 '19 at 08:59
  • @edin-m This sounds like kind of a workaround to me... it might actually work, but is this the best way to do so on a production environment? won't it incur extra, unneeded costs on the ec2 instance for like network usage or computations? – Gibor Jul 09 '19 at 09:05
  • What do you mean run permanently? Since you are using node-schedule package your script already never exits. Do you mean not die when you logout/close the terminal? – slebetman Jul 09 '19 at 10:21
  • @slebetman well... This is awekward but you're completely right. This paragraph at the node-schduler docs was what confused me: "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. If you need to schedule jobs that will persist even when your script isn't running, consider using actual cron." So I didn't even tested if it works without tryin to "run forever".... – Gibor Jul 09 '19 at 11:35
  • @slebetman about process not dying when I logout / close the terminal- I found a similar topic here at SO, so i'll research that and if for some reason it won't work I'll open a different thread for that. Thank you :D – Gibor Jul 09 '19 at 11:39

1 Answers1

0

Well, someone in the comment section pointed me to my mistake... I saw the following paragraph on node-scheduler and didn't even test if it works without tryin to find a way to "run forever":

"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. If you need to schedule jobs that will persist even when your script isn't running, consider using actual cron."

But apparently they meant I should keep the process running, effectively means the scheduler will terminate if I close the terminal. Here is a topic with a question and answer about how to prevent that from happening, if someone needs a reference:

How to make a node.js application run permanently?

Thanks to all who tried to help :)

Gibor
  • 1,695
  • 6
  • 20