Kue: [Question] A way to update data when processing a job?

Created on 11 Apr 2014  路  4Comments  路  Source: Automattic/kue

What i would like to do is when a job is processed, before completing, saving additional data to the data array for later retrieval.

for example:

// process.js-----------------------------------
var kue = require('kue')
    , express = require('express');

// create our job queue
jobs = kue.createQueue();

var data = {"text": "some text here", "title": "Process text save to file"};
var job = jobs.create('processFile', data).priority('normal').save(function () {
    console.log(job.id);
});


jobs.process('processFile', 2, function (job, done) {
    var text = job.data.text;

    var fileName = somefunctionSavesFile(text);

    // i want to save this to redis.
    job.data.filename = fileName;

    done();
});

function somefunctionSavesFile(text) {
    // creates a file with the text passed in.
    return "filename.txt";
}

// GetStatus.js-----------------------------------
//later we want to retrieve this data
var kue = require('kue');
kue.Job.get(id, function (err, job) {
    if (job._state == 'complete') {
        console.log(job.data.filename);
    } else {
        console.log('Not done yet');
    }
});

is anything like this possible?

Question

Most helpful comment

I use this code for save data during process:

 spooky.on('statusChange', function(data) {
    console.log('###############STATUS CHANGE EMIT##############');
    job.log('CasperJs Status Emit');
    //Update Status
    switch (data.status) {
      case 'login.failed':
        job.log('Error: Login Failed');
        job.data.status = "login.failed";
        job.update(function() {});
        break;
      case 'login.success':
        job.log('Info: Login Success');
        job.data.status = "login.success";
        job.update(function() {});
        break;
      case 'provider.offline':
        job.log('Error: Provider Offline');
        job.data.status = "provider.offline";
        job.update(function() {});
        break;
      case 'provider.failed':
        job.log('Error: Provider Failed');
        job.data.status = "provider.failed";
        job.update(function() {});
        break;
      default:
    }

  });

job.data.status = "login.failed";
job.update(function() {});

In ur case try:

   // i want to save this to redis.
    job.data.filename = fileName;
    job.update(function() {});

    done();

All 4 comments

I use this code for save data during process:

 spooky.on('statusChange', function(data) {
    console.log('###############STATUS CHANGE EMIT##############');
    job.log('CasperJs Status Emit');
    //Update Status
    switch (data.status) {
      case 'login.failed':
        job.log('Error: Login Failed');
        job.data.status = "login.failed";
        job.update(function() {});
        break;
      case 'login.success':
        job.log('Info: Login Success');
        job.data.status = "login.success";
        job.update(function() {});
        break;
      case 'provider.offline':
        job.log('Error: Provider Offline');
        job.data.status = "provider.offline";
        job.update(function() {});
        break;
      case 'provider.failed':
        job.log('Error: Provider Failed');
        job.data.status = "provider.failed";
        job.update(function() {});
        break;
      default:
    }

  });

job.data.status = "login.failed";
job.update(function() {});

In ur case try:

   // i want to save this to redis.
    job.data.filename = fileName;
    job.update(function() {});

    done();

awesome, this will work perfectly. Thanks :)

is there a new way to do this ? dosent seam to work any more

@thbl It does work, I just tried it. You just need to pass an empty callback function, update method will never call it. In your case I bet that you need to wait a couple of seconds for the update to propagate to Redis before doing something else on the job. I had to update the data on the completed job, and invalidate it again, so I did this:

job.data.param = "NEW VALUE";
job.update(()=>{});
await new Promise((resolve, reject)=>{setTimeout(resolve, 2000);});
job.invalidate();

First I tried to put the invalidation part inside of the callback - but it was never called. So timing out helped me here.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

RodH257 picture RodH257  路  3Comments

JacksonGariety picture JacksonGariety  路  10Comments

mogadanez picture mogadanez  路  3Comments

syadykin picture syadykin  路  8Comments

jibay picture jibay  路  6Comments