Using @nestjs/schedule module #2464
-
Hello, Has anyone implemented cron jobs using the @nestjs/schedule module? I've created a plugin in which a provider uses the @Cron decorator. I have Vendure running with 2 separate workers and the problem is both workers and main app are registering the cron therefore triggering the same action 3 times. A way I found to handle this was to add the plugin when bootstraping one of the workers, and not add it to the main vendure-config file.
Is this good practice? (I guess I'm overwriting the plugins field in What if i want to register the plugin only on the main app and not having it picked up by the workers? Also, when working on this I noticed that all plugins are registered multiple times by the workers and main app. Is this normal? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi, Yes, just using the Cron decorator is not suitable for multiple instances. I discuss that in this thread, and long-term I would like to support scheduled tasks in a more native way. Your solution is fine, although the way you do it there will replace all the plugins with just the CronPlugin, which might not be what you want. From the docs:
So you'd want it like: const workerConfig = mergeConfig(config, { plugins: [...config.plugins, CronPlugin], });
Inject the ProcessContext into your class and use it to check whether it is being run in the server or worker context.
A plugin should be registered once for each instance (worker or server). If you are seeing otherwise and can reproduce it, open an issue please. |
Beta Was this translation helpful? Give feedback.
Hi,
Yes, just using the Cron decorator is not suitable for multiple instances. I discuss that in this thread, and long-term I would like to support scheduled tasks in a more native way.
Your solution is fine, although the way you do it there will replace all the plugins with just the CronPlugin, which might not be what you want. From the docs:
So you'd want it like: