Skip to content

Commit

Permalink
Merge pull request #91 from Embraser01/fix-#90
Browse files Browse the repository at this point in the history
fix: check of existing redis instance
  • Loading branch information
manast authored Dec 16, 2019
2 parents 763ca72 + b3b89b6 commit 6ebae9e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
5 changes: 3 additions & 2 deletions src/classes/redis-connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import IORedis, { Redis } from 'ioredis';
import * as semver from 'semver';
import { load } from '../commands';
import { ConnectionOptions, RedisOptions } from '../interfaces';
import { isRedisInstance } from '../utils';

export class RedisConnection extends EventEmitter {
static minimumVersion = '5.0.0';
Expand All @@ -13,7 +14,7 @@ export class RedisConnection extends EventEmitter {
constructor(private opts?: ConnectionOptions) {
super();

if (!(opts instanceof IORedis)) {
if (!isRedisInstance(opts)) {
this.opts = {
port: 6379,
host: '127.0.0.1',
Expand All @@ -23,7 +24,7 @@ export class RedisConnection extends EventEmitter {
...opts,
};
} else {
this._client = opts;
this._client = <Redis>opts;
}

this.initializing = this.init();
Expand Down
7 changes: 4 additions & 3 deletions src/classes/worker.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fs from 'fs';
import IORedis from 'ioredis';
import { Redis } from 'ioredis';
import path from 'path';
import { Processor, WorkerOptions } from '../interfaces';
import { QueueBase, Repeat } from './';
Expand All @@ -10,6 +10,7 @@ import sandbox from './sandbox';
import { Scripts } from './scripts';
import uuid from 'uuid';
import { TimerManager } from './timer-manager';
import { isRedisInstance } from '../utils';

// note: sandboxed processors would also like to define concurrency per process
// for better resource utilization.
Expand Down Expand Up @@ -51,8 +52,8 @@ export class Worker<T = any> extends QueueBase {
this.opts.lockRenewTime || this.opts.lockDuration / 2;

this.blockingConnection = new RedisConnection(
opts instanceof IORedis
? (<IORedis.Redis>opts.connection).duplicate()
isRedisInstance(opts.connection)
? (<Redis>opts.connection).duplicate()
: opts.connection,
);
this.blockingConnection.on('error', this.emit.bind(this));
Expand Down
8 changes: 8 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,11 @@ export function delay(ms: number): Promise<void> {
setTimeout(() => resolve(), ms);
});
}

export function isRedisInstance(obj: any): boolean {
if (!obj) {
return false;
}
const redisApi = ['connect', 'disconnect', 'duplicate'];
return redisApi.every(name => typeof obj[name] === 'function');
}

0 comments on commit 6ebae9e

Please sign in to comment.