Pm2: Clustering code - Execute code portions only once

Created on 10 Sep 2017  路  1Comment  路  Source: Unitech/pm2

I wrote an Express application which I would like to scale using pm2. This express app performs a couple jobs which I only need to execute once. When I am going to start multiple instances of my jobs using pm2 I assume that it will also run the defined jobs multiple times.

So how can I make my application aware that only one instance has to run specific code portions?

Code example:
agenda.every('1 hours', 'refreshProfiles')

Question

Most helpful comment

You should be able to use this snippet, see docs

if (process.env.NODE_APP_INSTANCE === 0) {
  agenda.every('1 hours', 'refreshProfiles')
}

>All comments

You should be able to use this snippet, see docs

if (process.env.NODE_APP_INSTANCE === 0) {
  agenda.every('1 hours', 'refreshProfiles')
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

frontierpsycho picture frontierpsycho  路  90Comments

dreaddymck picture dreaddymck  路  35Comments

mohamedazouz picture mohamedazouz  路  43Comments

extensionsapp picture extensionsapp  路  35Comments

ColdFireIce picture ColdFireIce  路  35Comments