Reputation: 35
I have several .js scripts, is there a way to have them run all at the same time on the same Heroku app?
My folder
looks like this:
**MAIN_FOLDER**
Procfile
script1.js
script2.js
script3.js
script4.js
script5.js
script6.js
script7.js
script8.js
script9.js
My Procfile
:
worker: node script1.js
worker: node script2.js
...
worker: node script8.js
worker: node script9.js
And my package.json
:
"scripts": {
"script1": "node script1.js",
"script2": "node script2.js",
"script3": "node script3.js",
"script4": "node script4.js",
"script5": "node script5.js",
"script6": "node script6.js",
"script7": "node script7.js",
"script8": "node script8.js",
"script9": "node script9.js",
"start": "npm-run-all --parallel script1 script2 script3 script4 script5 script6 script7 script8 script9"
}
Upvotes: 2
Views: 2396
Reputation: 5261
There are several issues here. First of all, do you want to run each of your node scripts on a separate Heroku dyno, or is it sufficient for you to run your node scripts in parallel on one dyno?
If one dyno is enough for you, then I think you should change your Procfile to read:
worker: npm start
(I am assuming that npm-run-all that you are using in your "npm start" script appears in your package.json dependencies, otherwise that won't work).
Also, it seems you don't want a web dyno in your app (i.e. you aren't processing incoming HTTP/S traffic). If so, you need to explicitly scale your app's formation with something like heroku scale web=0 worker=1
.
Note that you should only do that if you actually need your worker dyno to be "always on". If however all you need is for your scripts to do some work and then to exit, you should use one-off dynos instead. In that case, heroku scale
your worker to 0, and then you can launch it as a one-off dyno from your command line with heroku run worker
.
If however your intent is for your node scripts to run in parallel on different dyno instances, then first of all you need to be aware of Dyno scaling limits. Note that "applications using a free dyno type are limited to a maximum of two concurrent running dynos", so that would seem to prevent you from running 9 dynos in parallel with free dynos. You could get slightly more free concurrent dynos by using one-off dynos, but still not enough to run 9 concurrent node scripts.
If you are using paid dynos, then one way to do what you want would be to modify your Procfile as follows:
worker1: node script1.js
worker2: node script2.js
...
worker8: node script8.js
worker9: node script9.js
Then, use heroku scale
to scale each of your worker*
Process Types to 1.
Note however that you should ONLY be doing that if you need your 9 node scripts to be "always on". If your scripts simply need to do some work and then exit, you should use one-off dynos instead. Otherwise you will be paying for ALOT of unused resources!! In that case, you could use heroku scale
to scale all your worker*
process types to 0, and then launch your 9 scripts from your command line to run on separate one-off dynos in parallel with something like:
heroku run worker1 &
heroku run worker2 &
...
heroku run worker9 &
Upvotes: 4