Skip to content

Commit

Permalink
Enable cluster mode in NodeJS as Heroku suggested
Browse files Browse the repository at this point in the history
  • Loading branch information
johnson-liang committed Feb 3, 2020
1 parent d2045be commit 0b3e690
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
3 changes: 3 additions & 0 deletions ecosystem.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ module.exports = {
autorestart: true,
watch: false,
max_memory_restart: `${process.env.WEB_MEMORY || 512}M`, // // Auto-restart if process takes more than XXmo

// https://devcenter.heroku.com/articles/optimizing-dyno-usage#node-js
exec_mode: 'cluster',
},
],
};
12 changes: 12 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,3 +282,15 @@ async function processText(
console.log('\n||LOG||---------->');
return result;
}

// Graceful shutdown
// https://pm2.keymetrics.io/docs/usage/cluster-mode/#graceful-shutdown
process.on('SIGINT', async () => {
try {
await redis.quit();
process.exit(0);
} catch (e) {
console.error(e);
process.exit(1);
}
});
13 changes: 13 additions & 0 deletions src/redisClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,21 @@ function del(key) {
});
}

function quit() {
return new Promise((resolve, reject) => {
client.quit(err => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
}

export default {
set,
get,
del,
quit,
};

0 comments on commit 0b3e690

Please sign in to comment.