Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get current worker id in loopback?

When I run

NODE_ENV=production slc run 

loopback will automatically start workers for each CPU core.

I want to run some code only once, but every worker runs it. How can I check what worker it is currently running in?

I noticed it is using strong-supervisor behind the scenes to do its magic.

like image 222
Kenneth Lynne Avatar asked Oct 31 '22 21:10

Kenneth Lynne


1 Answers

This is how I solved it:

var cluster = require('cluster');

if (cluster.isMaster || (cluster.isWorker && cluster.worker.id == '1'))) {
    //Do stuff
}

Read more about cluster here

like image 117
Kenneth Lynne Avatar answered Nov 15 '22 07:11

Kenneth Lynne