lykmapipo / kue-scheduler

A job scheduler utility for kue, backed by redis and built for node.js
246 stars 47 forks source link

Couldn't use existing ioredis instance #102

Closed edgracilla closed 7 years ago

edgracilla commented 7 years ago

We are trying to limit the connection count to redis so we need to reuse the existing connection, unfortunately re-using ioredis instance is not possible. Here is our code:

'use strict'

let kue = require('kue-scheduler')
let Redis = require('ioredis')

let redis = new Redis({
  host: 'localhost',
  port: 6379,
  password: '',
  dropBufferSupport: true,
  keepAlive: 10000,
  enableOfflineQueue: false
})

let Queue = kue.createQueue({
  prefix: 'w',
  skipConfig: false,
  restore: true,
  redis: {
    createClientFactory: function () {
      return redis
    }
  }
})

let job = Queue
  .createJob('7026da0d-1284-4d72-a83d-314e4069aada', {
    hello: 'world'
  })
  .attempts(3)
  .backoff({
    delay: 60000,
    type: 'fixed'
  })
  .priority('normal')
  .unique('7026da0d-1284-4d72-a83d-314e4069aada')

Queue.every('60 seconds', job)

Queue.process('7026da0d-1284-4d72-a83d-314e4069aada', function(job, done) {
  console.log(job)
  done()
})

it returns the following error: ReplyError: ERR only (P)SUBSCRIBE / (P)UNSUBSCRIBE / PING / QUIT allowed in this context

samhunta commented 7 years ago

Well you can't publish from an instance that's subscribing. You can have 2 connections with this little hack:

let redis = new Redis({
  host: 'localhost',
  port: 6379,
  password: '',
  dropBufferSupport: true,
  keepAlive: 10000,
  enableOfflineQueue: false
})

let redisSub = new Redis({
  host: 'localhost',
  port: 6379,
  password: '',
  dropBufferSupport: true,
  keepAlive: 10000,
  enableOfflineQueue: false
})

// Map subscribe to the subscription instance
redis.subscribe = redisSub.subscribe

let Queue = kue.createQueue({
  prefix: 'w',
  skipConfig: false,
  restore: true,
  redis: {
    createClientFactory: function () {
      return redis
    }
  }
})
edgracilla commented 7 years ago

We badly need 1 redis connection so the workaround might work but we cant deploy it. Ended up using bull instead.

edgracilla commented 7 years ago

btw, thank you @samhunta