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

Delete non unique repeating jobs? #103

Open
piggydoughnut opened this issue Aug 3, 2017 · 6 comments
Open

Delete non unique repeating jobs? #103

piggydoughnut opened this issue Aug 3, 2017 · 6 comments

Comments

@piggydoughnut
Copy link

piggydoughnut commented Aug 3, 2017

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.

@lykmapipo
Copy link
Owner

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

@piggydoughnut
Copy link
Author

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 👽

@Arun-KumarH
Copy link

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 ?

@piggydoughnut
Copy link
Author

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) {})

@syedshahzebhasnain
Copy link

syedshahzebhasnain commented Jan 2, 2018

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)
}
})`

@pavelescurazvan
Copy link

Hello,

Any updates regarding the initial post issue?

Thank you

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