| 13 | |
| 14 | class Queue extends Emitter { |
| 15 | constructor(name, settings = {}) { |
| 16 | super(); |
| 17 | |
| 18 | this.name = name; |
| 19 | this.paused = false; |
| 20 | this.jobs = new Map(); |
| 21 | this.activeJobs = new Set(); |
| 22 | this.checkTimer = null; |
| 23 | this.backoffStrategies = new Map(backoff); |
| 24 | |
| 25 | this._closed = null; |
| 26 | this._isClosed = false; |
| 27 | |
| 28 | this._emitError = (err) => void this.emit('error', err); |
| 29 | this._emitErrorAfterTick = (err) => |
| 30 | void process.nextTick(() => this.emit('error', err)); |
| 31 | |
| 32 | this.client = null; |
| 33 | this.bclient = null; |
| 34 | this.eclient = null; |
| 35 | |
| 36 | this.settings = { |
| 37 | redis: settings.redis || {}, |
| 38 | quitCommandClient: settings.quitCommandClient, |
| 39 | keyPrefix: (settings.prefix || defaults.prefix) + ':' + this.name + ':', |
| 40 | autoConnect: helpers.bool(settings.autoConnect, true), |
| 41 | }; |
| 42 | |
| 43 | this._isReady = false; |
| 44 | this._ready = false; |
| 45 | |
| 46 | for (const prop in defaults) { |
| 47 | const def = defaults[prop], |
| 48 | setting = settings[prop], |
| 49 | type = typeof def; |
| 50 | if (type === 'boolean') { |
| 51 | this.settings[prop] = typeof setting === 'boolean' ? setting : def; |
| 52 | } else if (type === 'number') { |
| 53 | this.settings[prop] = Number.isSafeInteger(setting) ? setting : def; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | /* istanbul ignore if */ |
| 58 | if (this.settings.redis.socket) { |
| 59 | this.settings.redis = Object.assign({}, this.settings.redis, { |
| 60 | path: this.settings.redis.socket, |
| 61 | }); |
| 62 | } |
| 63 | |
| 64 | // By default, if we're given a redis client and no additional instructions, |
| 65 | // don't quit the connection on Queue#close. |
| 66 | if (typeof this.settings.quitCommandClient !== 'boolean') { |
| 67 | this.settings.quitCommandClient = !redis.isClient(this.settings.redis); |
| 68 | } |
| 69 | |
| 70 | // To avoid changing the hidden class of the Queue. |
| 71 | this._delayedTimer = this.settings.activateDelayedJobs |
| 72 | ? new EagerTimer(this.settings.nearTermWindow) |