简体   繁体   English

是否可以在Kue Node.js中更新已创建的作业?

[英]Is it possible to update an already created job in Kue Node.js?

I am creating jobs using Kue . 我正在使用Kue创造就业机会。

jobs.create('myQueue', { 'title':'test', 'job_id': id ,'params':  params } )
            .delay(milliseconds)
            .removeOnComplete( true )
            .save(function(err) {
                if (err) {
                    console.log( 'jobs.create.err', err );
                }

});

Every job has a delay time, and normally it is 3 hours. 每个工作都有延迟时间,通常是3个小时。

Now I will to check every incoming request that wants to create a new job and get the id . 现在,我将检查每个想要创建新作业的传入请求并获取id。

As you can see from the above code, when I am creating a job I will add the job id to the job. 从上面的代码中可以看出,当我创建一个作业时,我会将作业ID添加到作业中。

So now I want to check the incoming id with the existing jobs' job_id s in the queue and update that existing job with the new parameters if a matching id found. 所以现在我想检查队列中现有作业'job_id'的传入id,如果找到匹配的id,则用新参数更新现有作业。

So my job queue will have unique job_id every time :). 所以我的工作队列每次都会有独特的job_id :)。

Is it possible? 可能吗? I have searched a lot, but no help found. 我搜索了很多,但没有找到帮助。 I checked the kue JSON API . 我检查了kue JSON API but it only can create and get retrieve jobs, can not update existing records. 但它只能创建和获取检索作业,无法更新现有记录。

This is not mentioned in the documentation and examples, but there is an update method for a job . 这在文档和示例中没有提到,但是有一个job更新方法。

You can update your jobs by job_id this way: 您可以通过job_id这种方式更新作业:

// you have the job_id
var job_id_to_update = 1;
// get delayed jobs
jobs.delayed( function( err, ids ) {
  ids.forEach( function( id ) {
    kue.Job.get( id, function( err, job ) {
      // check if this is job we want
      if (job.data.job_id === job_id_to_update) {
          // change job properties
          job.data.title = 'set another title';
          // save changes
          job.update();
      }
    });
  });
});

The full example is here . 完整的例子就在这里

Update: you can also consider using the "native" job ID, which is known for kue. 更新:您还可以考虑使用kue已知的“本机”作业ID。 You can get the job id when you create the job: 您可以在创建作业时获取作业ID:

var myjob = jobs.create('myQueue', ...
    .save(function(err) {
        if (err) {
            console.log( 'jobs.create.err', err );
        }
        var job_id = myjob.id;
        // you can send job_id back to the client
});

Now you can directly modify the job without looping over the list: 现在您可以直接修改作业而无需循环遍历列表:

kue.Job.get( id, function( err, job ) {
  // change job properties
  job.data.title = 'set another title';
  // save changes
  job.update();
});

Just wanted to post an update. 只是想发布更新。 This ticket https://github.com/Automattic/kue/issues/505 has the answer for my question. 这张票https://github.com/Automattic/kue/issues/505有我的问题的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM