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

Canceling of job doesn't work #1127

Closed
Izayda opened this issue Sep 30, 2021 · 4 comments
Closed

Canceling of job doesn't work #1127

Izayda opened this issue Sep 30, 2021 · 4 comments
Assignees
Labels
type: bug 🐛 Something isn't working

Comments

@Izayda
Copy link
Contributor

Izayda commented Sep 30, 2021

Describe the bug
After starting reindexing i cancel job. But i can see job is still processed.
Video https://vendure-ecommerce.slack.com/archives/CKYMF0ZTJ/p1632919005096600
It was on redis job queue. But probably bug also exists in default job plugin.

To Reproduce
Steps to reproduce the behavior:

  1. Run a job
  2. Cancel it
  3. See logs that job is still processed

Expected behavior
Job is successfully canceled.

Environment (please complete the following information):

  • @vendure/core version: 1.2.2
  • Database (mysql/postgres etc): postgresql
@Izayda Izayda added the type: bug 🐛 Something isn't working label Sep 30, 2021
@michaelbromley
Copy link
Member

For BullMQ, there is an open issue to allow cancellation of running jobs: taskforcesh/bullmq#632

I will check into the DefaultJobQueuePlugin.

@michaelbromley
Copy link
Member

Ok, so on further investigation there is no easy way to cancel a running job, since the process function is Promise-based. As mentioned in the lined BullMQ issue above, there is the possibility of using the AbortController API but this is only stable in Node.js v16+, and I cannot see that it is natively integrated with Promises.

So one possible way to "cancel" a process function is to query the job state, and return from the function is the state is CANCELLED. Example:

process: async job => {
    let progress = 0;
    while (progress < 100) {

        if (job.state === JobState.CANCELLED) {
          // manually abort if job is cancelled
          return;
        }

        await new Promise(resolve => setTimeout(resolve, job.data.intervalMs));
        progress += 10;
        job.setProgress(progress);
    }
    Logger.info(`Completed job ${job.id}`);
    return 'Done!';
},

The problem is that to make all jobs cancellable, we would have to add such logic into all existing jobs, which in many cases would require quite a bit of refactoring.

@Izayda
Copy link
Contributor Author

Izayda commented Oct 5, 2021

Hmmm, yeah, no easy way. Is there problem in default job plugin?

@michaelbromley
Copy link
Member

In the default plugin, the job will appear as CANCELLED, but if the process function has started, it will continue until completion anyway. This is a fundamental issue with Promises unfortunately.

michaelbromley pushed a commit that referenced this issue Feb 10, 2022
michaelbromley added a commit that referenced this issue Feb 2, 2024
Relates to #1127, relates to #2650. This commit adds a mechanism to
track cancellation of jobs in Redis using pub/sub on a custom
set which tracks the IDs which have been cancelled.

The job process functions will still continue to execute unless
there is specific logic to check the job state and throw an
error on cancellation.
michaelbromley added a commit that referenced this issue Feb 2, 2024
Relates to #1127, relates to #2650. This commit alters slightly the way cancellation
is handled: when a job is cancelled, it's status is updated but the
job will continue to run unless the `process` function explicitly
throws an error.
michaelbromley added a commit that referenced this issue Feb 2, 2024
Relates to #1127, relates to #2650. This commit handles the case of a job
being cancelled for those built-in jobs that can be long-running.
michaelbromley added a commit that referenced this issue Feb 2, 2024
@michaelbromley michaelbromley moved this to 🏗 In progress in Vendure OS Roadmap Feb 2, 2024
@michaelbromley michaelbromley moved this from 🏗 In progress to ✅ Done in Vendure OS Roadmap May 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug 🐛 Something isn't working
Projects
Status: 🚀 Shipped
Development

No branches or pull requests

2 participants