make subscriber only start when there is any listener so its not needed in gulptasks for example

change how to close redis since there are more than 1 connection for other thigns
merge-requests/218/head
Thomas Lynch 3 years ago
parent d29b82587f
commit 9ad03e096a
  1. 10
      gulpfile.js
  2. 35
      redis.js
  3. 7
      server.js

@ -53,7 +53,7 @@ async function password() {
await Accounts.changePassword('admin', randomPassword);
console.log('=====LOGIN DETAILS=====\nusername: admin\npassword:', randomPassword, '\n=======================');
Redis.redisClient.quit();
Redis.close();
return Mongo.client.close();
}
@ -64,7 +64,7 @@ async function ips() {
const Redis = require(__dirname+'/redis.js')
const { func: ipSchedule } = require(__dirname+'/schedules/tasks/ips.js');
await ipSchedule();
Redis.redisClient.quit();
Redis.close();
return Mongo.client.close();
}
@ -144,7 +144,7 @@ async function wipe() {
});
await Mongo.client.close();
Redis.redisClient.quit();
Redis.close();
//delete all the static files
return Promise.all([
@ -247,7 +247,7 @@ async function cache() {
Redis.deletePattern('overboard'),
Redis.deletePattern('catalog'),
]);
Redis.redisClient.quit();
Redis.close();
}
function deletehtml() {
@ -387,7 +387,7 @@ async function migrate() {
}
await Mongo.client.close();
Redis.redisClient.quit();
Redis.close();
}

@ -9,27 +9,34 @@ const Redis = require('ioredis')
'config': [], //others in future?
}
subscriber.subscribe('config', (err, count) => {
if (err) {
return console.error(err);
}
console.log(`Redis subscribed to ${count} channels`);
});
subscriber.on("message", (channel, message) => {
messageCallbacks[channel].forEach(cb => {
cb(message);
})
});
module.exports = {
redisClient: sharedClient,
redisSubsriber: subscriber,
redisPublisher: publisher,
close: () => {
sharedClient.quit();
publisher.quit();
subscriber.quit();
},
addCallback: (channel, cb) => {
messageCallbacks[channel].push(cb);
if (messageCallbacks.length == 0) {
subscriber.subscribe('config', (err, count) => {
if (err) {
return console.error(err);
}
console.log(`Redis subscribed to ${count} channels`);
});
subscriber.on("message", (channel, message) => {
const data = JSON.parse(message);
messageCallbacks[channel].forEach(cb => {
cb(data);
})
});
}
messageCallbacks[channel].push();
},
//get a value with key

@ -32,7 +32,7 @@ const getConfig = require(__dirname+'/getconfig.js')
// connect to redis
debugLogs && console.log('CONNECTING TO REDIS');
const { redisClient, addCallback } = require(__dirname+'/redis.js');
const redis = require(__dirname+'/redis.js');
// disable useless express header
app.disable('x-powered-by');
@ -60,7 +60,6 @@ const getConfig = require(__dirname+'/getconfig.js')
app.set('views', views);
const loadAppLocals = () => {
console.log('loadapplocals')
const { cacheTemplates, boardDefaults, globalLimits, captchaOptions,
enableUserBoardCreation, enableUserAccountCreation, cookieSecret,
debugLogs, ipHashPermLevel, meta, enableWebring } = getConfig();
@ -94,7 +93,7 @@ console.log('loadapplocals')
}
}
loadAppLocals();
addCallback('config', loadAppLocals);
redis.addCallback('config', loadAppLocals);
// routes
if (!production) {
@ -180,7 +179,7 @@ console.log('loadapplocals')
Mongo.client.close();
//close redis connection
debugLogs && console.log('DISCONNECTING REDIS')
redisClient.quit();
redis.close();
// now close without error
process.exit(0);
});

Loading…
Cancel
Save