From afd79c9c3906d9c6c1aa4dfa5c6f1c8cc9d76d3e Mon Sep 17 00:00:00 2001 From: Shahed Nasser Date: Thu, 4 May 2023 20:02:34 +0300 Subject: [PATCH] docs: added details about overriding scheduled jobs (#4021) * docs: added details about overriding scheduled jobs * remove link * eslint fixes --- .../development/scheduled-jobs/create.md | 37 ++++++++++++++++--- 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/docs/content/development/scheduled-jobs/create.md b/docs/content/development/scheduled-jobs/create.md index 16027a1cef..1158e97123 100644 --- a/docs/content/development/scheduled-jobs/create.md +++ b/docs/content/development/scheduled-jobs/create.md @@ -33,12 +33,17 @@ For the example in this tutorial, you can create the file `src/loaders/publish.t --- -## 2. Create Cron Job +## 2. Create Scheduled Job To create a scheduled job, add the following code in the file you created, which is `src/loaders/publish.ts` in this example: ```ts title=src/loaders/publish.ts -const publishJob = async (container, options) => { +import { AwilixContainer } from "awilix" + +const publishJob = async ( + container: AwilixContainer, + options: Record +) => { const jobSchedulerService = container.resolve("jobSchedulerService") jobSchedulerService.create( @@ -74,8 +79,8 @@ This file should export a function that accepts a `container` and `options` para You then resolve the `JobSchedulerService` and use the `jobSchedulerService.create` method to create the scheduled job. This method accepts four parameters: -- The first parameter is a unique name to give to the scheduled job. In the example above, you use the name `publish-products`; -- The second parameter is an object which can be used to [pass data to the job](#pass-data-to-the-scheduled-job); +- The first parameter is a unique name to give to the scheduled job. In the example above, you use the name `publish-products`. +- The second parameter is an object which can be used to [pass data to the job](#pass-data-to-the-scheduled-job). - The third parameter is the scheduled job expression pattern. In this example, it will execute the scheduled job once a day at 12 AM. - The fourth parameter is the function to execute. This is where you add the code to execute once the scheduled job runs. In this example, you retrieve the draft products using the [ProductService](../../references/services/classes/ProductService.md) and update the status of each of these products to `published`. @@ -85,7 +90,27 @@ You can see examples of scheduled job expression patterns on [crontab guru](http ::: -### Pass Data to the Cron Job +### Scheduled Job Name + +As mentioned earlier, the first parameter of the `create` method is the name of the scheduled job. By default, if another scheduled job has the same name, your custom scheduled job will replace it. + +If you want to ensure both scheduled jobs are registered and used, you can pass as a fifth parameter an options object with a `keepExisting` property set to `true`. For example: + +```ts +jobSchedulerService.create( + "publish-products", + {}, + "0 0 * * *", + async () => { + // ... + }, + { + keepExisting: true, + } +) +``` + +### Pass Data to the Scheduled Job To pass data to your scheduled job, you can add them to the object passed as a second parameter under the `data` property. This is helpful if you use one function to handle multiple scheduled jobs. @@ -108,7 +133,7 @@ jobSchedulerService.create("publish-products", { :::info -Cron Jobs only run while the Medusa backend is running. +Scheduled Jobs only run while the Medusa backend is running. :::