lykmapipo/kue-scheduler

Ability to fire onComplete and onFailAttempt events?

chrisli30 opened this issue · 2 comments

Hi,

I wonder if kue-scheduler has the ability to fire events upon completion, succeed or fail attempts.
Similar to Kue job events.
job.on('complete', function(result) {
console.log('Job completed with data ', result);

        }).on('failed attempt', function(errorMessage, doneAttempts) {
            console.log('Job failed');

        }).on('failed', function(errorMessage) {
            console.log('Job failed');

        }).on('progress', function(progress, data) {
            console.log('\r  job #' + job.id + ' ' + progress + '% complete with data ', data);

        }).on('error',function(err){
            console.log('Job err');
            console.log(err);
        });

Thanks.

Got time to dig deep and figured it out myself. The job object returned from createJob is not the same as the one in process(function(job)). Queue.on('schedule success') has access to the actual job instance and runs after _buildJob in Kue-Scheduler, so we can achieve onComplete and onFail event setup like below.

Queue.on('schedule success', function(job) {
if (jobObj.type === jobName) {
job.on('complete', function(result) {
console.log('Job completed with data ', result);
}).on('failed attempt', function(errorMessage, doneAttempts) {
console.log('Job failed');
}).on('failed', function(errorMessage) {
console.log('Job failed');
}).on('progress', function(progress, data) {
console.log('\r job #' + job.id + ' ' + progress +
'% complete with data ', data);
});
}
});

Hello @chrisli30.

Initial implementation for kue-scheduler tried to leave all heavy lifting to kue and thats why we fire events to allow fine hook-points to work with codes that are targeted to kue or more integrations.

Now, are onComplete and onFailAttempt related to job instance or schedules. If its job instance i will appreciate to utilize kue api otherwise we can resonate what to do.

Thanks.