Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kue (reds?) creates always a Redis connection to localhost when requiring #54

Closed
esamattis opened this issue Aug 9, 2011 · 39 comments
Closed

Comments

@esamattis
Copy link

Don't know if this is kue or reds issue, but it certainly affects kue. Tested on kue master.

Consider following:

var redis = require("redis");                                                                                                                
var client = redis.createClient(1234, "example.com");                           

setTimeout(function() {                                                         
    require("kue");                                                   
}, 1000);                                                                       

client.on("connect", function() {                                               
  console.log("Connected!");                                                    
});                                                                             

It dies immediately after kue is required in the timeout:

Connected!

node.js:134
        throw e; // process.nextTick error, or 'error' event on first tick
        ^
Error: Redis connection to 127.0.0.1:6379 failed - ECONNREFUSED, Connection refused
    at Socket.<anonymous> (/home/epeli/tmp/kue/node_modules/reds/node_modules/redis/index.js:123:28)
    at Socket.emit (events.js:64:17)
    at Array.<anonymous> (net.js:830:27)
    at EventEmitter._tickCallback (node.js:126:26)

shell returned 1

Creates a second connection to localhost?

I think the reason is here:

A search is created on require
https://github.com/LearnBoost/kue/blob/b2cd5d3393d12e4bdd1c2e654a3ef5eb45e5bf11/lib/queue/job.js#L27

which means new Search on Reds
https://github.com/visionmedia/reds/blob/156c046803df6b96fa40e90be26b933f7d324d01/lib/reds.js#L68

which then again means a new Redis client which connects to localhost
https://github.com/visionmedia/reds/blob/156c046803df6b96fa40e90be26b933f7d324d01/lib/reds.js#L236

This happens also if you override the createClient function in kue.

@davidwood
Copy link
Contributor

I'm confused - are you using Kue or reds? I haven't used reds, but in looking at the package.json, it doesn't look like it uses Kue.

If we just focus on Kue and look at your example, you're requiring the node.js Redis client, but not actually overriding the Kue function that creates the client. Try the following to create a Kue instance (named jobs) with a connection to Redis on example.com:1234:

var redis = require('redis'),
    kue = require('kue');

kue.redis.createClient = function() {
    var client = redis.createClient(1234, "example.com");
    return client;
}

var jobs = kue.createQueue();

@esamattis
Copy link
Author

I'm using Kue and Kue uses Reds under the hood.

As I stated, it doesn't help if you override the creator function in kue. Your code fails with the same error.

@davidwood
Copy link
Contributor

What version of Kue are you using? I just ran the above code using Kue 0.2.0 without issue. Your code (and subsequent stack trace) wasn't overriding the Kue creator function - can you provide code that overrides the creator function and the stack trace?

@esamattis
Copy link
Author

I'm using Kue master from Github.

Is there a Redis instance running on your localhost when you tested the code?

@esamattis
Copy link
Author

I'll get this if I use the code you posted (just changed the server and port):

node.js:134
        throw e; // process.nextTick error, or 'error' event on first tick
        ^
Error: Redis connection to 127.0.0.1:6379 failed - ECONNREFUSED, Connection refused
    at Socket.<anonymous> (/home/epeli/tmp/kue/node_modules/reds/node_modules/redis/index.js:123:28)
    at Socket.emit (events.js:64:17)
    at Array.<anonymous> (net.js:830:27)
    at EventEmitter._tickCallback (node.js:126:26)

@esamattis
Copy link
Author

Also if add an another Redis instance to my localhost: Both Redis instances, remote and the local, will get connected when I run your code and I won't get the error. Obviously.

@davidwood
Copy link
Contributor

Ok, I see what's happening now. I was looking at the 0.2.0 release, not master. I have a workaround - it's not pretty, but it works :)


var redis = require('redis'),
    reds = require('reds');

reds.createClient = function() {
  var client = redis.createClient(1234, "example.com");
  return client;
}

var kue = require('kue');
kue.redis.createClient = reds.createClient;
var jobs = kue.createQueue();    

@davidwood
Copy link
Contributor

In order for this to work, you'll need to make sure reds is installed for your application (with npm install reds) and not installed under Kue (rm -R node_modules/kue/node_modules/reds)

@tj
Copy link
Contributor

tj commented Aug 9, 2011

haha the joys of trying to share config like this blah. Yeah, we'll definitely need a reasonable solution for this in the next release. We dont have much choice other than to do kue.reds.createClient = kue.redis.createClient = fn etc, ugly but other than duplicating a ton of other logic in redis etc there's no easy way around it

@davidwood
Copy link
Contributor

The thing that makes this tricky is that lib/queue/job.js and lib/http/routes/json.js create the reds search when required, so currently you need to override the reds Redis client creation function before Kue is required. This seems less than ideal, so I've created a branch (named lazy-create-reds) that makes this a bit easier by lazily creating the reds search with an overridden reds.createClient upon first access.

This change updates lib/queue/job.js and lib/http/routes/json.js.

@tj
Copy link
Contributor

tj commented Aug 9, 2011

it's ideal as far as simplicity goes but yeah im fine with changing it for this

@tj
Copy link
Contributor

tj commented Aug 10, 2011

@davidwood merged

I wish we could do it with less of a hack, but I can't think of anything off hand

@davidwood
Copy link
Contributor

I'm with you - it works, but doesn't feel quite right

@larsemann
Copy link

hi,

the problem still exists, with version 0.3.2 :/

is there any fix now? i'm trying to use kue on different machines with one redis server :)

Update: switched to 0.3.1. - everything working fine...
thank you anyway! keep up the good work!

