8000 rename all jobs instances to queue · JavaScriptExpert/kue@d3ebf2b · GitHub
[go: up one dir, main page]

Skip to content

Commit d3ebf2b

Browse files
committed
rename all jobs instances to queue
1 parent dd21078 commit d3ebf2b

File tree

1 file changed

+55
-54
lines changed

1 file changed

+55
-54
lines changed

Readme.md

Lines changed: 55 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,13 @@ First create a job `Queue` with `kue.createQueue()`:
6464

6565
```js
6666
var kue = require('kue')
67-
, jobs = kue.createQueue();
67+
, queue = kue.createQueue();
6868
```
6969

70-
Calling `jobs.create()` with the type of job ("email"), and arbitrary job data will return a `Job`, which can then be `save()`ed, adding it to redis, with a default priority level of "normal". The `save()` method optionally accepts a callback, responding with an `error` if something goes wrong. The `title` key is special-cased, and will display in the job listings within the UI, making it easier to find a specific job.
70+
Calling `queue.create()` with the type of job ("email"), and arbitrary job data will return a `Job`, which can then be `save()`ed, adding it to redis, with a default priority level of "normal". The `save()` method optionally accepts a callback, responding with an `error` if something goes wrong. The `title` key is special-cased, and will display in the job listings within the UI, making it easier to find a specific job.
7171

