We are excited to announce a new Scheduled Jobs API that significantly improves the developer experience of a core concept in Medusa’s extensibility toolbox.
Scheduled Jobs
Scheduled Jobs in Medusa is a feature that lets developers schedule tasks or cron jobs to automate repetitive tasks. For example, you can push daily updates to an external ERP system or generate weekly sales reports.
With the new Scheduled Jobs API, setting up recurring tasks can be achieved with only a few lines of code with a syntax that is familiar to any JavaScript developer.
123456789101112131415161718// src/jobs/daily-erp-update.tsimport { ProductService, ScheduledJobsConfig, ScheduledJobsArgs } from "@medusajs/medusa"import ErpService from "../services/erp"export default async function ({ container }: ScheduledJobsArgs) {const productService: ProductService = container.resolve("productService")// Service for interacting with external ERP systemconst erpService: ErpService = container.resolve("erpService")const products = await productService.listAndCount()await erpService.update(products)}export const config: ScheduledJobsConfig = {name: "daily-erp-update",schedule: "0 0 * * *" // At 00:00 every night}
The above example illustrates a few key points of how to use the new Scheduled Jobs API:
- Exports from files in
control the behavior of the action performed in your task. The default export is a function with the logic to run, and the config specifies the name and schedule of the task.Copy to clipboard/jobs
- The handler is a function that gets invoked each time the job runs. In the given example, it lists products in Medusa and forwards them to the ERP system.
- The config assigns a name to the job and sets the schedule for the job. In the provided example, the schedule is set to repeat the job every night at 00:00.
You can read more about the new API in our documentation.
We are excited to hear your thoughts about our new Scheduled Jobs API. If you have any feedback or experience issues, don’t hesitate to post on our GitHub Issues board or GitHub Discussions.
Share this post