Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Update the jobs readme description #796

Merged
merged 5 commits into from
Jul 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 11 additions & 15 deletions src/jobs/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# Custom scheduled jobs

> Scheduled jobs are coming soon.

A scheduled job is a function executed at a specified interval of time in the background of your Medusa application.

A scheduled job is created in a TypeScript or JavaScript file under the `src/jobs` directory.
Expand All @@ -10,20 +8,20 @@ For example, create the file `src/jobs/hello-world.ts` with the following conten

```ts
import {
ProductService,
ScheduledJobArgs,
ScheduledJobConfig,
} from "@medusajs/medusa";
IProductModuleService,
MedusaContainer
} from "@medusajs/types";
import { ModuleRegistrationName } from "@medusajs/utils";

export default async function myCustomJob({ container }: ScheduledJobArgs) {
const productService: ProductService = container.resolve("productService");
export default async function myCustomJob(container: MedusaContainer) {
const productService: IProductModuleService = container.resolve(ModuleRegistrationName.PRODUCT)

const products = await productService.listAndCount();
const products = await productService.listAndCountProducts();

// Do something with the products
}

export const config: ScheduledJobConfig = {
export const config = {
name: "daily-product-report",
schedule: "0 0 * * *", // Every day at midnight
};
Expand All @@ -32,11 +30,9 @@ export const config: ScheduledJobConfig = {
A scheduled job file must export:

- The function to be executed whenever it’s time to run the scheduled job.
- A configuration object defining the job. It has two properties:
- A configuration object defining the job. It has three properties:
- `name`: a unique name for the job.
- `schedule`: a [cron expression](https://crontab.guru/).
- `numberOfExecutions`: an optional integer, specifying how many times the job will execute before being removed

The `handler` is a function which takes one parameter, an `object` of type `ScheduledJobArgs` with the following properties:

- `container` - a `MedusaContainer` instance which can be used to resolve services.
- `data` - an `object` containing data passed to the job when it was scheduled. This object is passed in the `config` object.
The `handler` is a function that accepts one parameter, `container`, which is a `MedusaContainer` instance used to resolve services.
Loading