diff --git a/types/bull/bull-tests.tsx b/types/bull/bull-tests.tsx index 1552febf85..e0854cfafa 100644 --- a/types/bull/bull-tests.tsx +++ b/types/bull/bull-tests.tsx @@ -2,12 +2,12 @@ * Created by Bruno Grieder */ +import * as Redis from "ioredis"; import * as Queue from "bull"; const videoQueue = new Queue('video transcoding', 'redis://127.0.0.1:6379'); const audioQueue = new Queue('audio transcoding', {redis: {port: 6379, host: '127.0.0.1'}}); // Specify Redis connection using object const imageQueue = new Queue('image transcoding'); -const pdfQueue = new Queue('pdf transcoding'); videoQueue.process((job, done) => { // job.data contains the custom data passed when the job was created @@ -67,6 +67,28 @@ videoQueue.add({video: 'http://example.com/video1.mov'}); audioQueue.add({audio: 'http://example.com/audio1.mp3'}); imageQueue.add({image: 'http://example.com/image1.tiff'}); +////////////////////////////////////////////////////////////////////////////////// +// +// Re-using Redis Connections +// +////////////////////////////////////////////////////////////////////////////////// + +const client = new Redis(); +const subscriber = new Redis(); + +const pdfQueue = new Queue('pdf transcoding', { + createClient: (type: string, options: Redis.RedisOptions) => { + switch (type) { + case 'client': + return client; + case 'subscriber': + return subscriber; + default: + return new Redis(options); + } + } +}); + ////////////////////////////////////////////////////////////////////////////////// // // Using Promises diff --git a/types/bull/index.d.ts b/types/bull/index.d.ts index 926952f6c2..fcab3c282e 100644 --- a/types/bull/index.d.ts +++ b/types/bull/index.d.ts @@ -12,7 +12,7 @@ import * as Redis from "ioredis"; * It creates a new Queue that is persisted in Redis. * Everytime the same queue is instantiated it tries to process all the old jobs that may exist from a previous unfinished session. */ -declare var Bull: { +declare const Bull: { // tslint:disable:unified-signatures (queueName: string, opts?: Bull.QueueOptions): Bull.Queue; (queueName: string, url?: string): Bull.Queue; @@ -28,6 +28,12 @@ declare namespace Bull { */ redis?: Redis.RedisOptions; + /** + * When specified, the `Queue` will use this function to create new `ioredis` client connections. + * This is useful if you want to re-use connections. + */ + createClient?(type: 'client' | 'subscriber', redisOpts?: Redis.RedisOptions): Redis.Redis; + /** * Prefix to use for all redis keys */