@ntresch
Copy link

ntresch commented May 3, 2012

I don't understand the assertion that Kue uses Reds under the hood when Reds was written to emulate Kue?

@tj
Copy link
Contributor

tj commented May 4, 2012

@ntresch huh? reds is full-text search, nothing more

@ntresch
Copy link

ntresch commented May 4, 2012

@visionmedia "I'm using Kue and Kue uses Reds under the hood.", was said by @epeli. I don't believe that this is true, and I don;t understand the assertion, hence the question.

@tj
Copy link
Contributor

tj commented May 4, 2012

it is true, Kue uses Reds for the search

@ntresch
Copy link

ntresch commented May 4, 2012

That makes more sense, with the qualification "for the search", and I see it now in the source. Thanks!

@loudin
Copy link

loudin commented Oct 9, 2012

This is still an issue for 0.4.0:

Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED

@judyhe
Copy link

judyhe commented Jan 29, 2013

Yep same with 0.5.0

@georgecoltart
Copy link

Pretty sure this is still happening, I can't stop the connection to localhost

@behrad behrad closed this as completed Jan 27, 2014
@schonfeld
Copy link

Still an issue with 0.7.5. Any chance of seeing a fix for this soon?

@behrad
Copy link
Collaborator

behrad commented Mar 17, 2014

@schonfeld would you provide the code for reproducing this?

@behrad behrad reopened this Mar 17, 2014
@behrad
Copy link
Collaborator

behrad commented Mar 27, 2014

@schonfeld !!!!????

@iamkale
Copy link

iamkale commented Mar 31, 2014

I wandered over from Google, but this is happening to me in a standard node/redis/express app (bare bones..). Running on locahost connecting to a remote redis server (Elasticache on AWS) works fine, but from my EC2 server that doesn't have redis installed I can't seem to connect, since it tries to connect to localhost first even though I have config specific for the remote server running.

Sorry for off-topic, but this was one of few places I found this issue.

@behrad
Copy link
Collaborator

behrad commented Apr 30, 2014

would u please provide the stack trace of connection error?

@judyhe
Copy link

judyhe commented May 2, 2014

We forked awhile ago (0.5.0) and updated locally: https://github.com/floored/kue/commit/ec021e07b6ceea9f77bb9556ac2fb8035a880620. Never did a pull request because we assumed it would get fixed "for real".

@behrad behrad closed this as completed Jul 11, 2014
@behrad
Copy link
Collaborator

behrad commented Jul 11, 2014

should be fixed in 0.8

@rvbsanjose
Copy link

I'm trying to connect with Redis that is deployed on Heroku and I used the example @davidwood provided but I am still receiving the Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED. Any other ideas on how to fix this?

@behrad
Copy link
Collaborator

behrad commented Oct 2, 2014

@rvbsanjose make sure if you are calling createQueue(...) before accessing kue.app express application, and also try with disableSearch: true.
Finally provide the code so that I can fix it, however I don't believe it is a bug.

@rvbsanjose
Copy link

@behrad I feel like a dummy. I was following along with the tutorials and never bothered to read the full doc on connecting. Once I read further down the doc page and followed the instructions, it worked just fine. Sorry for the inconvenience.

@petermekhaeil
Copy link

What was the solution? I am getting this problem after a successful connect to a non-localhost url.

@eaglevision20
Copy link

eaglevision20 commented Oct 10, 2016

I've been beating my head against this wall for a full day now. I'm running kue version 0.11 and node 4.5 but still cannot connect to a remote host. here is what my connection looks like:

const config = require('./config');
var Kue = require('kue');
var queue = Kue.createQueue({
redis: {
port: 6379, //process.env.REDIS_PORT,
host: 'DOMAIN_HERE.com' //process.env.REDIS_HOST,
//auth: process.env.REDIS_PASS
},
jobEvents: false,
disableSearch: true,
});

No matter what I try I get the same error back:

events.js:141
throw er; // Unhandled 'error' event
^

Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED 127.0.0.1:6379
at Object.exports._errnoException (util.js:907:11)
at exports._exceptionWithHostPort (util.js:930:20)
at TCPConnectWrap.afterConnect as oncomplete

I have tried every example I can find and still nothing.. :(

Any help is greatly appreciated.

@59023g
Copy link

59023g commented Oct 10, 2016

@eaglevision20 closest thing I could find: #875

If I have time I'll fork and look..

Maybe check the Pull Requests

@ganeshcse2991
Copy link

Still facing the same issue. Tried all the examples and pull requests too. Please help. Its still creating queue with local redis.

Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED 127.0.0.1:6379
at Object.exports._errnoException (util.js:907:11)
at exports._exceptionWithHostPort (util.js:930:20)
at TCPConnectWrap.afterConnect as oncomplete

@fabulator
Copy link

@ganeshcse2991 as was said in comment earlier #54 (comment) Problem is requiring app from kue before creating a queue. Something like this: import kue, { app } from 'kue'; At least it was my issue and it is working fine for me now.

@shierro
Copy link

shierro commented May 3, 2018

@behrad thanks for the input about creating the queue before calling kue.app you are awesome!

queueService.initQueue(REDIS_HOST, REDIS_PORT);
app.use('/kue-api/', basicAuth, require('kue').app);

@sinisavukovic
Copy link

@behrad @shierro. Thanks!

For those who are still looking for a quick answer:

import kue from "kue";
import express from "express";

kue.createQueue({
  prefix: 'q',
  redis: {
    port: <REDIS_PORT>,
    host: "<REDIS_HOST>"
  }
});

const app = express();
app.use("/kue-api/", kue.app);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests