Change detected on path app restarting on pm2 - node.js

Everytime I write something in the logs I get this and the pm2 restart giving me an Service unavailable error in my webpage.
To write the logs I use
winston.info('some info');
and in the app.js I have this:
winston.add(winston.transports.File, { name: 'app-info', maxFiles: 3, filename: 'logs/app-info.log', level: 'info' });
How can avoid the pm2 to restart everytime I am writing in the log?

i solved by ignore the log and watch
watch: 'false',
ignore_watch : [ "*.log"],
so full the code :
module.exports = {
apps : [{
name: "API-XXX",
script: "/var/www/api/api-xxx-xxx/server.js",
env: {
NODE_ENV: "production",
},
env_production: {
NODE_ENV: "production",
},
watch: 'false',
ignore_watch : [ "*.log"],
}],
deploy : {
production : {
user : 'root',
host : 'localhost',
ref : 'origin/master',
repo : 'https://github.com/yogithesymbian/api-xxx-nodejs.git',
path : '/var/www/api/api-xxx-nodejs/',
'pre-deploy-local': '',
'post-deploy' : 'npm install && pm2 reload ecosystem.config.js --env production',
'pre-setup': ''
}
}
};
then run
pm2 restart ecosystem.config.js --env production
i forgot the github disscussion to refer someone said put the line .

You can resolve this problem using --no-autorestart,
example:
pm2 start app.js --no-autorestart

Related

How can I deploy only one specific nodeJS app to one specific environment with PM2?

I have two nodejs app deployed on Digital Ocean, with PM2, which I am learning.
I'd like to know how I could update my apps (services) separately.
At the moment my pm2 config looks like this:
// ecosystem.config.js
module.exports = {
apps: [
{
name: `app1`,
script: './app1.js',
// ...
},
{
name: `app2`,
script: './app2.js',
// ...
}
],
deploy: {
production: {
key: '',
user: 'admin',
host: '',
ref: '',
repo: '',
path: '/home/admin/app/prod',
'post-deploy': './deploy.sh production'
},
development: {
key: '',
user: 'admin',
host: '',
ref: '',
repo: '',
path: '/home/admin/app/development',
'post-deploy': './deploy.sh development'
},
}
}
I can deploy per environment, like pm2 deploy production.
But, can I do something like pm2 deploy development app2 to only update app2?
You have many ways to do that, for me I always create ecosystem file in the client folder and server folder, then create publish ecosystem file in the root folder, like:
client
ecosystem.config.js
server
ecosystem.config.js
ecosystem.config.js
The root ecosystem file like:
deploy: {
frontend: {
...other,
path: '/app/easyv_spaceship',
'post-deploy': 'cd ./client && npm install && npm run build'
},
backend: {
...other,
path: '/app/easyv_spaceship',
'post-deploy': 'cd ./server && npm install && npm run tsc && pm2 startOrRestart ecosystem.config.js --env production'
}
}
hope this can inspire you.

pm2 does not apply changes to ecosystem.config.js on restart or reload