7272
```js
73-
var job = jobs.create('email', {
73+
var job = queue.create('email', {
7474
title: 'welcome email for tj'
7575
, to: 'tj@learnboost.com'
7676
, template: 'welcome-email'
@@ -84,7 +84,7 @@ var job = jobs.create('email', {
8484
To specify the priority of a job, simply invoke the `priority()` method with a number, or priority name, which is mapped to a number.
8585

8686
```js
87-
jobs.create('email', {
87+
queue.create('email', {
8888
title: 'welcome email for tj'
8989
, to: 'tj@learnboost.com'
9090
, template: 'welcome-email'
@@ -108,7 +108,7 @@ The default priority map is as follows:
108108
By default jobs only have _one_ attempt, that is when they fail, they are marked as a failure, and remain that way until you intervene. However, Kue allows you to specify this, which is important for jobs such as transferring an email, which upon failure, may usually retry without issue. To do this invoke the `.attempts()` method with a number.
109109

110110
```js
111-
jobs.create('email', {
111+
queue.create('email', {
112112
title: 'welcome email for tj'
113113
, to: 'tj@learnboost.com'
114114
, template: 'welcome-email'
@@ -171,7 +171,7 @@ Job-specific events are fired on the `Job` instances via Redis pubsub. The follo
171171
For example this may look something like the following:
172172

173173
```js
174-
var job = jobs.create('video conversion', {
174+
var job = queue.create('video conversion', {
175175
title: 'converting loki\'s to avi'
176176
, user: 1
177177
, frames: 200
@@ -199,7 +199,7 @@ job.on('complete', function(result){
199199
Queue-level events provide access to the job-level events previously mentioned, however scoped to the `Queue` instance to apply logic at a "global" level. An example of this is removing completed jobs:
200200

201201
```js
202-
jobs.on('job enqueue', function(id, type){
202+
queue.on('job enqueue', function(id, type){
203203
console.log( 'Job %s got queued of type %s', id, type );
204204

205205
}).on('job complete', function(id, result){
@@ -221,7 +221,7 @@ Delayed jobs may be scheduled to be queued for an arbitrary distance in time by
221221
This automatically flags the `Job` as "delayed".
222222

223223
```js
224-
var email = jobs.create('email', {
224+
var email = queue.create('email', {
225225
title: 'Account renewal required'
226226
, to: 'tj@learnboost.com'
227227
, template: 'renewal-email'
@@ -233,21 +233,21 @@ var email = jobs.create('email', {
233233
When using delayed jobs, we must also check the delayed jobs with a timer, promoting them if the scheduled delay has been exceeded. This `setInterval` is defined within `Queue#promote(ms,limit)`, defaulting to a check of top 200 jobs every 5 seconds. If you have a cluster of kue processes, you must call `.promote` in just one (preferably master) process or promotion race can happen.
234234

235235
```js
236-
jobs.promote();
236+
queue.promote();
237237
```
238238

239239
## Processing Jobs
240240

241-
Processing jobs is simple with Kue. First create a `Queue` instance much like we do for creating jobs, providing us access to redis etc, then invoke `jobs.process()` with the associated type.
241+
Processing jobs is simple with Kue. First create a `Queue` instance much like we do for creating jobs, providing us access to redis etc, then invoke `queue.process()` with the associated type.
242242
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.
243243

244244
In the following example we pass the callback `done` to `email`, When an error occurs we invoke `done(err)` to tell Kue something happened, otherwise we invoke `done()` only when the job is complete. If this function responds with an error it will be displayed in the UI and the job will be marked as a failure. The error object passed to done, should be of standard type `Error`.
245245

246246
```js
247247
var kue = require('kue')
248-
, jobs = kue.createQueue();
248+
, queue = kue.createQueue();
249249

250-
jobs.process('email', function(job, done){
250+
queue.process('email', function(job, done){
251251
email(job.data.to, done);
252252
});
253253

@@ -265,10 +265,10 @@ Workers can also pass job result as the second parameter to done `done(null,resu
265265

266266
### Processing Concurrency
267267

268-
By default a call to `jobs.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:
268+
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:
269269

270270
```js
271-
jobs.process('email', 20, function(job, done){
271+
queue.process('email', 20, function(job, done){
272272
// ...
273273
});
274274
```
@@ -278,7 +278,7 @@ jobs.process('email', 20, function(job, done){
278278
Workers can temporary pause and resume their activity. It is, after calling `pause` they will receive no jobs in their process callback until `resume` is called. `pause` function gracefully shutdowns this worker, and uses the same internal functionality as `shutdown` method in [Graceful Shutdown](#graceful-shutdown).
279279

280280
```js
281-
jobs.process('email', function(job, done, ctx){
281+
queue.process('email', function(job, done, ctx){
282282
ctx.pause( function(err){
283283
console.log("Worker is paused... ");
284284
setTimeout( function(){ ctx.resume(); }, 10000 );
@@ -291,7 +291,7 @@ jobs.process('email', function(job, done, ctx){
291291
For a "real" example, let's say we need to compile a PDF from numerous slides with [node-canvas](http://github.com/learnboost/node-canvas). Our job may consist of the following data, note that in general you should _not_ store large data in the job it-self, it's better to store references like ids, pulling them in while processing.
292292

293293
```js
294-
jobs.create('slideshow pdf', {
294+
queue.create('slideshow pdf', {
295295
title: user.name + "'s slideshow"
296296
, slides: [...] // keys to data stored in redis, mongodb, or some other store
297297
});
@@ -300,7 +300,7 @@ jobs.create('slideshow pdf', {
300300
We can access this same arbitrary data within a separate process while processing, via the `job.data` property. In the example we render each slide one-by-one, updating the job's log and process.
301301

302302
```js
303-
jobs.process('slideshow pdf', 5, function(job, done){
303+
queue.process('slideshow pdf', 5, function(job, done){
304304
var slides = job.data.slides
305305
, len = slides.length;
306306

@@ -351,52 +351,53 @@ queue.on( 'error', function( err ) {
351351
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.
352352
This can be achieved in two ways:
353353

354-
* Wrapping your worker's process function in [Domains](https://nodejs.org/api/domain.html)
354+
1. Wrapping your worker's process function in [Domains](https://nodejs.org/api/domain.html)
355355

356-
```js
357-
jobs.process('my-error-prone-task', function(job, done){
358-
var domain = require('domain').create();
359-
domain.on('error', function(err){
360-
done(err);
361-
});
362-
domain.run(function(){ // your process function
363-
throw new Error( 'bad things happen' );
364-
done();
356+
```js
357+
queue.process('my-error-prone-task', function(job, done){
358+
var domain = require('domain').create();
359+
domain.on('error', function(err){
360+
done(err);
361+
});
362+
domain.run(function(){ // your process function
363+
throw new Error( 'bad things happen' );
364+
done();
365+
});
365366
});
366-
});
367-
```
367+
```
368368

369-
This is the softest and best solution, however is not built-in with Kue. Please refer to [this discussion](https://github.com/kriskowal/q/issues/120). You can comment on this feature in the related open Kue [issue](https://github.com/LearnBoost/kue/pull/403).
369+
This is the softest and best solution, however is not built-in with Kue. Please refer to [this discussion](https://github.com/kriskowal/q/issues/120). You can comment on this feature in the related open Kue [issue](https://github.com/LearnBoost/kue/pull/403).
370370

371-
You can also use promises to do something like
371+
You can also use promises to do something like
372372

373-
```js
374-
jobs.process('my-error-prone-task', function(job, done){
375-
Promise.method( function(){ // your process function
376-
throw new Error( 'bad things happen' );
377-
})().nodeify(done)
378-
});
379-
```
373+
```js
374+
queue.process('my-error-prone-task', function(job, done){
375+
Promise.method( function(){ // your process function
376+
throw new Error( 'bad things happen' );
377+
})().nodeify(done)
378+
});
379+
```
380380

381-
but this won't catch exceptions in your async call stack.
381+
but this won't catch exceptions in your async call stack as domains do.
382382

383383

384-
* Binding to `uncaughtException` and gracefully shutting down the Kue.
385384

386-
```js
387-
process.once( 'uncaughtException', function(err){
388-
queue.shutdown(function(err2){
389-
process.exit( 0 );
390-
}, 2000 );
391-
});
392-
```
385+
2. Binding to `uncaughtException` and gracefully shutting down the Kue.
386+
387+
```js
388+
process.once( 'uncaughtException', function(err){
389+
queue.shutdown(function(err2){
390+
process.exit( 0 );
391+
}, 2000 );
392+
});
393+
```
393394

394395
### Unstable Redis connections
395396

396397
Kue currently uses client side job state management and when redis crashes in the middle of that operations, some stuck jobs or index inconsistencies will happen. If you are facing poor redis connections or an unstable redis service you can start Kue's watchdog to fix stuck inactive jobs (if any) by calling:
397398

398399
```js
399-
jobs.watchStuckJobs()
400+
queue.watchStuckJobs()
400401
```
401402

402403
Kue will be refactored to fully atomic job state management from version 1.0 and this will happen by lua scripts and/or BRPOPLPUSH combination. You can read more [here](https://github.com/LearnBoost/kue/issues/130) and [here](https://github.com/LearnBoost/kue/issues/38).
@@ -408,7 +409,7 @@ Kue will be refactored to fully atomic job state management from version 1.0 and
408409
If you did none of above or your process lost active jobs in any way, you can recover from them when your process is restarted. A blind logic would be to re-queue all stuck jobs:
409410

410411
```js
411-
jobs.active( function( err, ids ) {
412+
queue.active( function( err, ids ) {
412413
ids.forEach( function( id ) {
413414
kue.Job.get( id, function( err, job ) {
414415
// if job is a stuck one
@@ -425,7 +426,7 @@ jobs.active( function( err, ids ) {
425426
Jobs data and search indexes eat up redis memory space, so you will need some job-keeping process in real world deployments. Your first chance is using automatic job removal on completion.
426427

427428
```javascript
428-
jobs.create( ... ).removeOnComplete( true ).save()
429+
queue.create( ... ).removeOnComplete( true ).save()
429430
```
430431

431432
But if you eventually/temporally need completed job data, you can setup an on-demand job removal script like below to remove top `n` completed jobs:
@@ -559,8 +560,8 @@ By default kue indexes the whole Job data object for searching, but this can be
559560
560561
```javascript
561562
var kue = require('kue');
562-
jobs = kue.createQueue();
563-
jobs.create('email', {
563+
queue = kue.createQueue();
564+
queue.create('email', {
564565
title: 'welcome email for tj'
565566
, to: 'tj@learnboost.com'
566567
, template: 'welcome-email'
@@ -692,7 +693,7 @@ When cluster `.isMaster` the file is being executed in context of the master pro
692693
```js
693694
var kue = require('kue')
694695
, cluster = require('cluster')
695-
, jobs = kue.createQueue();
696+
, queue = kue.createQueue();
696697

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

@@ -702,7 +703,7 @@ if (cluster.isMaster) {
702703
cluster.fork();
703704
}
704705
} else {
705-
jobs.process('email', 10, function(job, done){
706+
queue.process('email', 10, function(job, done){
706707
var pending = 5
707708
, total = pending;
708709

0 commit comments

Comments
 (0)
0