Self Hosting PM2 Variables and Flows

I have recently implemented on a self hosted version of directus multiple PM2 instances using the PM2_INSTANCES env variable, since then though directus flows that have on a scheduled chron-job are running multiple times in each instance which is causing all sorts of duplicate data.

Does anyone know a way to force flows to run in just one instance of the app when using PM2 in cluster mode with multiple instances?

4 Answers

4

Make sure that you Redis running, connected, and configured correctly.

Please see the Synchronization config here.

The SYNCHRONIZATION_STORE defaults to memory vs redis so you have to make sure you’re setting that explicitly.

Will this work with OpenSource Redis if I install that on the same server as Directus? Or are there limitations that make the cloud based Redis preferable?

–

If you are hosting via docker(-compose) most easy is to use a recent -single- instance of redis. Cluster is not supported.

–

Do you have Redis enabled and configured correctly?

Directus will always try to execute a cron but they have made a cron locking mechanism that relies on Redis. This will prevent multiple cronjobs from executing at the same time.

Thanks both! I had set cache: redis using just the default config but appears I have completely missed this part of setting Redis up. I will look into this guide and get that up and running

We were facing the same problem as we were syncing products from Shopify, so we moved it away from the Directus to a different inhouse NestJs service

If you still want to use Directus then you will have to move it into custom hooks, and their use Redis based locking

I hope that isnt the answer! I could probably make do without the timer job if need be but I hope theres a solution that allows timer triggered flows to operate in one of the instances only

–

No if you have multiple containers running w/ Redis configured it'll take a lock and only run the flow on one container at a time :+1:

–

We have a case where only one instance have to run the crons and the other should not, due to topology. Redis handles the sync for other flows. Is it possible to disable crons at one instance and let it run at the other from .env? (Hello btw, I'm new around here, and we are testing with 11.12.0)

–