From 4340cbf5c38ccd2d6a751e1c17dff70d8e403406 Mon Sep 17 00:00:00 2001 From: Nicholas Penree Date: Thu, 16 Jul 2015 08:34:58 -0400 Subject: [PATCH 1/2] Add type definitions for kue --- kue/kue-tests.ts | 96 ++++++++++++++++++++++++++++++ kue/kue.d.ts | 149 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 245 insertions(+) create mode 100644 kue/kue-tests.ts create mode 100644 kue/kue.d.ts diff --git a/kue/kue-tests.ts b/kue/kue-tests.ts new file mode 100644 index 0000000000..c58f6f874a --- /dev/null +++ b/kue/kue-tests.ts @@ -0,0 +1,96 @@ +/// +/// + +import kue = require('kue'); + +// create our job queue + +var jobs = kue.createQueue(); + +// start redis with $ redis-server + +// create some jobs at random, +// usually you would create these +// in your http processes upon +// user input etc. + +function create() { + var name = [ 'tobi', 'loki', 'jane', 'manny' ][ Math.random() * 4 | 0 ]; + var job = jobs.create( 'video conversion', { + title: 'converting ' + name + '\'s to avi', user: 1, frames: 200 + }); + + job.on('complete', function() { + console.log(" Job complete"); + }).on('failed', function() { + console.log(" Job failed"); + }).on('progress', function(progress: number) { + process.stdout.write('\r job #' + job.id + ' ' + progress + '% complete'); + }); + + job.save(); + + setTimeout( create, Math.random() * 2000 | 0 ); +} + +create(); + +// process video conversion jobs, 1 at a time. + +jobs.process('video conversion', 1, function(job: kue.Job, done: Function) { + var frames: number = job.data.frames; + + function next(i: number) { + // pretend we are doing some work + convertFrame(i, function(err: Error) { + if (err) return done(err); + // report progress, i/frames complete + job.progress(i, frames); + if (i >= frames) done(); + else next(i + Math.random() * 10); + } ); + } + + next(0); +} ); + +function convertFrame(i: number, fn: Function) { + setTimeout(fn, Math.random() * 50); +} + +// one minute + +var minute = 60000; + +var email = jobs.create('email', { + title: 'Account renewal required', to: 'tj@learnboost.com', template: 'renewal-email' +}).delay(minute) + .priority('high') + .save(); + + +email.on('promotion', function() { + console.log('renewal job promoted'); +} ); + +email.on('complete', function() { + console.log('renewal job completed' ); +} ); + +jobs.create('email', { + title: 'Account expired', to: 'tj@learnboost.com', template: 'expired-email' +} ).delay( minute * 10 ) + .priority('high') + .save(); + +jobs.promote(); + +jobs.process('email', 10, function(job, done) { + setTimeout(function() { + done(); + }, Math.random() * 5000); +}); + +// start the UI +kue.app.listen(3000); +console.log('UI started on port 3000'); \ No newline at end of file diff --git a/kue/kue.d.ts b/kue/kue.d.ts new file mode 100644 index 0000000000..1b3fff2277 --- /dev/null +++ b/kue/kue.d.ts @@ -0,0 +1,149 @@ +// Type definitions for kue 0.9.x +// Project: https://github.com/Automattic/kue +// Definitions by: Nicholas Penree +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +/// +/// +/// + +declare module "kue" { + import events = require('events'); + import express = require('express'); + import redis = require('redis'); + + export class Queue extends events.EventEmitter { + name: string; + id: string; + promoter: any; + workers: Worker[]; + shuttingDown: boolean; + client: redis.RedisClient; + testMode: TestMode; + + static singleton: Queue; + + constructor(options: Object); + create(type: string, data: Object): Job; + createJob(type: string, data: Object): Job; + promote(ms?: number): void; + setupTimer(): void; + checkJobPromotion(ms: number): void; + checkActiveJobTtl(ttlOptions: Object): void; + watchStuckJobs(ms: number): void; + setting(name: string, fn: Function): Queue; + process(type: string, n?: number, fn?: Function): void; + shutdown(timeout: number, type: string, fn: Function): Queue; + types(fn: Function): Queue; + state(string: string, fn: Function): Queue; + workTime(fn: Function): Queue; + cardByType(type: string, state: string, fn: Function): Queue; + card(state: string, fn: Function): Queue; + complete(fn: Function): Queue; + failed(fn: Function): Queue; + inactive(fn: Function): Queue; + active(fn: Function): Queue; + delayed(fn: Function): Queue; + completeCount(type: string, fn: Function): Queue; + failedCount(type: string, fn: Function): Queue; + inactiveCount(type: string, fn: Function): Queue; + activeCount(type: string, fn: Function): Queue; + delayedCount(type: string, fn: Function): Queue; + } + + interface Priorities { + low: number; + normal: number; + medium: number; + high: number; + critical: number; + } + + export class Job extends events.EventEmitter { + public id: number; + public type: string; + public data: any; + public client: redis.RedisClient; + private _max_attempts; + + static priorities: Priorities; + static disableSearch: boolean; + static jobEvents: boolean; + static get(id: number, fn: Function): void; + static remove(id: number, fn?: Function): void; + static removeBadJob(id: number): void; + static log(id: number, fn: Function): void; + static range(from: number, to: number, order: string, fn: Function): void; + static rangeByState(state: string, from: number, to: number, order: string, fn: Function): void; + static rangeByType(type: string, state: string, from: number, to: number, order: string, fn: Function): void; + + constructor(type: string, data?: any); + toJSON(): Object; + log(str: string): Job; + set(key: string, val: string, fn?: Function): Job; + get(key: string, fn?: Function): Job; + progress(complete: number, total: number, data?: any): Job; + delay(ms:number|Date): Job; + removeOnComplete(param: any): void; + backoff(param: any): void; + ttl(param: any): void; + private _getBackoffImpl(): void; + priority(level: string|number): Job; + attempt(fn: Function): Job; + reattempt(attempt: number, fn?: Function): void; + attempts(n: number): Job; + searchKeys(keys: string[]|string): Job; + remove(fn?: Function): Job; + state(state: string, fn?: Function): Job; + error(err: Error): Job; + complete(fn?: Function): Job; + failed(fn?: Function): Job; + inactive(fn?: Function): Job; + active(fn?: Function): Job; + delayed(fn?: Function): Job; + save(fn?: Function): Job; + update(fn?: Function): Job; + subscribe(fn?: Function): Job; + } + + class Worker extends events.EventEmitter { + queue: Queue; + type: string; + client: redis.RedisClient; + job: Job; + + constructor(queue: Queue, type: string); + start(fn: Function): Worker; + error(err: Error, job: Job): Worker; + failed(job: Job, theErr: Object, fn?: Function): Worker; + process(job: Job, fn: Function): Worker; + private zpop(key: string, fn: Function): void; + private getJob(fn: Function): void; + idle(): Worker; + shutdown(timeout: number, fn: Function): void; + emitJobEvent(event: Object, job: Job, arg1: any, arg2: any): void; + resume(): boolean; + } + + interface Redis { + configureFactory(options: Object, queue: Queue): void; + createClient(): redis.RedisClient; + createClientFactory(options: Object): redis.RedisClient; + client(): redis.RedisClient; + pubsubClient(): redis.RedisClient; + reset(): void; + } + + interface TestMode { + jobs: Job[]; + enter(): void; + exit(): void; + clear(): void; + } + + export var app: express.Application; + export var redis: Redis; + export var workers: Worker[]; + + export function createQueue(options?: Object): Queue; +} \ No newline at end of file From 7057e7d2aac7e19cd035497fc289205539f3b5b8 Mon Sep 17 00:00:00 2001 From: Nicholas Penree Date: Thu, 16 Jul 2015 09:48:03 -0400 Subject: [PATCH 2/2] Fix types in kue tests --- kue/kue-tests.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kue/kue-tests.ts b/kue/kue-tests.ts index c58f6f874a..0a990ee7b1 100644 --- a/kue/kue-tests.ts +++ b/kue/kue-tests.ts @@ -85,7 +85,7 @@ jobs.create('email', { jobs.promote(); -jobs.process('email', 10, function(job, done) { +jobs.process('email', 10, function(job: kue.Job, done: Function) { setTimeout(function() { done(); }, Math.random() * 5000);