Skip to content

Commit

Permalink
style: make redis_ping async function (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
msimerson authored May 25, 2022
1 parent 4d9cfc7 commit 8dea153
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 35 deletions.
6 changes: 4 additions & 2 deletions Changes.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@


### 2.0.3 - 2022-05-24
### 2.0.4 - 2022-05-25

- fix: rename p* methods -> * (required in redis v4)
- fix: rename p\* methods -> * (required in redis v4)
- fix: add `await ...connect()` as is now required, fixes #32
- fix: make redis_ping async function
- dep(redis): bump 4.0 -> 4.1
- ci: updated syntax
- test: added tests for init_redis_plugin


### 2.0.0 - 2022-03-29
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Use redis in your plugin like so:

```js
if (server.notes.redis) {
server.notes.redis.hgetall(...);
server.notes.redis.hGetAll(...);
// or any other redis command
}
```
Expand Down
40 changes: 13 additions & 27 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,8 @@ exports.load_redis_ini = function () {
}

// backwards compat with node-redis < 4
if (rc.server && !rc.socket) {
rc.socket = rc.server
delete rc.server
}

// same as above
if (rc.db && !rc.database) {
rc.database = rc.db
delete rc.db
}

plugin.redisCfg.pubsub = Object.assign({}, defaultOpts, rc.opts, rc.socket, rc.pubsub);
Expand All @@ -59,11 +52,10 @@ exports.merge_redis_ini = function () {
}

exports.init_redis_shared = function (next, server) {
const plugin = this;

let calledNext = false;
function nextOnce (e) {
if (e) plugin.logerror(`Redis error: ${e.message}`);
if (e) this.logerror(`Redis error: ${e.message}`);
if (calledNext) return;
calledNext = true;
next();
Expand All @@ -72,7 +64,7 @@ exports.init_redis_shared = function (next, server) {
// this is the server-wide redis, shared by plugins that don't
// specificy a db ID.
if (!server.notes.redis) {
plugin.get_redis_client(plugin.redisCfg.server).then(client => {
this.get_redis_client(this.redisCfg.server).then(client => {
server.notes.redis = client
nextOnce()
})
Expand All @@ -82,7 +74,7 @@ exports.init_redis_shared = function (next, server) {
server.notes.redis.ping((err, res) => {
if (err) return nextOnce(err);

plugin.loginfo('already connected');
this.loginfo('already connected');
nextOnce(); // connection is good
});
}
Expand Down Expand Up @@ -135,24 +127,17 @@ exports.shutdown = function () {
exports.redis_ping = async function () {

this.redis_pings=false;
if (!this.db) throw new Error('redis not initialized');

if (!this.db) {
return new Error('redis not initialized');
}

try {
const r = await this.db.ping()
if (r !== 'PONG') return new Error('not PONG');
this.redis_pings=true
}
catch (e) {
this.logerror(e.message)
}
const r = await this.db.ping()
if (r !== 'PONG') throw new Error('not PONG');
this.redis_pings=true
return true
}

function getUriStr (client, opts) {
let msg = `redis://${opts?.socket?.host}:${opts?.socket?.port}`;
if (opts.database) msg += `/${opts.database}`;
if (opts?.database) msg += `/${opts?.database}`;
if (client?.server_info?.redis_version) {
msg += `\tv${client?.server_info?.redis_version}`;
}
Expand All @@ -176,7 +161,7 @@ exports.get_redis_client = async function (opts) {
try {
await client.connect()

if (opts.database) client.dbid = opts.database
if (opts?.database) client.dbid = opts?.database

client.server_info = await client.info()
urlStr = getUriStr(client, opts)
Expand All @@ -186,6 +171,7 @@ exports.get_redis_client = async function (opts) {
}
catch (e) {
console.error(e)
this.logerror(e);
}
}

Expand All @@ -201,7 +187,7 @@ exports.redis_subscribe_pattern = async function (pattern) {

if (this.redis) return // already subscribed?

this.redis = await redis.createClient(this.redisCfg.pubsub)
this.redis = redis.createClient(this.redisCfg.pubsub)
await this.redis.connect()

await this.redis.pSubscribe(pattern);
Expand All @@ -219,7 +205,7 @@ exports.redis_subscribe = async function (connection) {
connection.logerror('redis subscribe timed out');
}, 3 * 1000);

connection.notes.redis = await redis.createClient(this.redisCfg.pubsub)
connection.notes.redis = redis.createClient(this.redisCfg.pubsub)
await connection.notes.redis.connect()

clearTimeout(timer);
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "haraka-plugin-redis",
"version": "2.0.3",
"version": "2.0.4",
"description": "Redis plugin for Haraka & other plugins to inherit from",
"main": "index.js",
"directories": {
Expand All @@ -16,8 +16,8 @@
"mocha": "9"
},
"scripts": {
"lint": "npx eslint *.js test/*.js",
"lintfix": "npx eslint --fix *.js test/*.js",
"lint": "npx eslint *.js test",
"lintfix": "npx eslint --fix *.js test",
"cover": "NODE_ENV=cov npx nyc --reporter=lcovonly npm run test",
"versions": "npx dependency-version-checker check",
"test": "npx mocha"
Expand Down
33 changes: 31 additions & 2 deletions test/redis.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ describe('connects', function () {
retry_strategy: retry,
})
assert.ok(redis);
redis.disconnect()
await redis.quit()
})

it('populates plugin.cfg.redis when asked', async function () {
Expand All @@ -107,6 +107,35 @@ describe('connects', function () {
const res = await client.ping()
assert.equal(res, 'PONG')
assert.ok(client)
await client.disconnect()
await client.quit()
})
})

describe('init_redis_plugin', function () {
before(async function () {
this.server = { notes: { } }

this.plugin = new fixtures.plugin('index')
this.plugin.register()
})

after(function () {
this.plugin.db.quit()
})

it('connects to redis', function (done) {
this.plugin.init_redis_plugin(() => {
assert.ok(this.plugin.db?.server_info)
done()
}, this.server)
})

it('pings and gets PONG answer', function (done) {
this.plugin.redis_ping()
.then(r => {
assert.equal(r, true)
done()
})
.catch(done)
})
})

0 comments on commit 8dea153

Please sign in to comment.