How can I get the current worker id in loopback?

When i started

NODE_ENV=production slc run 

loopback will automatically launch workers for each processor core.

I want to run some code only once, but every worker runs it. How can I check at what workplace he works?

I noticed that he uses a strong-supervisor behind the scenes to do his magic.

+5
source share
1 answer

Here's how I solved it:

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

Learn more about the cluster here.

+2
source

All Articles