Activating cron restarts on pm2 after reboot - node.js

Currently, I have this ecosystem.config.js file setup:
module.exports = {
apps: [
{
name: 'Bot',
script: `./src/index.js`,
watch: false,
autorestart: true,
},
{
name: 'Post Stats',
script: `./jobs/postStats.js`,
cron_restart: '*/30 * * * *',
watch: false,
autorestart: false,
},
],
}
I then run this with pm2 start ecosystem.config.js and everything works fine, with the various files restarting when expected.
To get the process to start automatically on system start up, I run pm2 startup and run the command suggested (as shown in https://pm2.keymetrics.io/docs/usage/startup/). I then run pm2 save to save the current config.
Then, when I restart the server, and the Bot process starts as expected but the Post Stats process doesn't.
How can I have the cron restart re-registered when the server restarts?

Related

Node/Express server deploy with Heroku and PM2

Attempting to add clustering ability via PM2 and deploy via my Node/Express application.
I've set up the following command:
pm2 start build/server/app.js -i max
The above works fine locally. I'm testing the functionality on a staging environment on Heroku via Performance 1X.
The above shows the log for the command but attempting 1 instance rather than max. It shows typical info after successfully running pm2 start however you can see app immediately crashes afterward.
Any advice or guidance is appreciated.
I ended up using the following documentation: https://pm2.keymetrics.io/docs/integrations/heroku/
Using a ecosystem.config.js with the following:
module.exports = {
apps : [
{
name: `app-name`,
script: 'build/server/app.js',
instances: "max",
exec_mode: "cluster",
env: {
NODE_ENV: "localhost"
},
env_development: {
NODE_ENV: process.env.NODE_ENV
},
env_staging: {
NODE_ENV: process.env.NODE_ENV
},
env_production: {
NODE_ENV: process.env.NODE_ENV
}
}
],
};
Then the following package.json script handles the deployment per the environment I am looking to deploy e.g. production:
"deploy:cluster:prod": "pm2-runtime start ecosystem.config.js --env production --deep-monitoring",
I got the same error but I fixed it by adding
{
"preinstall":"npm I -g pm2",
"start":"pm2-runtime start build/server/app.js -i 1"
}
To my package.json file
This is advised for production environment
But running
pm2 start build/server/app.js -i max
Is for development purpose

How to configure PM2 to start certain applications based on the system's environment variables?

I went through the PM2 documentation and it looks like it provides the facility to send environment variables to your application but I could not find any information on how to configure PM2 such that it starts applications based on the system's environment variables.
For example, I have the following config file -
module.exports = {
apps: [{
name: 'app',
script: 'app.js',
instances: 1,
autorestart: true,
watch: true
}, {
name: 'worker',
script: './server/sync.js',
instances: 1,
autorestart: true,
watch: true
}]
};
On the server where I have an environment variable set, say database, I want the worker app to run. Otherwise, I want the main application app to run.
I basically want to run pm2 start ecosystem.config.js and let PM2 decide which of the applications to start based on the system's environment variables. How can I achieve this?

How to require dotenv/config file on (before) PM2 starts

I run node app like this:
node -r dotenv/config dist/app
I need something similar using PM2:
pm2 start -r dotenv/config dist/app.js --name appname // doesn't work
I receive the next error: error: unknown option -r
Using node_args.
pm2 start --node-args="-r dotenv/config" dist/app.js --name appname
I have made a Shell script:
// pm2-start.sh
NODE_ENV=production &&
node -r dotenv/config dist/app
Then I ran pm2 start pm2-start.sh --name appname
Tip I have also ran: pm2 startup then copied command that pm2 instructed to run in order to activate the auto startup of all apps that are registered via pm2.
Then I ran pm2 save to save the auto service.
Note: pm2 lists apps distinctly between server account respectively. That means that apps that are listed on user A will not be listed on user B. That's true for the pm2 startup command - that should be done for each account.
Hope it helps.
None of this worked for me because I was using an ecosystem file AND cluster mode which behaves really odd (not like without cluster mode...).
I installed dotenv as dev dependency at the root (I was using yarn workspaces too).
Then I did this:
require('dotenv').config({ path: 'path/to/your/.env' })
module.exports = {
apps: [
{
name: 'app',
script: 'server/dist/index.js',
instances: 2,
exec_mode: 'cluster',
instance_var: 'APP_INSTANCE_SEQ',
// listen_timeout: 10000,
// restart_delay: 10000,
}
]
}

Why is PM2 watching but not catching changes to code?

I read related posts on this question, but they don't help.
I'm running a Node-Express app in a windows environment using PM2 with pm2-windows-service. Until today it's been running node just fine and reloading Node when I save changes to my code. Today it stopped working. When I make changes, Node still serves up the old code. Even when I manually restart PM2. Plus, when I manually start PM2 it rapidly restarts Node until I kill Node with Task Manager.
Furthermore, even when I kill PM2, delete the PM2 app, and try manually running Node or nodemon, I still get the old code.
Baffling. Any theories?
Thank you!
Here's my ecosystem.config.js file:
module.exports = {
apps : [{
name: 'sm_api',
script: 'server/index.js',
log_date_format : "YYYY-MM-DD HH:mm Z",
// Options reference: https://pm2.io/doc/en/runtime/reference/ecosystem-file/
args: 'one two',
instances: 'max',
error_file : "C:\\pm2_system\\.pm2\\logs\\sm-api-error",
out_file: "C:\\pm2_system\\.pm2\\logs\\sm-api-out",
autorestart: true,
watch: true,
max_restarts: 10,
max_memory_restart: '1G',
env: {
NODE_ENV: 'development'
},
env_production: {
NODE_ENV: 'production'
},
exec_mode: 'cluster'
}],
};

pm2 script execution path is incorrect, doesn't match the one in ecosystem.config.js

My ecosystem.config.js looks like this:
module.exports = {
apps: [{
name: 'production',
script: '/home/username/sites/Website/source/server.js',
env: { NODE_ENV: 'PRODUCTION' },
args: '--run-server'
}, {
name: 'staging',
script: '/home/username/sites/WebsiteStaging/source/server.js',
env: { NODE_ENV: 'STAGING' },
args: '--run-server'
}],
deploy: {
production: {
user: 'username',
host: ['XXX.XXX.XX.XXX'],
ref: 'origin/production',
repo: 'git#github.com:ghuser/Website.git',
path: '/home/username/sites/Website',
'post-deploy': 'npm install && pm2 reload ecosystem.config.js --only production',
env: { NODE_ENV: 'PRODUCTION' }
},
staging: {
user: 'username',
host: ['XXX.XXX.XX.XXX'],
ref: 'origin/staging',
repo: 'git#github.com:ghuser/Website.git',
path: '/home/username/sites/WebsiteStaging',
'post-deploy': 'npm install && pm2 reload ecosystem.config.js --only staging',
env: { NODE_ENV: 'STAGING' }
}
}
};
When I deploy the application, I expect to see two processes - one called 'production' and one called 'staging'. These run code from the same repo, but from different branches.
I do see two processes, however, when I run pm2 desc production I can see that the script path is /home/username/sites/WebsiteStaging/source/server.js. This path should be /home/username/sites/Website/source/server.js as per the config file.
I've tried setting the script to ./server.js and using the cwd parameter but the result has been the same.
The deploy commands I am using are pm2 deploy production and pm2 deploy staging and I have verified that both the Website and the WebsiteStaging folders are present on my server.
Is there something I'm missing here? Why would it be defaulting to the staging folder like this?
What worked for me was to delete the pm2 application and start it.
pm2 delete production
pm2 start production
When I ran pm2 desc production, I saw that the script path was incorrect, and nothing I did seemed to correct that path, short of the above.
I had the same issue.
Seems it happend due to old dump.pm2 that was not updated after changes to ecosystem.config.js were made.
Updating the startup script solved the issue
pm2 save
pm2 unstartup
pm2 startup

Resources