lykmapipo / kue-scheduler

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

Ability to fire onComplete and onFailAttempt events? #105

Open chrisli30 opened 6 years ago

chrisli30 commented 6 years ago

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.

chrisli30 commented 6 years ago

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

lykmapipo commented 6 years ago

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.