lykmapipo / kue-scheduler

A job scheduler utility for kue, backed by redis and built for node.js
246 stars 47 forks source link

Delete non unique repeating jobs? #103

Open piggydoughnut opened 6 years ago

piggydoughnut commented 6 years ago

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 commented 6 years ago

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

piggydoughnut commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

Hello,

Any updates regarding the initial post issue?

Thank you