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

ghost picture ghost  路  3Comments

jubairsaidi picture jubairsaidi  路  3Comments

phra picture phra  路  3Comments

waygee picture waygee  路  4Comments

rajendar38 picture rajendar38  路  3Comments