Home
Blog
Product

Announcing Scheduled Jobs API

Nov 27, 2023 by

Kasper avatar

Kasper Kristensen

Kasper avatar

Kasper Kristensen

Our improved Scheduled Jobs API allows developers to schedule recurring tasks using simple JavaScript functions.

Announcing Scheduled Jobs API  - Featured image

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.

// src/jobs/daily-erp-update.ts
import { 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 system
const 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 Copy to clipboard/jobs 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.
  • 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.

Track our progress on X and in our changelog.

Share this post

Try Medusa

Spin up your environment in a few minutes.