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

Killing processes #1950

Open
LeoRuspini opened this issue Jan 11, 2021 · 4 comments
Open

Killing processes #1950

LeoRuspini opened this issue Jan 11, 2021 · 4 comments

Comments

@LeoRuspini
Copy link

LeoRuspini commented Jan 11, 2021

Description

Hi all,
thanks for this great work.
I been researching the killing of waiting and active jobs (#114, #1098, #812, #1432). I see the need for a killing method and there is several suggested "solutions", however, none of them seems to work properly plus they involved a lot of extra code (connection to ioredis, broadcast, etc). So in same setup as #1432, after discard and moveToFailed in provider, the job is marked as failed but still kept in waiting ( job.isWaiting(): true, job.isFailed(): true). The current example covers how to kill a job from the local server that run the process. However, the jobPromise.cancel() (described in the docs) seems to be removed and I can't find a method to remove from waiting state. If these two methods would be available, then all this can be added as a one line .kill(jobId) method (from the provider), which, from my point of view, is needed by many users.

Minimal, Working Test code to reproduce the issue.

(An easy to reproduce test case will dramatically decrease the resolution time.)

provider.js

let job = await queue.getJob(8)

job.discard();
job.moveToFailed(new Error('cancelled'), true)
// the two lines above should be replaced by a kill(jobId) method

worker.js

queue.process(Job', 2, __dirname + '/job.js')

queue.on('active', async (job, jobPromise) => {
    console.log("Worker: Active ", job.id)
    var idx = setInterval(async () => {
        isActive = await job.isActive()
        isCompleted = await job.isCompleted()
        isFailed = await job.isFailed()
        console.log("Worker: Checking job ", job.id, isActive, isCompleted, isFailed)
        if (isActive && (isCompleted || isFailed)) {
            console.log("Should kill job", job.id)
            // jobPromise.cancel()
        }
        if (!isActive) {
            console.log("Leaving interval", job.id)
            return clearInterval(idx)
        }
    }, 500)
})

queue.on('global:failed', async (jobId) => {
    console.log("Worker: Job ", jobId, " got globally failed ")
    // Remove ??
    queue.getJob(jobId)
        .then(async (job) => {
            console.log("Worker: Should I remove locally Job ", job.id)
            let waiting = await job.isWaiting()
            console.log("Worker: Job", job.id, waiting, failed)
            if(waiting){
                console.log("Should remove from waiting job", job.id)
                // job.removeWaiting()
            }
        })
        .catch((e) => {
            console.log("Worker:Not able to get job", jobId, e)
        })
})

queue.process(1, __dirname + "/" + 'process.js')

Bull version

3.20.0

Additional information

Thanks a lot !!!

@tjhiggins
Copy link
Contributor

@manast wanted to bump this one since as mentioned the docs seem to be incorrect at least for sandboxed processes
https://github.com/OptimalBits/bull/blob/v3.22.4/REFERENCE.md#events

.on('active', function(job, jobPromise){
  // A job has started. You can use `jobPromise.cancel()`` to abort it.
  jobPromise.cancel()
})

throws error TypeError: job_promise.cancel is not a function

Also would love to know the recommended way to kill processes.

@stale stale bot added the wontfix label Jul 12, 2021
@stale stale bot removed the wontfix label Jul 15, 2021
@OptimalBits OptimalBits deleted a comment from stale bot Jul 15, 2021
@alolis
Copy link
Contributor

alolis commented Aug 16, 2021

@tjhiggins, @LeoRuspini, you can also try this. Works like a charm for me.

@iflairritesh
Copy link

iflairritesh commented Dec 30, 2022

Hey @alolis
Can you please tell how it will work with @nestjs/bull?

@alolis
Copy link
Contributor

alolis commented Jan 1, 2023

Hey @alolis Can you please tell how it will work with @nestjs/bull?

I am afraid I do not have experience with nestjs. However, I have described my whole approach in detail here. It might be helpful to you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants