multiple worker/web processes on a single heroku app - node.js

Is there some way to configure multiple worker and/or web processes to run in the single Heroku app container? Or does this have to be broken up into multiple Heroku apps?
For example:
worker: node capture.js
worker: node process.js
worker: node purge.js
web: node api.js
web: node web.js

All processes must have unique names. Additionally, the names web and worker are insignificant and carry no special meaning. The only process that carries a significant name is the web process, as stated in the Heroku docs:
The web process type is special as it’s the only process type that
will receive HTTP traffic from Heroku’s routers. Other process types
can be named arbitrarily. -- (https://devcenter.heroku.com/articles/procfile)
So you want a Procfile like so:
capture: node capture.js
process: node process.js
purge: node purge.js
api: node api.js
web: node web.js
You can then scale each process separately:
$ heroku ps:scale purge=4

Related

Running several node.js scripts on Heroku

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"
}
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 &

webpack for multiple express instances?

I have an application that creates multiple express servers each acting as endpoints at the same time.
Each of them serve a react app that relies on the api from the that particular express server.
How do i use webpack to compile the react app and serve for the particular instance of the app for each express server .
As each react app relies on the endpoint of that instance of express the port number differs.
which i need to communicate to react app through webpack .
Also how do i run webpack when i npm start my server.
You could use the webpack.DefinePlugin to replace identifiers in your webpacked code with anything, like a port number.
In your "start" script, you could do "webpack -w & <however you start your server>", however I would recommend separating the two steps and having two different npm scripts for build and start.

Redeploying NAR (node.js) archive with PM2

There is node.js app built with Typescript, so it needs to be first "compiled" to JS before it gets run. I'm planning to use NAR (https://github.com/h2non/nar) to build ready-to-deploy package to avoid fiddling with npm install and compiling it on production. I also use PM2 as process manager for node apps.
However as far as I know PM2 can only deploy from git (fetching sources and calling npm install etc. later on), but I couldnt find a way to easily deploy application that is already pre-built.
This is my deploy.yml file contained within archive that I extract with nar extract <package>:
apps:
- script: dist/app.js
merge_logs: true
name: server
instances: 1 # 0 => max, depending on CPU cores
exec_mode: cluster
node_args: --harmony --harmony_destructuring --harmony_default_parameters
log_file: deploy/logs/server.log
pid_file: deploy/pids/server.pid
source_map_support: true
env:
NODE_ENV: production
It works fine when run for the first time, but then when I try to redeploy it (replacing application content with new version) and call pm2 reload all I get errored processes saying they either cannot load ProcessManager from PM2 or cannot find my .env file (which is in place).
As soon as I kill PM2 daemon with pm2 kill and start apps again with pm2 start all deploy.yml it clicks. But this is probably not how PM2 should be used, right?
Do you have any experience with such setup and had similar issues? Or maybe can you point me to another way of running my deployment?

Avoid garbage with nodejs app using foreverjs and running on Heroku

According to Heroku doc, to avoid garbage, you can provide flags to V8 in your Procfile:
web: node --optimize_for_size --max_old_space_size=460 --gc_interval=100 server.js
However, my app uses foreverjs, and I use the following instruction in the Procfile:
web: ./node_modules/.bin/forever -m 5 server.js
Is there a way to provide flags like --max_old_space_size or gc_interval=100 and still use foreverjs?
I'm using pm2 not forever but as it seems that forever as this syntax :
usage: forever [action] [options] SCRIPT [script-options]
I would try to put those in the script-options part :
./node_modules/.bin/forever -m 5 server.js --max_old_space_size=460 --gc_interval=100
Either way, I wouldn't force the timing of the gc...
The max_old_space_size should be in acordance to the RAM you have available for your node process, and in your case, if your node process take more than ~500M of RAM it should trigger a gc call by himself.

What heroku Procfile for parse server

Completely new to heroku, profiles and node.js. I'm running Parse Server hosted on Heroku and I'm trying to get kue running in order to do scheduled jobs.
To achieve that I need to add a worker. And to do that, I need a Procfile. But I don't know what to put in it.
This seems to work fine :
web: node index.js
worker: node queue.js
Where index.js is the index.js file of parse server and I have my queue related code in queue.js. Hope it'll help !

Resources