I have the following ecosystem.config.js file for my application:
module.exports = {
apps: [
{
name: 'Worker A',
script: 'scripts/workerA.js',
instances: 4,
autorestart: true,
watch: false,
instance_var: 'INSTANCE_ID',
max_memory_restart: '1G'
},
{
name: 'Worker B',
script: 'scripts/workerB.js',
instances: 4,
autorestart: true,
watch: false,
instance_var: 'INSTANCE_ID',
max_memory_restart: '1G'
}
]
}
When the processes are started via pm2 start ecosystem.config.js everything is working fine. However, if I now change the instances of the applications to a different number (i.e. 8) and try to restart the processes via pm2 restart ecosystem.config.js, the same number of instances will be started as before. pm2 reload ecosystem.config.js and pm2 startOrReload ecosystem.config.js show the same behaviour.
The only way I have solved the problem is to completely delete all the processes with pm2 delete ecosystem.config.js and then start them all again. While this works, it robs me of the possiblity to use the "0-second-downtime" restart that pm2 reload ... offers.
Is there any solution to this? Thanks!
$ pm2 -v
4.2.3
scaling nodes only work in cluster mode in pm2 (https://pm2.keymetrics.io/docs/usage/cluster-mode/)
so add
exec_mode : "cluster" to your configuration
module.exports = {
apps: [
{
name: 'Worker A',
script: 'scripts/workerA.js',
instances: 4,
autorestart: true,
watch: false,
instance_var: 'INSTANCE_ID',
max_memory_restart: '1G',
exec_mode : "cluster"
},
{
name: 'Worker B',
script: 'scripts/workerB.js',
instances: 4,
autorestart: true,
watch: false,
instance_var: 'INSTANCE_ID',
max_memory_restart: '1G',
exec_mode : "cluster"
}
]
}

pm2 node api start seems to not respect host?

I'm trying to wrap around pm2:
pm2.start(
{
apps: [
{
script: 'app.js',
path: 'remote/path',
name: 'App',
autorestart: false,
host: [123.456.789],
username: 'root',
},
],
},
err => {
if (err) throw err
},
)
It still seems to be trying to run app.js on my local machine, and not the host 123.456.789 - any idea what's going on?
host should be an array of strings: host: ['123.456.789'].

How not to create a "cron-out" log file?

When I run pm2, it creates two cron-out and cron-error files in the logs folder. How to not create these files when starting pm2?
You can Generate Configuration
pm2 ecosystem
This will generate following file (ecosystem.config.js) ,pass error_file: "/dev/null" to not to generate pm2 logs
module.exports = {
apps : [{
name: "app",
script: "./app.js",
error_file: "/dev/null"
env: {
NODE_ENV: "development",
},
env_production: {
NODE_ENV: "production",
}
}]
}
pm2 [start|restart|stop|delete] ecosystem.config.js
This is the link I followed -http://pm2.keymetrics.io/docs/usage/application-declaration/
Or Shortest Way
pm2 start app /dev/null

How to make express.js using pm2 run only one instance on port (8080) and on second instance use another port (8081)

I searched all over, but could not find the answer
for how to config pm2 with Express.js
here is what i have so far based on other's answers and pm2 documentation.
this is on the server main file(index.js):
const port = process.env.NODE_PORT;
I get undefined unless i use || 8080 .
const port = process.env.NODE_PORT || 8080;
I need it working only on dev env for now..
but it seems it does not get what i config on ecosystem.config.js file.
and on my ecosystem.config.js:
module.exports = {
apps: [{
name: 'API',
script: 'index.js',
// Options reference: https://pm2.io/doc/en/runtime/reference/ecosystem-file/
args: 'one two',
instances: 1,
exec_mode: "fork_mode",
autorestart: true,
watch: false,
max_memory_restart: '1G',
env: {
NODE_PORT = 8080 `pm2 start app.js -f`,
NODE_PORT = 8081 `pm2 start app.js -f`,
NODE_ENV: 'development'
},
env_production: {
NODE_ENV: 'production'
}
},
{
name: 'API',
script: 'index.js',
// Options reference: https://pm2.io/doc/en/runtime/reference/ecosystem-file/
args: 'one two',
instances: 1,
exec_mode: "fork_mode",
autorestart: true,
watch: false,
max_memory_restart: '1G',
env: {
PORT: 8081,
NODE_ENV: 'development'
},
env_production: {
NODE_ENV: 'production'
}
}
],
deploy: {
production: {
user: 'node',
host: '212.83.163.1',
ref: 'origin/master',
repo: 'git#github.com:repo.git',
path: '/var/www/production',
'post-deploy': 'npm install && pm2 reload ecosystem.config.js --env
production'
}
}
};
I'm using environment variable process.env.NODE_APP_INSTANCE to do this. (https://pm2.io/doc/en/runtime/guide/load-balancing/#cluster-environment-variable)
I'm setting my PORT before starting my server, and then I'm setting server port based on PORT environment variable and NODE_APP_INSTANCE, something like this:
const nodeInstance = parseInt(process.env.NODE_APP_INSTANCE || 0, 10);
const port = process.env.PORT + nodeInstance;

Resources