Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add server teardown ability #5410

Merged
merged 2 commits into from
Jan 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions lib/bus.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
'use strict';

var Stream = require('stream');

function init (settings) {
var beats = 0;
var started = new Date( );
var interval = settings.heartbeat * 1000;
let busInterval;

var stream = new Stream;

Expand All @@ -24,9 +24,15 @@ function init (settings) {
stream.emit('tick', ictus( ));
}

stream.teardown = function ( ) {
console.log('Initiating server teardown');
clearInterval(busInterval);
stream.emit('teardown');
};

stream.readable = true;
stream.uptime = repeat;
setInterval(repeat, interval);
busInterval = setInterval(repeat, interval);
return stream;
}
module.exports = init;
Expand Down
18 changes: 12 additions & 6 deletions lib/plugins/bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ var engine = require('share2nightscout-bridge');
// Track the most recently seen record
var mostRecentRecord;

function init (env) {
function init (env, bus) {
if (env.extendedSettings.bridge && env.extendedSettings.bridge.userName && env.extendedSettings.bridge.password) {
return create(env);
return create(env, bus);
} else {
console.info('Dexcom bridge not enabled');
}
Expand Down Expand Up @@ -56,26 +56,32 @@ function options (env) {
};
}

function create (env) {
function create (env, bus) {

var bridge = { };

var opts = options(env);
var interval = opts.interval;

mostRecentRecord = new Date().getTime() - opts.fetch.minutes * 60000
mostRecentRecord = new Date().getTime() - opts.fetch.minutes * 60000;

bridge.startEngine = function startEngine (entries) {

opts.callback = bridged(entries);

setInterval(function () {
let timer = setInterval(function () {
opts.fetch.minutes = parseInt((new Date() - mostRecentRecord) / 60000);
opts.fetch.maxCount = parseInt((opts.fetch.minutes / 5) + 1);
opts.firstFetchCount = opts.fetch.maxCount
opts.firstFetchCount = opts.fetch.maxCount;
console.log("Fetching Share Data: ", 'minutes', opts.fetch.minutes, 'maxCount', opts.fetch.maxCount);
engine(opts);
}, interval);

if (bus) {
bus.on('teardown', function serverTeardown () {
clearInterval(timer);
});
}
};

return bridge;
Expand Down
14 changes: 10 additions & 4 deletions lib/plugins/mmconnect.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
var _ = require('lodash'),
connect = require('minimed-connect-to-nightscout');

function init (env, entries, devicestatus) {
function init (env, entries, devicestatus, bus) {
if (env.extendedSettings.mmconnect && env.extendedSettings.mmconnect.userName && env.extendedSettings.mmconnect.password) {
return {run: makeRunner(env, entries, devicestatus)};
return {run: makeRunner(env, entries, devicestatus, bus)};
} else {
console.info('MiniMed Connect not enabled');
return null;
}
}

function makeRunner (env, entries, devicestatus) {
function makeRunner (env, entries, devicestatus, bus) {
var options = getOptions(env);

var client = connect.carelink.Client(options);
Expand All @@ -22,9 +22,15 @@ function makeRunner (env, entries, devicestatus) {
var handleData = makeHandler_(entries, devicestatus, options.sgvLimit, options.storeRawData);

return function run () {
setInterval(function() {
let timer = setInterval(function() {
client.fetch(handleData);
}, options.interval);

if (bus) {
bus.on('teardown', function serverTeardown () {
clearInterval(timer);
});
}
};
}

Expand Down
4 changes: 2 additions & 2 deletions lib/server/bootevent.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ function boot (env, language) {
return next();
}

ctx.bridge = require('../plugins/bridge')(env);
ctx.bridge = require('../plugins/bridge')(env, ctx.bus);
if (ctx.bridge) {
ctx.bridge.startEngine(ctx.entries);
}
Expand All @@ -255,7 +255,7 @@ function boot (env, language) {
return next();
}

ctx.mmconnect = require('../plugins/mmconnect').init(env, ctx.entries, ctx.devicestatus);
ctx.mmconnect = require('../plugins/mmconnect').init(env, ctx.entries, ctx.devicestatus, ctx.bus);
if (ctx.mmconnect) {
ctx.mmconnect.run();
}
Expand Down
7 changes: 7 additions & 0 deletions lib/server/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ function init (env, ctx, server) {
'browser client etag': true,
'browser client gzip': false
});

ctx.bus.on('teardown', function serverTeardown () {
Object.keys(io.sockets.sockets).forEach(function(s) {
io.sockets.sockets[s].disconnect(true);
});
io.close();
});
}

function verifyAuthorization (message, callback) {
Expand Down
8 changes: 7 additions & 1 deletion server.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ require('./lib/server/bootevent')(env, language).boot(function booted (ctx) {
return;
}

ctx.bus.on('teardown', function serverTeardown () {
server.close();
clearTimeout(sendStartupAllClearTimer);
ctx.store.client.close();
});

///////////////////////////////////////////////////
// setup socket io for data and message transmission
///////////////////////////////////////////////////
Expand All @@ -68,7 +74,7 @@ require('./lib/server/bootevent')(env, language).boot(function booted (ctx) {
});

//after startup if there are no alarms send all clear
setTimeout(function sendStartupAllClear () {
let sendStartupAllClearTimer = setTimeout(function sendStartupAllClear () {
var alarm = ctx.notifications.findHighestAlarm();
if (!alarm) {
ctx.bus.emit('notification', {
Expand Down
2 changes: 1 addition & 1 deletion tests/api3.basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('Basic REST API3', function() {


after(function after () {
self.instance.server.close();
self.instance.ctx.bus.teardown();
});


Expand Down
2 changes: 1 addition & 1 deletion tests/api3.create.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ describe('API3 CREATE', function() {


after(() => {
self.instance.server.close();
self.instance.ctx.bus.teardown();
});


Expand Down
2 changes: 1 addition & 1 deletion tests/api3.delete.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe('API3 UPDATE', function() {


after(() => {
self.instance.server.close();
self.instance.ctx.bus.teardown();
});


Expand Down
2 changes: 1 addition & 1 deletion tests/api3.generic.workflow.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe('Generic REST API3', function() {


after(() => {
self.instance.server.close();
self.instance.ctx.bus.teardown();
});


Expand Down
2 changes: 1 addition & 1 deletion tests/api3.patch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe('API3 PATCH', function() {


after(() => {
self.instance.server.close();
self.instance.ctx.bus.teardown();
});


Expand Down
2 changes: 1 addition & 1 deletion tests/api3.read.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe('API3 READ', function() {


after(() => {
self.instance.server.close();
self.instance.ctx.bus.teardown();
});


Expand Down
2 changes: 1 addition & 1 deletion tests/api3.search.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ describe('API3 SEARCH', function() {


after(() => {
self.instance.server.close();
self.instance.ctx.bus.teardown();
});


Expand Down
8 changes: 4 additions & 4 deletions tests/api3.security.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const request = require('supertest')
, moment = require('moment')
;
require('should');

describe('Security of REST API3', function() {
const self = this
, instance = require('./fixtures/api3/instance')
Expand All @@ -26,10 +26,10 @@ describe('Security of REST API3', function() {
self.token = authResult.token;
});


after(() => {
self.http.server.close();
self.https.server.close();
self.http.ctx.bus.teardown();
self.https.ctx.bus.teardown();
});


Expand Down
2 changes: 1 addition & 1 deletion tests/api3.socket.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe('Socket.IO in REST API3', function() {
if(self.instance && self.instance.clientSocket && self.instance.clientSocket.connected) {
self.instance.clientSocket.disconnect();
}
self.instance.server.close();
self.instance.ctx.bus.teardown();
});


Expand Down
2 changes: 1 addition & 1 deletion tests/api3.update.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe('API3 UPDATE', function() {


after(() => {
self.instance.server.close();
self.instance.ctx.bus.teardown();
});


Expand Down