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

Removing active/running jobs #473

Closed
ferronrsmith opened this issue Mar 29, 2017 · 11 comments
Closed

Removing active/running jobs #473

ferronrsmith opened this issue Mar 29, 2017 · 11 comments

Comments

@ferronrsmith
Copy link

Tried using the following to remove an active job. Is there a better way to do this ? I am still seeing the job in redis

    var q = queue.get(qName);
    return q.getJob(id).then(function (job) {
      job.releaseLock(job.lockKey()).then(function () {
        return job.remove();
      });
    });
@manast
Copy link
Member

manast commented Mar 29, 2017

The code seems to be correct. If you can write a test case that fails I can look into it.

@ferronrsmith
Copy link
Author

Ok will do

@ferronrsmith
Copy link
Author

It's strange that i tried to release the lock, then delete, but shows errors in my stacktrace

[queue-manager-1]2017-03-30T05:56:25.229464043Z Unhandled rejection Error: Could not get lock for job: 3ebadf00-14c0-11e7-b2b5-91515b0b1873. Cannot remove job.

[queue-manager-1]2017-03-30T05:56:25.229507753Z     at /usr/src/app/node_modules/bull/lib/job.js:316:13

[queue-manager-1]2017-03-30T05:56:25.229518898Z     at tryCatcher (/usr/src/app/node_modules/bluebird/js/release/util.js:16:23)

[queue-manager-1]2017-03-30T05:56:25.229527773Z     at Promise._settlePromiseFromHandler (/usr/src/app/node_modules/bluebird/js/release/promise.js:510:31)

[queue-manager-1]2017-03-30T05:56:25.229537154Z     at Promise._settlePromise (/usr/src/app/node_modules/bluebird/js/release/promise.js:567:18)

[queue-manager-1]2017-03-30T05:56:25.229548847Z     at Promise._settlePromise0 (/usr/src/app/node_modules/bluebird/js/release/promise.js:612:10)

[queue-manager-1]2017-03-30T05:56:25.229555024Z     at Promise._settlePromises (/usr/src/app/node_modules/bluebird/js/release/promise.js:691:18)

[queue-manager-1]2017-03-30T05:56:25.229559872Z     at Promise._fulfill (/usr/src/app/node_modules/bluebird/js/release/promise.js:636:18)

@manast
Copy link
Member

manast commented Mar 30, 2017

Does this happens all the time? since releasing the lock and then removing is not performed atomically, there may be another worker trying to lock the job during that small window. In which context are you tying to remove the jobs?

@ferronrsmith
Copy link
Author

yea seems to happen all the time for me. I am trying to remove a running job. What happens is there are long running jobs that might get stuck without returning a status, I wrote the above code to allow developers to kill the active job to free the queue.

@manast
Copy link
Member

manast commented Mar 30, 2017

why don't you try job.moveToFailed instead? You can use it giving it a reason as well:

job.moveToFailed(Error('job has been stuck for too long'))

It seems that the feature that you would like to have is some kind of TTL, where if a job takes more than the given time it is failed automatically. I will consider it for an upcoming release.

@manast
Copy link
Member

manast commented Mar 30, 2017

Feature request: #479

@ferronrsmith
Copy link
Author

I do realize that then I moveToFailed, the queue doesn't automatically start pending jobs, is this expected behavior ?

@pariola
Copy link

pariola commented Jul 1, 2018

Please any update on removing all jobs?

@dhamaniasad
Copy link

One way to do this is that you could spawn child processes from your main process that implement Queue.process(), and hold a reference to the child process in memory. When you want to kill active jobs, just kill the child process. You could use a worker for each queue, or a single worker for all queues, or any combination.

const execa = require("execa");

let filePath = path.join(__dirname, "./worker.js");
worker = execa(`node`, filePath);

// kill job
worker.kill();

@Torsten-ha1
Copy link

A simple way of cleaning all queues, freeing them from all jobs is to use following class:

export class CleanQueues {
	constructor() {}

	private getKeys = async (q) => {
		const multi = q.multi()
		multi.keys('*')
		const keys = await multi.exec()
		return keys[0][1]
	}

	private filterQueueKeys = (q, keys) => {
		const prefix = `${q.keyPrefix}:${q.name}`
		return keys.filter((k) => k.includes(prefix))
	}

	private deleteKeys = async (q, keys) => {
		const multi = q.multi()
		keys.forEach((k) => multi.del(k))
		await multi.exec()
	}

	/**
	 * @param queue
	 */
	emptyQueue = async (q) => {
		const keys = await this.getKeys(q)
		const queueKeys = this.filterQueueKeys(q, keys)
		await this.deleteKeys(q, queueKeys)
	}
}

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

No branches or pull requests

5 participants