kue-scheduler icon indicating copy to clipboard operation
kue-scheduler copied to clipboard

Delete non unique repeating jobs?

Open piggydoughnut opened this issue 7 years ago • 6 comments

Hello,

I am a bit confused over here. I understand how to schedule repeating jobs with Queue.every('2 seconds', job); But I don't understand how to stop non unique repeating jobs? Let's say I want a job to run every month until some precondition fails. So my worker takes the job, checks precondition and if the precondition fails then the worker stops the repetition of that job.

Thank you in advance for any advice.

piggydoughnut avatar Aug 03 '17 13:08 piggydoughnut

@piggydoughnut May you please share a scenario for the above.

lykmapipo avatar Aug 03 '17 13:08 lykmapipo

Scenario

I have a project. I would like to remind people every month about how awesome my project is. But I only want these reminders to run until the end of the project. So I was imagining I will create repeated notifications and once the precondition is not satisfied ( today > project.endDate ) then I will remove the recurring event.

This is a simplification of what I got

Job scheduler

const KUE = require('kue-scheduler');
const QUEUE = KUE.createQueue({
	restore: true,
	worker: false,
});

/*
schedules a job in kue task queue

data: anything that needs to be passed to the job
*/
export function scheduleJob(data: any) {
	const job = QUEUE.createJob('email', data.payload)
		.attempts(5)
		.backoff({ delay: 60000, type: 'exponential' })
		.delay(data.payload.delayedBy)
		.removeOnComplete(true);

	if (data.payload.repeat) {
		QUEUE.every(data.payload.repeat, job);
	}

	return job.save((err: any, res: any) => {
		if (err) {
			return err;
		}
	});
}

Worker

const KUE = require('kue-scheduler');
const QUEUE = KUE.createQueue({
	restore: true,
	worker: true,
});

QUEUE.process('email', async (job: any, done: any) => {

	try {
	   // will throw Exception if the job cannot be processed or will return nothing = the job continues to be processed
		checkPrecondition(job);
	} catch (e) {
		done(e.code, e.message);
	}

	// send an email

	done();

});

So some of the jobs repeat as I got in the Scheduler. But if I just create non_unique jobs, so with each repeat a new job is created, I do not know how to stop them. I had a little incident today, with jobs running every 5 second and I didn't know how to stop the actual recurring event programatically.

I hope this clarifies and provides you with enough information 😸 Please lettme know if you need me to specify more or add more info.

Thx 👽

piggydoughnut avatar Aug 03 '17 16:08 piggydoughnut

But is there any way to stop the unique job as well ? (Each and every time I remove the job from redis a new job gets posted). So I do not know how to stop the recurring the jobs (either unique or non-unique). Is there any way to stop the recurring jobs ?

Arun-KumarH avatar Oct 05 '17 08:10 Arun-KumarH

Each unique job has a name. In order to stop it you gotta remove it by its name.

To stop unique and recurring jobs you gotta use the following:

//using criteria
Queue.remove({
    unique: 'every_mail'
}, function(error, response) {})

piggydoughnut avatar Oct 06 '17 03:10 piggydoughnut

Queue.remove doesn't work for me.

`function scheduleTask (data, done) { var job = queue.createJob('scheduleTask', data) .unique('scheduleTask') .priority('normal') .removeOnComplete(false) .attempts(3) .backoff(true) .save(err => { if (err) { console.error(err) done(err) } if (!err) { done() } }) queue.every('2 seconds', job) } /************** Unschedular****************/ function unscheduleTask () { console.log('Cancel the task at hand') queue.remove({ unique: 'scheduleTask' }, function (error, job) { if (!error) { console.log(job) } }) }

/** ************ Workers **************************/

queue.process('scheduleTask', 1, function (job, done) { try { console.log('here') done() } catch (err) { done(err) } })`

syedshahzebhasnain avatar Jan 02 '18 22:01 syedshahzebhasnain

Hello,

Any updates regarding the initial post issue?

Thank you

pavelescurazvan avatar Apr 04 '18 08:04 pavelescurazvan