Connecting to redis using kue always creates a connection to localhost - node.js

I can't connect to Redis using kue, I've followed this article, practically I'm creating the connection by using the kue redis client, and the connection code is this:
var kue = require('kue'),
redis = require('kue/node_modules/redis');
app.redisClient = redis.createClient('6379', 'remoteip',{});
app.redisClient.on('error', function (err) {
console.log('Redis error encountered', err);
});
app.redisClient.on('end', function() {
console.log('Redis connection closed');
});
kue.redis.createClient = function() {
console.log('client ------------------',app.redisClient);
return app.redisClient;
};
and it seems like Kue is trying to connect to a local Redis (which I don't have installed), because I'm getting this exception:
Error: Redis connection to 127.0.0.1:6379 failed - connect
ECONNREFUSED
I've read this post and it seems like the issue has been resolved in version 0.8 and I'm using 0.8.11 :/, finally I also wanted to override the client using a different client instance by using redis nodejs without any luck, because I'm getting the same error.
any help will be more than appreciated.
thanks!

I used a different way to setup the connection and it works so far this is what I've done:
app.redisClient = redis.createClient(config.redisPort, config.redisUrl,{});
app.redisClient.on('connect', function () {
console.info('successful connection to redis server');
});
app.redisClient.on('error', function (err) {
console.log('Redis error encountered', err);
});
app.redisClient.on('end', function() {
console.log('Redis connection closed');
});
kue.app.listen(config.kuePort);
app.jobs = kue.createQueue({
redis: {
createClientFactory: function(){
return app.redisClient;
}
}
});

Related

Failed to connect ElastiCache from NodeJS server on Elastic Beanstalk

We have a nodeJS server with express on AWS Elastic Beanstalk and we are trying to connect it with the Elasticache(Redis clustered) from the NodeJS but getting this error Redis Client Connection Error ClusterAllFailedError: Failed to refresh slots cache.. The error seems very common as a lot of people are facing the same bug. In order to connect to ElastiCache, we are using an npm module named ioredis.
A lot of people recommend using the same VPC and security group for both ElastiCache and Elastic Beanstalk. We are already using the same VPC and on Elastic Beanstalk we are using two security groups one of them matches the security group of ElastiCache. For the default VPC, we have enabled All Traffic for the inbound and outbound rules, but still, we are facing the same bug.
In order to connect to ElastiCache from NodeJS server I am using the following code:
const Redis = require("ioredis");
exports.connect = () => {
const client = new Redis.Cluster(
["xxxxx.xxxxx.clustercfg.use1.cache.amazonaws.com:6379"],
{
slotsRefreshTimeout: 10000,
dnsLookup: (address, callback) => callback(null, address),
redisOptions: {
showFriendlyErrorStack: true,
tls: {
checkServerIdentity: (/*host, cert*/) => {
// skip certificate hostname validation
return undefined;
},
},
},
}
);
client.on("ready", () => {
console.log("Redis Client Ready");
});
client.on("connect", () => {
console.log("Redis Client Connected");
});
client.on("error", (error) => {
console.log("Redis Client Connection Error", error);
});
client.on("reconnecting", () => {
console.log("Redis Client Reconnecting");
});
client.on("end", () => {
console.log("Redis Client Connection ended");
});
return client;
};
ElastiCache Configuration
Default VPC Security Group with Inbound and Outbound rules
Elastic Beanstalk security group(Same as default)
Error information from Elastic Beanstalk
Versions:
Node.js running on 64bit Amazon Linux with platform version 4.15.1
NodeJS version: 12.18.3
ioredis version: 4.17.3
npm version: 6.14.6
express version: 4.17.1
UPDATE: I am able to access the ElastiCache from ElasticBeanstalk if I do ssh and use redis-cli, but unable to access it using ioredis on NodeJS which is running on ElasticBeanstalk.
I have a similar setup and eventually got it working, a few key points:
Elasticbeanstalk and Elasticache have to be in the same VPC
Elasticache's security group should have an inbound rule to allow traffic from Elasticbeanstalk
Here's a code to connect:
import { RedisPubSub } from 'graphql-redis-subscriptions';
import Redis from 'ioredis';
import config from '../../config/env';
const options = {
// AWS host will look like this: somecache-dev-ro.k6sjdj.ng.0001.use1.cache.amazonaws.com
host: config.redis.host || 'localhost',
port: config.redis.port || 6379,
retryStrategy: (times: number): number => {
// reconnect after
return Math.min(times * 50, 2000);
},
};
export const pubsub = new RedisPubSub({
publisher: new Redis(options),
subscriber: new Redis(options),
});
I was debugging a similar issue. To access redis, I had to add tls: {} to the ioredis options:
{
host: process.env.REDIS_HOST,
port: process.env.REDIS_PORT,
password: process.env.REDIS_PASSWORD,
tls: {}
}
you can simply create connection
const Redis = require("ioredis");
const client = new Redis(
6379,
"Configiration Endpoint (xxx.xxxx.xxxcache.amazonaws.com)"
);
client.on("ready", () => {
console.log("Redis Client Ready");
client.send(
});
client.on("connect", () => {
console.log("Redis Client Connected");
});
client.on("error", (error) => {
console.log("Redis Client Connection Error", error);
});

Unable to connect remote db2 with node js

I am trying to connect remote ibm db2 with node js in mac machine but I'm getting following error
[Error: [IBM][CLI Driver] SQL1598N An attempt to connect to the database server failed because of a licensing problem. SQLSTATE=42968
] {
error: '[node-ibm_db] SQL_ERROR',
sqlcode: -1598,
message: '[IBM][CLI Driver] SQL1598N An attempt to connect to the database server failed because of a licensing problem. SQLSTATE=42968\n',
state: '42968'
}
And this is how I'm trying to connect
function db2Connection() {
var ibmdb = require('ibm_db');
var connStr = "DATABASE=dbname;HOSTNAME=mydb.ibm.com;UID=userid;PWD=password;PORT=447;PROTOCOL=TCPIP";
ibmdb.open(connStr, function (err,conn) {
if (err) return console.log(err);
conn.query('SELECT * FROM T_Name FETCH FIRST 6 ROWS ONLY', function (err, data) {
if (err) console.log(err);
else console.log(data);
conn.close(function () {
console.log('done');
});
});
});
}
db2Connection();
I went through some docs regarding that error but did not get any solution. Can anyone help on how to achieve this.

Setting Heartbeat when connecting to RabbitMQ via Amqplib

When connecting to RabbitMQ via amqplib, the value of the Heartbeat parameter cannot be set.
I do this:
// NPM Modules
const amqp = require('amqplib/callback_api');
const withAutoRecovery = require('amqplib-auto-recovery');
// Application configuration
const config = settings.getConfig();
withAutoRecovery(amqp, {
onError: (err) => { console.log(err.message); },
isErrorUnrecoverable: (err) => { console.log(err) }
}).connect(config.rabbitmq, (err, connection) => {
// If there are connection errors
if (err) {
console.error(this.conn_str_amqp);
console.error('[AMQP] Connection error:', err.message);
} else if (connection) {
// Connection Error Event
connection.on('error', (err) => {
if (err.message !== 'Connection closing') {
console.error('[AMQP] Closing connection', err.message);
}
});
// Error event when you close the connection
connection.on('close', () => {
console.error('[AMQP] Closing connection');
});
// Hint to user
console.log('[AMQP] Connected');
}
});
Here config.rabbitmq is:
{
protocol: 'amqp',
hostname: 'localhost',
port: 5672,
username: 'guest',
password: 'guest',
locale: 'en_US',
frameMax: 0x1000,
heartbeat: 1800,
vhost: '/',
}
I expect to get the Heartbeat = 1800s parameter value, in the UI Managment window the default value Heartbeat = 60s is displayed.
Screenshot:
I tried to pass another object instead config.rabbitmq line (https://www.rabbitmq.com/uri-spec.html and https://www.rabbitmq.com/uri-query-parameters.html) in the format:
function getRabbitMQConnectionString() {
const rabbitmq = config.rabbitmq;
return `${rabbitmq.protocol}://${rabbitmq.username}:${rabbitmq.password}#${rabbitmq.hostname}:${rabbitmq.port}${encodeURIComponent(rabbitmq.vhost)}?heartbeat=${rabbitmq.heartbeat}`;
}
Thus, too, it turned out similar to the above effect.
Tell me, please, how to correctly set the Heartbeat parameter through the Node.js client application on amqplib?
For some reason I set up the heartbeat to 30 s and it worked.
From RabbitMQ documentation, they suggest not to set the heartbeats high, because it can disable them.
However, I don't know what is shown as 60s heartbeats.
https://www.rabbitmq.com/heartbeats.html#disabling
Reason
The reason of getting heartbeat 60s in Management UI is because the RabbitMQ server have default heartbeat 60s.
Explanation
And during connection creation the minimum of both client(amqplib) and server(RabbitMQ) heartbeat value is taken.
Solution
So, to increate the heartbeat value from amqplib, we need to have more or equal value set in RabbitMQ configurations
Methods to Set heartbeat value for RabbitMQ Server
How to change RabbitMQ Heartbeat without restart
https://www.rabbitmq.com/configure.html#:~:text=Max%20value%3A%20536870912-,heartbeat,-Value%20representing%20the
Reference for more detail
https://www.rabbitmq.com/heartbeats.html
https://www.rabbitmq.com/configure.html

NodeJS Mongoose & Mongo Db Connection Issue

I am new to mongodb and I have setup on mac os and it's successfully done. Now when I run my node js application with mongoose to connect with mongodb, It's always show a connecting state.
I am using below code to connect the database.
module.exports = function() {
/**
* Requiring this module will populate the mongoose connection pool.
* Any models defined on mongoose will be ready to use (mongoose is
* a singleton).
*/
var mongoose = require('mongoose');
var logger = include('logger.js');
// attempt to connect to the database server
logger.info("DB CONNECTION ", {DB: global.settings.DB, options: global.settings.DbOptions});
//console.log("DB CONNECTION ", {DB: global.settings.DB, options: global.settings.DbOptions});
mongoose.connect('mongodb://localhost/circleweb');
mongoose.connection.on("open", function(ref) {
console.log("Connected to mongo server.");
});
mongoose.connection.on('connected', function () {
console.log('connected');
console.log('Mongoose default connection open to ' + global.settings.DB);
});
mongoose.connection.on('error', function (error) {
logger.error(error);
console.log('Mongoose default connection error: ' + error.err);
// if the database cannot be connected to. We are going to throw
// an error. This will be caught by the error handler
throw error.err;
});
mongoose.connection.on('disconnected', function () {
console.log('Mongoose default connection disconnected');
});
process.on('SIGINT', function () {
mongoose.connection.close(function () {
console.log('Mongoose default connection disconnected through app termination');
process.exit(0);
});
});
console.log(mongoose.connection.readyState);
return mongoose.connection;
}();
And When i run my application I am not getting any callback from mongoose not even any error.
Also when I run mongod on terminal it shows correct output and also it's accepting the connection from nodejs application but not connecting with it.
Any help would be appreciate
Because console.log(mongoose.connection.readyState) is running synchronously - it will get executed first - before the mongoose.connection.on async calls, which means that the state is 2 or connecting.
Surround it with a timeout and you will get the expected 1 value:
setTimeout( () => {
console.log(mongoose.connection.readyState); // prints 1
}, 2000);
Or print the state within the async call:
mongoose.connection.on('connected', function () {
console.log(`STATE: `, mongoose.connection.readyState);
console.log('connected');
console.log('Mongoose default connection open to ' );
});

When mongodb server is down how to catch the error while running mongoose query

I am using mongoose for connecting node.js with mongoDB, now i wrote below query
var trans = new transmodel({method: method, trans_id: r});
trans.save(function(err) {
if (err) {
console.error("Razor_pay_webhook Error 4 err: " + err);
res.write('statusCode: 200');
res.end();
} else {
res.write('statusCode: 400');
res.end();
}
});
I thought when my mongodb cluster will be down then i will get 'err' while executing above mongoose query, but when i ran above query while my mongo cluster was down nothing happened(No err was called). Can anyone please tell me how can i catch the error if my mongodb server is down inside my query. Also for reconnecting again with my cluster i have set below parameters but my node server is not trying to reconnect again with my mongodb server i don't know what's going wrong.
var mongoose = require('mongoose');
var config = require('./config/database.js');
var DB_URL = config.db.url;
mongoose.connection.on("connected", function(ref) {
console.log("Connected to " + " DB!");
});
mongoose.connection.on("error", function(err) {
console.error('Failed to connect to DB ' + ' on startup ', err);
if (err) {
return next(err);
}
});
mongoose.connection.on('disconnected', function(err) {
console.log('Mongoose default connection to DB :' + ' disconnected');
if (err) {
return next(err);
}
});
var gracefulExit = function() {
mongoose.connection.close(function () {
console.log('Mongoose default connection with DB :' + ' is disconnected through app termination');
process.exit(0);
});
}
process.on('SIGINT', gracefulExit).on('SIGTERM', gracefulExit);
exports.con_close = function () {
console.log('Mongoose connection disconnected');
mongoose.connection.close();
}
var options = {
server: {
socketOptions: {
keepAlive: 1000,
connectTimeoutMS: 30000
}
},
replset: {
rs_name: 'replicaset',
auto_reconnect:true,
socketOptions: {
keepAlive: 1000, // doubt about it
connectTimeoutMS: 30000
}
},
user: 'root',
pass: 'G3saGT2Y',
auth: {
authdb: 'admin'
}
}
mongoose.connect(DB_URL, options, function(err) {
console.log('ho rha hai');
if (err) {
console.log('error connection to mongo server!');
console.log(err);
}
});
You are using mongoose, it emits events (the EventEmitter pattern) when the database is down and when the database is reconnecting and up again.
from mongoose code found here we can see that the library db connection - connection.js
has the following events that are emitted:
* #param {Mongoose} base a mongoose instance
* #inherits NodeJS EventEmitter
http://nodejs.org/api/events.html#events_class_events_eventemitter
* #event connecting: Emitted when connection.{open,openSet}() is executed on this connection.
#event connected: Emitted when this connection successfully connects to the db. May be emitted multiple times in reconnected scenarios.
#event open: Emitted after we connected and onOpen is executed on all of this connections models.
#event disconnecting: Emitted when connection.close() was executed.
#event disconnected: Emitted after getting disconnected from the db.
#event close: Emitted after we disconnected and onClose executed on all of this connections models.
#event reconnected: Emitted after we connected and subsequently disconnected, followed by successfully another successfull connection.
#event error: Emitted when an error occurs on this connection.
#event fullsetup: Emitted in a replica-set scenario, when primary and at
least one seconaries specified in the connection string are connected.
#event all: Emitted in a replica-set scenario, when all nodes specified in the connection string are connected.
When the database is down you will receive two events:
1. disconnected
2. error (the error that driver encountered)
When the database is up again you will receive the reconnect event.
So you don't need to try catch the error rather you should listen to these events.
More helpful information about connection failures and reconnecting can be found here.
This article explain how to use and configure the autoReconnect and the bufferMaxEntries according to your settings.

Resources