`
flex_莫冲
  • 浏览: 1092418 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

kue api文档说明

阅读更多
## 创建job queue job队列
var jobs = kue.createQueue();

##创建job
var job = queue.create('email', {
    title: 'welcome email for tj'
  , to: 'tj@learnboost.com'
  , template: 'welcome-email'
}).save( function(err){
   if( !err ) console.log( job.id );
});

##设置job优先级
queue.create('email', {
    title: 'welcome email for tj'
  , to: 'tj@learnboost.com'
  , template: 'welcome-email'
}).priority('high').save();

{
    low: 10
  , normal: 0
  , medium: -5
  , high: -10
  , critical: -15
};

##失败后重试次数
queue.create('email', {
     title: 'welcome email for tj'
   , to: 'tj@learnboost.com'
   , template: 'welcome-email'
}).priority('high').attempts(5).save();

##失败后重试的延迟设置
// Honor job's original delay (if set) at each attempt, defaults to fixed backoff 采用默认delay设置
    job.attempts(3).backoff( true )

// Override delay value, fixed backoff 延迟1分钟
    job.attempts(3).backoff( {delay: 60*1000, type:'fixed'} )

    // Enable exponential backoff using original delay (if set)
    job.attempts(3).backoff( {type:'exponential'} )

    // Use a function to get a customized next attempt delay value
    job.attempts(3).backoff( function( attempts, delay ){
      return my_customized_calculated_delay;
    })

##job有效时间 TTL
queue.create('email', {title: 'email job with TTL'}).ttl(milliseconds).save();

##job log
job.log('$%d sent to %s', amount, user.name);

##job进度
job.progress(completed, total [, data])
job.progress(frames, totalFrames);

##job event
event types
- `enqueue` the job is now queued
- `promotion` the job is promoted from delayed state to queued
- `progress` the job's progress ranging from 0-100
- `failed attempt` the job has failed, but has remaining attempts yet
- `failed` the job has failed and has no remaining attempts
- `complete` the job has completed
- `remove` the job has been removed

var job = queue.create('video conversion', {
    title: 'converting loki\'s to avi'
  , user: 1
  , frames: 200
});

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

});

Note Kue stores job objects in memory until
they are complete/failed to be able to emit events on them.
If you have a huge concurrency in uncompleted jobs,
turn this feature off and use queue level events for better memory scaling.
job对象是保存在内存的,因此如果有大量未完成的并发jobs,建议将jobEvents特性关闭,
并使用队列级别的events
kue.createQueue({jobEvents: false})

##queue events
queue.on('job enqueue', function(id, type){
  console.log( 'Job %s got queued of type %s', id, type );

}).on('job complete', function(id, result){
  kue.Job.get(id, function(err, job){
    if (err) return;
    job.remove(function(err){
      if (err) throw err;
      console.log('removed completed job #%d', job.id);
    });
  });
});

##delay jobs 延迟job执行
var email = queue.create('email', {
    title: 'Account renewal required'
  , to: 'tj@learnboost.com'
  , template: 'renewal-email'
}).delay(milliseconds)
  .priority('high')
  .save();
 
##processing jobs 处理job
process是个单例对象
Note that unlike what the name createQueue suggests,
it currently returns a singleton Queue instance.
So you can configure and use only a single Queue object within your node.js process.

var kue = require('kue')
, queue = kue.createQueue();

queue.process('email', function(job, done){
  email(job.data.to, done);
});

function email(address, done) {
  if(!isValidEmail(address)) {
    //done('invalid to address') is possible but discouraged
    return done(new Error('invalid to address'));
  }
  // email send stuff...
  done();
}

##process Concurrency并发
By default a call to queue.process() will only accept one job at a time
for processing. For small tasks like sending emails this is not ideal,
so we may specify the maximum active jobs for this type by passing a number:
第二个参数的数字表示jobs并发数量

queue.process('email', 20, function(job, done){
  // ...
});

##pause process 暂停和恢复任务处理
queue.process('email', function(job, ctx, done){
  ctx.pause( 5000, function(err){
    console.log("Worker is paused... ");
    setTimeout( function(){ ctx.resume(); }, 10000 );
  });
});

##update progress 更新进度
// 创建job
queue.create('slideshow pdf', {
    title: user.name + "'s slideshow"
  , slides: [...] // keys to data stored in redis, mongodb, or some other store
});

queue.process('slideshow pdf', 5, function(job, done){
  var slides = job.data.slides
    , len = slides.length;

  function next(i) {
    var slide = slides[i]; // pretend we did a query on this slide id ;)
    job.log('rendering %dx%d slide', slide.width, slide.height);
    renderSlide(slide, function(err){
      if (err) return done(err);
      job.progress(i, len, {nextSlide : i == len ? 'itsdone' : i + 1});
      if (i == len) done()
      else next(i + 1);
    });
  }

  next(0);
});


##Graceful shutdown 优雅的停止
showdown 信号 的两种处理方式
.在给定的时间内,等jobs正在处理的job都执行完毕,所有workers都停止
.所有正在处理的job变成失败状态,并记录失败原因为shutdown

Queue#shutdown([timeout,] fn) signals all workers to stop processing
after their current active job is done.

Workers will wait timeout milliseconds for their active job's done
to be called or mark the active job failed with shutdown error reason.
When all workers tell Kue they are stopped fn is called.

var queue = require('kue').createQueue();

process.once( 'SIGTERM', function ( sig ) {
  queue.shutdown( 5000, function(err) {
    console.log( 'Kue shutdown: ', err||'' );
    process.exit( 0 );
  });
});
Note that shutdown method signature is changed from Kue >=0.9.0 to move the callback function to the last.

##Error Handling

var queue = require('kue').createQueue();

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

##Prevent from Stuck Active Jobs 防止jobs一直处于active状态
Kue marks a job complete/failed when done is called by your worker,
so you should use proper error handling to prevent uncaught exceptions
in your worker's code and node.js process exiting before in handle jobs get done.
This can be achieved in two ways:

1 Wrapping your worker's process function in Domains(domain是node.js的异常处理模块)
queue.process('my-error-prone-task', function(job, done){
  var domain = require('domain').create();
  domain.on('error', function(err){
    done(err);
  });
  domain.run(function(){ // your process function
    throw new Error( 'bad things happen' );
    done();
  });
});

##Queue Maintenance
Queue object has two type of methods to tell you about the number of jobs in each state

queue.inactiveCount( function( err, total ) { // others are activeCount, completeCount, failedCount, delayedCount
  if( total > 100000 ) {
    console.log( 'We need some back pressure here' );
  }
});

you can also query on an specific job type:

queue.failedCount( 'my-critical-job', function( err, total ) {
  if( total > 10000 ) {
    console.log( 'This is tOoOo bad' );
  }
});

##Programmatic Job Management

##Redis Connection Settings
定义redis 数据库连接
var kue = require('kue');
var q = kue.createQueue({
  prefix: 'job',
  redis: {
    port: 1234,
    host: '10.0.50.20',
    auth: 'password',
    db: 0, // if provided select a non-default redis db
    options: {
      // see https://github.com/mranney/node_redis#rediscreateclient
    }
  }
});
或者这种方式
var q = kue.createQueue({
  redis: 'redis://example.com:1234?redis_option=value&redis_option=value'
});
##Connecting using Unix Domain Sockets
或者通过socket方式
var kue = require('kue');
var q = kue.createQueue({
  prefix: 'q',
  redis: {
    socket: '/data/sockets/redis.sock',
    auth: 'password',
    options: {
      // see https://github.com/mranney/node_redis#rediscreateclient
    }
  }
});

##User-Interface
express.js开发的ui

##Third-party interfaces
https://github.com/StreetHub/kue-ui

##JSON API
###查询jobs
Query jobs, for example "GET /job/search?q=avi video":
GET /job/search?q=

在创建job的时候要加上searchKeys
var kue = require('kue');
queue = kue.createQueue();
queue.create('email', {
    title: 'welcome email for tj'
  , to: 'tj@learnboost.com'
  , template: 'welcome-email'
}).searchKeys( ['to', 'title'] ).save();

在创建队列的时候要开启search设置,默认是关闭的
var kue = require('kue');
q = kue.createQueue({
    disableSearch: false
});

##GET /stats
获取jobs统计数据
http://127.0.0.1:3000/stats
Currently responds with state counts, and worker activity time in milliseconds:

{"inactiveCount":0,"completeCount":0,"activeCount":0,"failedCount":0,"delayedCount":7,"workTime":null}

##GET /job/:id
Get a job by :id:
http://127.0.0.1:3000/job/7
{"id":"3","type":"email","data":{"title":"welcome email for tj","to":"tj@learnboost.com","template":"welcome-email"},"priority":-10,"progress":"100","state":"complete","attempts":null,"created_at":"1309973155248","updated_at":"1309973155248","duration":"15002"}

##GET /job/:id/log

##DELETE /job/:id
Delete job :id:

$ curl -X DELETE http://local:3000/job/2
{"message":"job 2 removed"}

##POST /job
通过API创建jobs
Create a job:

$ curl -H "Content-Type: application/json" -X POST -d \
    '{
       "type": "email",
       "data": {
         "title": "welcome email for tj",
         "to": "tj@learnboost.com",
         "template": "welcome-email"
       },
       "options" : {
         "attempts": 5,
         "priority": "high"
       }
     }' http://localhost:3000/job
{"message": "job created", "id": 3}

You can create multiple jobs at once by passing an array. In this case, the response will be an array too, preserving the order:

$ curl -H "Content-Type: application/json" -X POST -d \
    '[{
       "type": "email",
       "data": {
         "title": "welcome email for tj",
         "to": "tj@learnboost.com",
         "template": "welcome-email"
       },
       "options" : {
         "attempts": 5,
         "priority": "high"
       }
     },
     {
       "type": "email",
       "data": {
         "title": "followup email for tj",
         "to": "tj@learnboost.com",
         "template": "followup-email"
       },
       "options" : {
         "delay": 86400,
         "attempts": 5,
         "priority": "high"
       }
     }]' http://localhost:3000/job
[
    {"message": "job created", "id": 4},
    {"message": "job created", "id": 5}
]
Note: when inserting multiple jobs in bulk, if one insertion fails Kue
will keep processing the remaining jobs in order.
The response array will contain the ids of the jobs added successfully,
and any failed element
will be an object describing the error: {"error": "error reason"}.

##Parallel Processing With Cluster
并行处理(使用cluster和wokers)
var kue = require('kue')
  , cluster = require('cluster')
  , queue = kue.createQueue();

var clusterWorkerSize = require('os').cpus().length;

if (cluster.isMaster) {
  kue.app.listen(3000);
  for (var i = 0; i < clusterWorkerSize; i++) {
    cluster.fork();
  }
} else {
  queue.process('email', 10, function(job, done){
    var pending = 5
      , total = pending;

    var interval = setInterval(function(){
      job.log('sending!');
      job.progress(total - pending, total);
      --pending || done();
      pending || clearInterval(interval);
    }, 1000);
  });
}

##Securing Kue
var app = express.createServer({ ... tls options ... });
app.use(express.basicAuth('foo', 'bar'));
app.use(kue.app);
app.listen(3000);
分享到:
评论

相关推荐

    前端开源库-kue

    7. **API接口**:kue提供了丰富的API,让开发者可以通过JavaScript轻松地对作业队列进行操作,无论是创建新任务还是查询现有任务,都非常便捷。 使用kue,开发者可以构建出更稳定、高性能的Web应用,特别是在需要...

    前端开源库-kue-ui

    5. **创建和管理任务**:在应用代码中,使用Kue API创建任务,Kue-UI会自动同步并显示这些任务。 **总结** Kue-UI是Kue任务队列的可视化管理工具,它简化了前端开发者对后台任务的管理,提高了开发效率。通过实时...

    Node.js-基于kue的egg延时队列

    通过编写Egg.js插件,我们可以将Kue的API封装起来,使得在Egg.js应用中添加、删除和查询延时任务变得更加便捷。以下是一些可能的步骤: 1. 安装依赖:在项目中安装`kue`和`egg-kue`,`egg-kue`是Egg.js的Kue插件。 ...

    Node.js-Kue是一款为node.js而构建由redis支持的优先级队列

    Kue提供了丰富的API接口,用于创建、更新、删除和查询任务。例如,你可以使用`job.create()`来创建新任务,`job.promote()`提升任务优先级,`job.complete()`标记任务完成,以及`job.remove()`删除任务。同时,Kue还...

    kue-scheduler:用于kue的Job Scheduler实用程序,由redis支持并为node.js构建

    kue-scheduler 一个针对Job Scheduler实用程序,由支持并为构建,并支持在系统重启时恢复计划。 Scheduling API受到了很大的启发,并从和其他方面借鉴而来。 注意!:如果提供的kue选项具有许可权,则默认情况下...

    kue-env:从环境初始化一个作业kue

    kue-env kue.createQueue 的简单包装器,用于从环境初始化 redis 设置安装使用 npm 安装 $ npm install kue-env例子 var kue = require ( 'kue' )var jobs = require ( 'kue-env' ) ( kue )执行 var env = process ....

    vertx-kue, Vert.x Blueprint项目 Vert.x Kue,这是由 Vert.x 支持的优先级任务队列.zip

    vertx-kue, Vert.x Blueprint项目 Vert.x Kue,这是由 Vert.x 支持的优先级任务队列 Vert.x Kue Vert.x Kue 是用 Vert.x 开发的优先级任务队列,并以的Redis支持。 它是 automattic/kue的Vert.x 实现版本。...详细文档

    基于node express快速开发api类后端的框架适用angularjs和ionic等前后端分离的项目源码.zip

    技术栈 base2(mirco kernel) mongoose bluebird res.api 前后端分离实践 前端:moa-frontend 和 moa-h5 public下面的采用nginx做反向代理 其他的采用express+jade精简代码...集成kue队列[需要使用mount-queues插件]

    kudo:使用 nodejs 进行概念证明 - kue - docker

    工藤 使用 nodejs 进行概念证明 - kue - docker

    kue-unique:独特的Kue作业实用程序

    要求 安装$ npm install --save kue kue-unique用法import kue from 'kue-unique' ;const queue = kue . createQueue ( ) ;// create and save unique jobconst job = queue . create ( 'email' , { title : '...

    taskrr:nodejs中使用kue的任务服务器

    nodejs中使用kue的任务服务器 如何使用 确保你有redis正在运行。 安装依赖, npm install Kue 带有一个很棒的 UI。 打开终端并cd到 taskrr 目录并运行 node viewjobs 然后浏览到localhost:3000 。 您可以配置它的...

    nestjs-kue:NestJS框架的Kue包装器

    安装$ npm install --save nestjs-kue 该模块使用REDIS进行操作,并利用以下环境变量和默认值作为配置: KUE_REDIS_PREFIX#默认'q' KUE_REDIS_HOST#默认为'localhost' KUE_REDIS_PORT#默认6379 KUE_REDIS_DB#...

    kue-chain:Chain Kue职位

    使用 Kue 我发现我系统中的大多数新作业都是在对其他一些作业完成的React后创建的。 在实现了一些辅助函数之后,它变得更好了一点,但它仍然在管理作业排序的代码中留下了很多重复的部分。 所以我走得更远,使正在...

    egg-kue:由redis支持的优先级作业队列,为eggjs构建

    $ npm i egg-kue --save 用法 // {app_root}/config/plugin.js exports . kue = { enable : true , package : 'egg-kue' , } ; 配置 // {app_root}/config/config.default.js 'use strict' ; exports . kue = { ...

    docker-kue-debugger:一个连接Redis对外模拟接收和执行kue作业的容器

    docker-kue-调试器 一个简单的容器,连接到 Redis 以从外部模拟接收和执行 kue 作业。 自定义命令的参数 [环境] KUE_PREFIX(无默认) [环境] REDIS_DATABASE(无默认值) [链接] = 你的 redis 容器 例子 docker ...

    mock-kue:蛋糕模拟

    模拟KUE是一个小的嘲弄库 。 它模拟 kue 中的trigger和process函数,同时在内部数组中维护作业。 然后,您可以检查作业计数并在任何给定时间运行它们。 kue 不会连接到 redis。安装节点.js: npm install mock-kue要...

    kue-helpers:Kue的简单,高级帮助程序(Node.js作业队列)

    帮手 简单,基于承诺的高级帮助程序 。 yarn add kue-helpers ... 返回一个承诺,该承诺将执行then on Kue的complete事件,并拒绝Kue的failed事件。 例子 const promise = enqueueJob('ExpensesBulk

    kue-ui:追踪k工作

    该存储库曾经包含有关如何为Kue启用此新UI的说明,现在它仅包含Ember应用程序本身。 要将Kue-Ui添加到现有的Express应用程序中,请参阅如何开始在app.js文件中配置Redis地址 node app.js打开localhost:3000 / kue-...

    kue-example:蛋糕 + Redis 示例

    Kue的API允许开发者方便地添加、检索和操作任务,使得在Web应用中处理复杂的后台作业变得简单。 Redis是一个内存数据结构存储系统,常被用作数据库、缓存和消息中间件。在Kue中,Redis作为主要的数据存储,存储了...

    kue:Kue 是一个 Rails 就绪的键值存储,在引擎盖下使用活动记录

    ###屈Kue 是一个支持 Rails 的键值存储,在幕后使用活动记录。 ###构建状态 ###Kue 是什么意思? 核心价值###真正的键值存储呢? Redis 太棒了! 但有时你只是不想要或不需要外部依赖!我如何安装酷? 安装宝石 gem ...

Global site tag (gtag.js) - Google Analytics