Merge pull request #17182 from amiram/kue-process

Kue process - add option to call it with a type and a callback
This commit is contained in:
Mine Starks
2017-06-19 11:31:38 -07:00
committed by GitHub
2 changed files with 27 additions and 8 deletions
+11 -2
View File
@@ -1,6 +1,7 @@
// Type definitions for kue 0.11.x
// Project: https://github.com/Automattic/kue
// Definitions by: Nicholas Penree <http://github.com/drudge>
// Amiram Korach <http://github.com/amiram>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference types="node" />
@@ -29,7 +30,7 @@ export declare class Queue extends events.EventEmitter {
checkActiveJobTtl(ttlOptions: Object): void;
watchStuckJobs(ms: number): void;
setting(name: string, fn: Function): Queue;
process(type: string, n?: number, fn?: Function): void;
process(type: string, n?: number | DoneCallback, fn?: DoneCallback): void;
shutdown(timeout: number, type: string, fn: Function): Queue;
types(fn: Function): Queue;
state(string: string, fn: Function): Queue;
@@ -56,17 +57,24 @@ interface Priorities {
critical: number;
}
export type DoneCallback = (err?: any, result?: any) => void;
export type JobCallback = (err?: any, job?: Job) => void;
export declare class Job extends events.EventEmitter {
public id: number;
public type: string;
public data: any;
public result: any;
// Should always be a number however currently it is a number when creating and a string when loading
// https://github.com/Automattic/kue/issues/1081
public created_at: string | number;
public client: redisClientFactory.RedisClient;
private _max_attempts;
static priorities: Priorities;
static disableSearch: boolean;
static jobEvents: boolean;
static get(id: number, fn: Function): void;
static get(id: number, type: string | JobCallback, fn?: JobCallback): void;
static remove(id: number, fn?: Function): void;
static removeBadJob(id: number): void;
static log(id: number, fn: Function): void;
@@ -79,6 +87,7 @@ export declare class Job extends events.EventEmitter {
log(str: string): Job;
set(key: string, val: string, fn?: Function): Job;
get(key: string, fn?: Function): Job;
get(key: string, jobType: string, fn?: Function): Job;
progress(complete: number, total: number, data?: any): Job;
delay(ms: number | Date): Job;
removeOnComplete(param: any): Job;
+16 -6
View File
@@ -39,6 +39,13 @@ function create() {
job.save();
kue.Job.get(job.id, function (err: any, _job: kue.Job) {
console.log('get job', _job);
});
kue.Job.get(job.id, 'video conversion', function (err: any, _job: kue.Job) {
console.log('get job', _job);
});
setTimeout( create, Math.random() * 2000 | 0 );
}
@@ -46,25 +53,28 @@ create();
// process video conversion jobs, 1 at a time.
jobs.process('video conversion', 1, function(job: kue.Job, done: Function) {
var processCb = function(job: kue.Job, done: kue.DoneCallback) {
var frames: number = job.data.frames;
function next(i: number) {
// pretend we are doing some work
convertFrame(i, function(err: Error) {
convertFrame(i, function(err: Error, result: any) {
if (err) return done(err);
// report progress, i/frames complete
job.progress(i, frames);
if (i >= frames) done();
if (i >= frames) done(null, result);
else next(i + Math.random() * 10);
} );
}
next(0);
} );
}
jobs.process('video conversion', 1, processCb);
jobs.process('video conversion', processCb);
function convertFrame(i: number, fn: Function) {
setTimeout(fn, Math.random() * 50);
setTimeout(() => fn(null, Math.random()), Math.random() * 50);
}
// one minute
@@ -102,4 +112,4 @@ jobs.process('email', 10, function(job: kue.Job, done: Function) {
// start the UI
kue.app.listen(3000);
console.log('UI started on port 3000');
console.log('UI started on port 3000');