Skip to content

Commit

Permalink
Refactor socket.io logic to it own class, use optimistic locking in r…
Browse files Browse the repository at this point in the history
…edis for connections, use redis to store conneciton info (openhab#426)

Signed-off-by: Dan Cunningham <[email protected]>
  • Loading branch information
digitaldan authored Feb 19, 2023
1 parent a15d9b4 commit 5dce71e
Show file tree
Hide file tree
Showing 18 changed files with 912 additions and 1,048 deletions.
754 changes: 25 additions & 729 deletions app.js

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions date_util.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ var { DateTime } = require('luxon');
module.exports = function (date, timezone) {
/**
* Convert a Javascript Date into node-time wrapper with the appropriate timezone.
* @param date {Date} Javascript Date object
* @param date {Date} Javascript Date object or ISO String
* @param timezone {String} Olson timezone for this date (e.g. 'America/New_York')
* @return luxon object with the appropriate timezone
*/
return DateTime.fromJSDate(date).setZone(timezone || 'UTC')
if(typeof date === 'string'){
return DateTime.fromISO(date).setZone(timezone || 'UTC')
} else {
return DateTime.fromJSDate(date).setZone(timezone || 'UTC')
}
}
66 changes: 0 additions & 66 deletions jobs/checkopenhabsoffline.js

This file was deleted.

34 changes: 22 additions & 12 deletions jobs/every5minstat.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var cronJob = require('cron').CronJob,
*
* @private
*/
function countCallback (err, count) {
function countCallback(err, count) {
if (!err) {
stats[this] = count;
}
Expand All @@ -31,10 +31,9 @@ function saveStats() {
// validate the results
if (Object.keys(stats).length !== 6) {
// not all data could be retrieved
logger.info('The length of the stats object does not match the expected one, can not save statistical data.');
logger.info('The length of the stats object does not match the expected one, can not save statistical data. %s', stats);
return;
}

// Set current statistics to redis
redis.mset(
[
Expand All @@ -61,15 +60,26 @@ function saveStats() {

module.exports = new cronJob('00 */5 * * * *', function () {
var promises = [];

logger.info('every5min statistics collection job started');
//obtain a lock to update, we don't bother removing as it has a short expire, this is just to avoid unnecessary updates from multiple servers.
redis.set("jobs:every5minstat", "", 'NX', 'EX', 10, (error, result) => {
if (result) {
logger.info('every5min statistics collection job obtained lock');
promises.push(Openhab.count({}, countCallback.bind('openhabCount')).exec());
promises.push(new Promise(resolve => {
redis.eval("return #redis.pcall('keys', 'connection:*')", 0, (err, res) => {
const f = countCallback.bind('openhabOnlineCount');
f(err, res);
resolve(res);
});
}));
//promises.push(Openhab.count({status: 'online'}, countCallback.bind('openhabOnlineCount')).exec());
promises.push(User.count({}, countCallback.bind('userCount')).exec());
promises.push(Invitation.count({ used: true }, countCallback.bind('invitationUsedCount')).exec());
promises.push(Invitation.count({ used: false }, countCallback.bind('invitationUnusedCount')).exec());
promises.push(UserDevice.count({}, countCallback.bind('userDeviceCount')).exec());

promises.push(Openhab.count({}, countCallback.bind('openhabCount')).exec());
promises.push(Openhab.count({status: 'online'}, countCallback.bind('openhabOnlineCount')).exec());
promises.push(User.count({}, countCallback.bind('userCount')).exec());
promises.push(Invitation.count({used:true}, countCallback.bind('invitationUsedCount')).exec());
promises.push(Invitation.count({used:false}, countCallback.bind('invitationUnusedCount')).exec());
promises.push(UserDevice.count({}, countCallback.bind('userDeviceCount')).exec());

Promise.all(promises).then(saveStats);
Promise.all(promises).then(saveStats);
}
});
});
2 changes: 1 addition & 1 deletion logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ logger.auditRequest = function (req) {
requestPath = requestPath.replace('/remote', '');
}

this.audit("%s | %s | %s | %s | %s | %s | %s", req.user.username, req.openhab.status, req.method, requestPath, headers[`x-real-ip`], headers['host'], headers['user-agent'])
this.audit("%s | %s | %s | %s | %s | %s | %s", req.user.username, req.connectionInfo.status, req.method, requestPath, headers[`x-real-ip`], headers['host'], headers['user-agent'])
}

module.exports = logger;
36 changes: 0 additions & 36 deletions mailchimp-myohversion.js

This file was deleted.

37 changes: 12 additions & 25 deletions models/openhab.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,22 @@
var mongoose = require('mongoose'),
Schema = mongoose.Schema,
ObjectId = mongoose.SchemaTypes.ObjectId;

var OpenhabSchema = new Schema({
name: String, // A meaningfull name of openHAB
uuid: {type: String, unique: true}, // openHAB generated UUID
uuid: { type: String, unique: true }, // openHAB generated UUID
secret: String, // openHAB generated secret
config: Schema.Types.Mixed, // openhab.cfg
owner: {type: ObjectId }, // obsolate
account: {type: ObjectId}, // An account openHAB belongs to
openhabVersion: String, // openHAB version
clientVersion: String, // openhab-cloud bundle version
global_location: {type: [Number], index: '2d'}, // openHAB's global location
last_online: { type: Date }, // last seen this openHAB online
last_email_notification: {type: Date}, // last notification about openHAB being offline for long time
status: {type: String, default: "offline"}, // current openHAB status (online/offline)
serverAddress: {type: String}, // the host:port that this openhab is connected to
connectionId: {type: String} // the local instance ID needed when marking openHABs offline
account: { type: ObjectId }, // An account openHAB belongs to
last_online: { type: Date } // last seen this openHAB online
});

// Index for lookups by uuid
OpenhabSchema.index({uuid:1});
OpenhabSchema.index({ uuid: 1 });
// Index for lookups by owner
OpenhabSchema.index({account:1});
// Index for lookups by status
OpenhabSchema.index({status:1, last_online:1});
// Index for lookups by connectionId
OpenhabSchema.index({connectionId:1});
OpenhabSchema.index({ account: 1 });

OpenhabSchema.methods.authenticate = function(openhabUuid, openhabSecret, callback) {
this.model('Openhab').findOne({uuid: openhabUuid, secret: openhabSecret}, function(error, openhab) {
OpenhabSchema.methods.authenticate = function (openhabUuid, openhabSecret, callback) {
this.model('Openhab').findOne({ uuid: openhabUuid, secret: openhabSecret }, function (error, openhab) {
if (error) {
callback(error, false);
} else {
Expand All @@ -42,11 +29,11 @@ OpenhabSchema.methods.authenticate = function(openhabUuid, openhabSecret, callba
});
}

OpenhabSchema.statics.setOffline = function(connectionId, callback) {
OpenhabSchema.statics.setLastOnline = function (id, callback) {
this.model('Openhab').findOneAndUpdate(
{ connectionId: connectionId },
{ $set: { status: 'offline', last_online: new Date()}},
callback );
{ _id: new mongoose.Types.ObjectId(id)},
{ $set: { last_online: new Date() } },
callback);
}

module.exports = mongoose.model('Openhab', OpenhabSchema);
16 changes: 0 additions & 16 deletions models/requestlog.js

This file was deleted.

3 changes: 2 additions & 1 deletion notificationsender/aps-helper.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { APNS, Notification, Errors } = require('apns2'),
app = require('./../app'),
logger = require('./../logger');
logger = require('./../logger'),
fs = require('fs');
let client = null;

if (app.config.apn && app.config.apn.signingKey) {
Expand Down
2 changes: 2 additions & 0 deletions notificationsender/firebase.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const system = require('../system');
const Firebase = require('firebase-messaging');
const logger = require('../logger.js');
const redis = require('../redis-helper');

const firebaseClient = new Firebase(system.getGcmPassword());

const firebaseOptions = {
Expand Down
Loading

0 comments on commit 5dce71e

Please sign in to comment.