Skip to content

Commit

Permalink
Load Balancing Release Improvements (openhab#148)
Browse files Browse the repository at this point in the history
* Adding additional changes to support multiple node processes

Signed-off-by: digitaldan <[email protected]>

* Adding mute notifications from the cloud server, support reloading the config from a HUP signal

Signed-off-by: digitaldan <[email protected]>

* Do not append remote hack if another server has already done so

Signed-off-by: digitaldan <[email protected]>
  • Loading branch information
digitaldan authored Jan 19, 2018
1 parent 3e11d6d commit f5e19c6
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 35 deletions.
89 changes: 58 additions & 31 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,20 @@ var logger = require('./logger.js'),
env = process.env.NODE_ENV || 'production',
config;

if (env !== 'development') {
config = require('./config.json');
} else {
config = require('./config-development.json');

//load and set our configuration, delete any cache first
var loadConfig = function() {
if (env !== 'development') {
delete require.cache[require.resolve('./config.json')];
config = require('./config.json');
} else {
delete require.cache[require.resolve('./config-development.json')];
config = require('./config-development.json');
}
system.setConfiguration(config);
}

system.setConfiguration(config);
loadConfig();

var internalAddress = system.getInternalAddress();

Expand All @@ -42,6 +49,11 @@ process.on('uncaughtException', function (err) {
logger.error(err);
});

process.on('SIGHUP', function () {
logger.info('Reloading config...');
loadConfig();
});

logger.info('openHAB-cloud: Backend logging initialized...');

// Initialize the main configuration
Expand Down Expand Up @@ -120,10 +132,8 @@ var offlineOpenhabs = {};
* @param error The error, if an error occured
* @param {Openhab} openhab The openHAB instance
*/
function notifyOpenHABOwnerOffline(error, openhab) {
if (!openhab || error) {
return;
}
function notifyOpenHABOwnerOffline(openhab) {

openhab.status = 'offline';
openhab.last_online = new Date();
openhab.save(function (error) {
Expand All @@ -149,7 +159,7 @@ function notifyOpenHABOwnerOffline(error, openhab) {
// This timer runs every minute and checks if there are any openHABs in offline status for more then 300 sec
// Then it sends notifications to openHAB's owner if it is offline for more then 300 sec
// This timer only runs on the job task
if (taskEnv === 'job') {
if (taskEnv === 'main') {
setInterval(function () {
logger.debug('openHAB-cloud: Checking for offline openHABs (' + Object.keys(offlineOpenhabs).length + ')');
for (var offlineOpenhabUuid in offlineOpenhabs) {
Expand All @@ -159,7 +169,15 @@ if (taskEnv === 'job') {
logger.debug('openHAB-cloud: openHAB with ' + offlineOpenhabUuid + ' is offline > 300 sec, time to notify the owner');
Openhab.findOne({
uuid: offlineOpenhabUuid
}).cache().exec(notifyOpenHABOwnerOffline);
}).exec(function (error, openhab) {
if (!openhab || error) {
return;
}
//if this has not connected to another server, then notify
if(openhab.serverAddress == internalAddress){
notifyOpenHABOwnerOffline(openhab);
}
});
}
}, 60000);
}
Expand Down Expand Up @@ -309,7 +327,10 @@ app.use(function (req, res, next) {
}
// If host matches names for full /* proxying, go ahead and just proxy it.
if (host.indexOf('remote.') === 0 || host.indexOf('home.') === 0) {
//make sure this was not set by another server
if(req.url.indexOf('/remote') != 0){
req.url = '/remote' + req.url;
}
}
next();
});
Expand Down Expand Up @@ -940,26 +961,32 @@ io.sockets.on('connection', function (socket) {
});

function notifyOpenHABStatusChange(openhab, status) {
User.find({
account: openhab.account,
role: 'master'
}, function (error, users) {
if (!error && users) {
for (var i = 0; i < users.length; i++) {
if (status === 'online') {
sendNotificationToUser(users[i], 'openHAB is online', 'openhab', 'good');
} else {
sendNotificationToUser(users[i], 'openHAB is offline', 'openhab', 'bad');
}
}
} else {
if (error) {
logger.warn('openHAB-cloud: Error finding users to notify: ' + error);
} else {
logger.warn('openHAB-cloud: Unable to find any masters for openHAB ' + openhab.uuid);
}
}
});

//we can mute notifications, for example when we are doing a deploy
if(system.getMuteNotifications()){
return;
}

User.find({
account: openhab.account,
role: 'master'
}, function (error, users) {
if (!error && users) {
for (var i = 0; i < users.length; i++) {
if (status === 'online') {
sendNotificationToUser(users[i], 'openHAB is online', 'openhab', 'good');
} else {
sendNotificationToUser(users[i], 'openHAB is offline', 'openhab', 'bad');
}
}
} else {
if (error) {
logger.warn('openHAB-cloud: Error finding users to notify: ' + error);
} else {
logger.warn('openHAB-cloud: Unable to find any masters for openHAB ' + openhab.uuid);
}
}
});
}

function shutdown() {
Expand Down
3 changes: 2 additions & 1 deletion config-production.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"port": "443",
"protocol": "https",
"logging": "info",
"subDomainCookies": false
"subDomainCookies": false,
"muteNotifications": false
},
"express":{
"key" : "some express key"
Expand Down
6 changes: 3 additions & 3 deletions etc/openhabcloud-job.service → etc/[email protected]
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
[Unit]
Description=node.js openhab cloud server jobs
Description=node.js openhab cloud server www process (no disconnect notices/socket management)

[Service]
User=openhabcloud
Group=openhabcloud
LimitNOFILE=300000
Environment=NODE_ENV=production
Environment=TASK=main
Environment=PORT=3030
Environment=TASK=web
Environment=PORT=%i
Environment=HOST=127.0.0.1
#Environment=DEBUG=oath2orize
Restart=always
Expand Down
13 changes: 13 additions & 0 deletions system/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,19 @@ System.prototype.getProtocol = function() {
System.prototype.getBaseURL = function() {
return this.getProtocol() + '://' + this.getHost() + ':' + this.getPort();
};

/**
* Returns if we are muting notifications
* @returns {Boolean}
*/
System.prototype.getMuteNotifications = function() {
try {
return this.getConfig(['system', 'muteNotifications']);
} catch (err) {
return false;
}
};

/**
* Checks, if new user registration should be enabled or not.
*
Expand Down

0 comments on commit f5e19c6

Please sign in to comment.