docs: update scheduled jobs docs (#7905)
- Update scheduled jobs docs to support updated definition + remove coming soon notice - Update scheduled jobs everywhere else they're used.
This commit is contained in:
1 parent
6cb28eedf7
commit
0aeba83a66
7 files changed
+104
-74
No files matched your search
@@ -0,0 +1,40 @@
|
||||
export const metadata = {
|
||||
title: `${pageNumber} Scheduled Jobs Number of Executions`,
|
||||
}
|
||||
|
||||
# {metadata.title}
|
||||
|
||||
In this chapter, you'll learn how to set a limit on the number of times a scheduled job is executed.
|
||||
|
||||
## numberOfExecutions Option
|
||||
|
||||
The export configuration object of the scheduled job accepts an optional property `numberOfExecutions`. Its value is a number indicating how many times the scheduled job can be executed during the Medusa application's runtime.
|
||||
|
||||
For example:
|
||||
|
||||
export const highlights = [
|
||||
["9", "numberOfExecutions", "The number of times the job should be executed."]
|
||||
]
|
||||
|
||||
```ts highlights={highlights}
|
||||
export default async function myCustomJob() {
|
||||
console.log("I'll be executed three times only.")
|
||||
}
|
||||
|
||||
export const config = {
|
||||
name: "hello-world",
|
||||
// execute every minute
|
||||
schedule: "* * * * *",
|
||||
numberOfExecutions: 3,
|
||||
}
|
||||
```
|
||||
|
||||
The above scheduled job has the `numberOfExecutions` configuration set to `3`.
|
||||
|
||||
So, it'll only execute 3 times, each every minute, then it won't be executed anymore.
|
||||
|
||||
<Note>
|
||||
|
||||
If you restart the Medusa application, the scheduled job will be executed again until reaching the number of executions specified.
|
||||
|
||||
</Note>
|
||||
@@ -6,12 +6,6 @@ export const metadata = {
|
||||
|
||||
In this chapter, you’ll learn about scheduled jobs and how to use them.
|
||||
|
||||
<Note type="soon">
|
||||
|
||||
Scheduled jobs are coming soon.
|
||||
|
||||
</Note>
|
||||
|
||||
## What is a Scheduled Job?
|
||||
|
||||
A scheduled job is a function executed at a specified interval of time in the background of your Medusa application. It’s like a cron job that runs during the application's runtime.
|
||||
@@ -22,30 +16,23 @@ For example, you can synchronize your inventory with an Enterprise Resource Plan
|
||||
|
||||
## How to Create a Scheduled Job?
|
||||
|
||||
<Note type="check">
|
||||
|
||||
- [Redis](https://redis.io/) is installed and Redis server is running.
|
||||
|
||||
</Note>
|
||||
|
||||
A scheduled job is created in a TypeScript or JavaScript file under the `src/jobs` directory.
|
||||
|
||||
For example, create the file `src/jobs/hello-world.ts` with the following content:
|
||||
|
||||
```ts title="src/jobs/hello-world.ts"
|
||||
import { ScheduledJobConfig } from "@medusajs/medusa"
|
||||
|
||||
// the scheduled-job function
|
||||
export default function () {
|
||||
console.log("Time to say hello world!")
|
||||
}
|
||||
|
||||
// the job's configurations
|
||||
export const config: ScheduledJobConfig = {
|
||||
export const config = {
|
||||
name: "hello-world",
|
||||
// execute every minute
|
||||
schedule: "* * * * *",
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
A scheduled job file must export:
|
||||
@@ -59,20 +46,7 @@ This scheduled job executes every minute and logs into the terminal `Time to say
|
||||
|
||||
### Test Scheduled Jobs
|
||||
|
||||
To test out your scheduled job:
|
||||
|
||||
1. Uncomment the following line in the `medusa-config.js` file:
|
||||
|
||||
```js title="medusa-config.js"
|
||||
module.exports = defineConfig({
|
||||
projectConfig: {
|
||||
redisUrl: REDIS_URL,
|
||||
// ...
|
||||
},
|
||||
})
|
||||
```
|
||||
|
||||
2. Start the Medusa application:
|
||||
To test out your scheduled job, start the Medusa application:
|
||||
|
||||
```bash npm2yarn
|
||||
npm run dev
|
||||
@@ -81,12 +55,9 @@ npm run dev
|
||||
After a minute, the following message will be logged to the terminal:
|
||||
|
||||
```bash
|
||||
info: Processing scheduled job: hello-world
|
||||
Time to say hello world!
|
||||
```
|
||||
|
||||
The first line indicates that the application is executing the scheduled job. The second line is the text logged by the scheduled job.
|
||||
|
||||
---
|
||||
|
||||
## When to Use Scheduled Jobs
|
||||
@@ -115,32 +86,31 @@ The scheduled job function receives an object parameter that has a `container` p
|
||||
For example:
|
||||
|
||||
export const highlights = [
|
||||
["12", "resolve", "Resolve the Product Module's main service."],
|
||||
["12", "ModuleRegistrationName.PRODUCT", "The resource registration name imported from `@medusajs/modules-sdk`."]
|
||||
["11", "resolve", "Resolve the Product Module's main service."],
|
||||
["11", "ModuleRegistrationName.PRODUCT", "The resource registration name imported from `@medusajs/modules-sdk`."]
|
||||
]
|
||||
|
||||
```ts title="src/jobs/hello-world.ts" highlights={highlights}
|
||||
import {
|
||||
ScheduledJobArgs,
|
||||
ScheduledJobConfig,
|
||||
} from "@medusajs/medusa"
|
||||
import { IProductModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
IProductModuleService,
|
||||
MedusaContainer,
|
||||
} from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export default async function ({
|
||||
container,
|
||||
}: ScheduledJobArgs) {
|
||||
const productModuleService: IProductModuleService =
|
||||
export default async function myCustomJob(
|
||||
container: MedusaContainer
|
||||
) {
|
||||
const productModuleService: IProductModuleService =
|
||||
container.resolve(ModuleRegistrationName.PRODUCT)
|
||||
|
||||
const [, count] = await productModuleService.listAndCount()
|
||||
const [, count] = await productModuleService.listAndCountProducts()
|
||||
|
||||
console.log(
|
||||
`Time to check products! You have ${count} product(s)`
|
||||
)
|
||||
}
|
||||
|
||||
export const config: ScheduledJobConfig = {
|
||||
export const config = {
|
||||
name: "hello-world",
|
||||
// execute every minute
|
||||
schedule: "* * * * *",
|
||||
|
||||
@@ -156,16 +156,13 @@ To execute the workflow, invoke it passing the Medusa container as a parameter.
|
||||
</CodeTab>
|
||||
<CodeTab label="Scheduled Job" value="scheduled-job">
|
||||
|
||||
```ts title="src/jobs/message-daily.ts" highlights={[["10"], ["11"], ["12"], ["13"], ["14"], ["15"]]}
|
||||
import {
|
||||
type ScheduledJobConfig,
|
||||
type ScheduledJobArgs,
|
||||
} from "@medusajs/medusa"
|
||||
```ts title="src/jobs/message-daily.ts" highlights={[["7"], ["8"], ["9"], ["10"], ["11"], ["12"]]}
|
||||
import { MedusaContainer } from "@medusajs/types"
|
||||
import myWorkflow from "../workflows/hello-world"
|
||||
|
||||
export default async function handler({
|
||||
container,
|
||||
}: ScheduledJobArgs) {
|
||||
export default async function myCustomJob(
|
||||
container: MedusaContainer
|
||||
) {
|
||||
const { result } = await myWorkflow(container)
|
||||
.run({
|
||||
input: {
|
||||
@@ -176,10 +173,10 @@ To execute the workflow, invoke it passing the Medusa container as a parameter.
|
||||
console.log(result.message)
|
||||
}
|
||||
|
||||
export const config: ScheduledJobConfig = {
|
||||
export const config = {
|
||||
name: "run-once-a-day",
|
||||
schedule: `0 0 * * *`,
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
|
||||
@@ -179,6 +179,15 @@ export const sidebar = sidebarAttachHrefCommonOptions(
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
title: "Scheduled Jobs",
|
||||
children: [
|
||||
{
|
||||
path: "/advanced-development/scheduled-jobs/execution-number",
|
||||
title: "Execution Number",
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
title: "Workflows",
|
||||
children: [
|
||||
|
||||
Reference in new issue
Block a user