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

Scheduled tasks #3463

Merged
merged 14 commits into from
Apr 9, 2025
Merged

Scheduled tasks #3463

merged 14 commits into from
Apr 9, 2025

Conversation

michaelbromley
Copy link
Member

Description

Relates to #1425

This PR implements a new mechanism and set of APIs which allows us to define scheduled tasks directly in Vendure and Vendure Plugins.

It solves the fundamental problem of solutions like @nestjs/schedule by guaranteeing that a given task will only execute exactly once for a given schedule period, even if we are running in a horizontally-scaled environment with multiple server and worker instances.

It does this by using a shared locking mechanism. By default this is done using a new database table, which allows each task to be tracked and locked across instances.

The API looks like this:

import { ScheduledTask } from '@vendure/core';

export const cleanSessionsTask= new ScheduledTask({
  id: 'clean-expired-sessions',
  description: 'Clean up expired and inactive sessions from the database',
  schedule: cron => cron.every(12).hours(),
  execute: async (injector, params) => {
      // some logic here
  },
});
export const config: VendureConfig = {
  // ...
  schedulerOptions: {
    tasks: [
      cleanSessionsTask,
    ],
  }
};

Breaking changes

We export a new DefaultSchedulerPlugin which includes a new database entity, so when adding this to a project, a database migration will be required.

Copy link

vercel bot commented Apr 4, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
docs ✅ Ready (Inspect) Visit Preview Apr 9, 2025 11:40am

It was automatically removing the `standalone` property from components, breaking the build.
@michaelbromley michaelbromley merged commit ede754f into master Apr 9, 2025
25 checks passed
@michaelbromley michaelbromley deleted the scheduled-tasks branch April 9, 2025 11:56
@github-actions github-actions bot locked and limited conversation to collaborators Apr 9, 2025
@michaelbromley michaelbromley restored the scheduled-tasks branch April 9, 2025 13:45
@michaelbromley michaelbromley deleted the scheduled-tasks branch April 9, 2025 13:48
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant