@meteorjs/ddp-graceful-shutdown
Advanced tools
Comparing version 1.0.0-beta.5 to 1.0.0-beta.6
100
lib/index.js
@@ -7,4 +7,2 @@ 'use strict'; | ||
function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
@@ -21,5 +19,3 @@ | ||
// Meteor.server. On Galaxy, a good value for gracePeriodMillis is 1000 * | ||
// process.env.METEOR_SIGTERM_GRACE_PERIOD_SECONDS. If installSIGTERMHandler | ||
// is set to false, you are responsibile for calling this.closeConnections | ||
// yourself (perhaps without the log argument). | ||
// process.env.METEOR_SIGTERM_GRACE_PERIOD_SECONDS. | ||
function DDPGracefulShutdown(_ref) { | ||
@@ -29,5 +25,3 @@ var _this = this; | ||
var gracePeriodMillis = _ref.gracePeriodMillis, | ||
server = _ref.server, | ||
_ref$installSIGTERMHa = _ref.installSIGTERMHandler, | ||
installSIGTERMHandler = _ref$installSIGTERMHa === undefined ? true : _ref$installSIGTERMHa; | ||
server = _ref.server; | ||
@@ -44,17 +38,26 @@ _classCallCheck(this, DDPGracefulShutdown); | ||
}); | ||
if (installSIGTERMHandler) { | ||
} | ||
// Sets up a SIGTERM handler to call closeConnections with logging. You should | ||
// either call this function or arrange for closeConnections to be called in | ||
// some other way. | ||
_createClass(DDPGracefulShutdown, [{ | ||
key: 'installSIGTERMHandler', | ||
value: function installSIGTERMHandler() { | ||
var _this2 = this; | ||
process.on('SIGTERM', function () { | ||
_this.closeConnections({ log: true }); | ||
_this2.closeConnections({ log: true }); | ||
}); | ||
} | ||
} | ||
// closeConnections is called when SIGTERM is received (unless you configure | ||
// this class with installSIGTERMHandler: false). It calculates an interval | ||
// for closing connections and starts doing so. If log is specified (the | ||
// default if this is called from the default SIGTERM handler) it will log one | ||
// line to stdout as well. | ||
// closeConnections calculates an interval for closing connections and starts | ||
// doing so. It is intended to be called when SIGTERM is received; | ||
// installSIGTERMHandler arranges for this to happen. If log is specified (the | ||
// default if this is called from the default SIGTERM handler) it will log one | ||
// line to stdout as well. | ||
_createClass(DDPGracefulShutdown, [{ | ||
}, { | ||
key: 'closeConnections', | ||
@@ -79,46 +82,27 @@ value: function closeConnections() { | ||
key: 'closeOneConnectionAndScheduleNext', | ||
value: function () { | ||
var _ref3 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(delay) { | ||
var _this2 = this; | ||
value: function closeOneConnectionAndScheduleNext(delay) { | ||
var _this3 = this; | ||
var _connections$entries$, done, value, _value, id, conn; | ||
Promise.resolve().then(function () { | ||
var _connections$entries$ = _this3.connections.entries().next(), | ||
done = _connections$entries$.done, | ||
value = _connections$entries$.value; | ||
return regeneratorRuntime.wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
_connections$entries$ = this.connections.entries().next(), done = _connections$entries$.done, value = _connections$entries$.value; | ||
if (done) { | ||
return; | ||
} | ||
if (!done) { | ||
_context.next = 3; | ||
break; | ||
} | ||
var _value = _slicedToArray(value, 2), | ||
id = _value[0], | ||
conn = _value[1]; | ||
return _context.abrupt('return'); | ||
case 3: | ||
_value = _slicedToArray(value, 2), id = _value[0], conn = _value[1]; | ||
this.connections.delete(id); | ||
conn.close(); | ||
if (this.connections.size > 0) { | ||
setTimeout(function () { | ||
return _this2.closeOneConnectionAndScheduleNext(delay); | ||
}, delay); | ||
} | ||
case 7: | ||
case 'end': | ||
return _context.stop(); | ||
} | ||
} | ||
}, _callee, this); | ||
})); | ||
function closeOneConnectionAndScheduleNext(_x2) { | ||
return _ref3.apply(this, arguments); | ||
} | ||
return closeOneConnectionAndScheduleNext; | ||
}() | ||
_this3.connections.delete(id); | ||
conn.close(); | ||
if (_this3.connections.size > 0) { | ||
setTimeout(function () { | ||
return _this3.closeOneConnectionAndScheduleNext(delay); | ||
}, delay); | ||
} | ||
}); | ||
} | ||
}]); | ||
@@ -125,0 +109,0 @@ |
{ | ||
"name": "@meteorjs/ddp-graceful-shutdown", | ||
"version": "1.0.0-beta.5", | ||
"version": "1.0.0-beta.6", | ||
"description": "Close DDP connections gradually on server shutdown", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -1,4 +0,6 @@ | ||
# @meteorjs/ddp-graceful-shutdown | ||
# [@meteorjs/ddp-graceful-shutdown](https://www.npmjs.com/package/@meteorjs/ddp-graceful-shutdown) | ||
This package is designed for use with [Meteor](https://www.meteor.com/) apps | ||
[![Build Status](https://travis-ci.org/meteor/ddp-graceful-shutdown.svg?branch=master)](https://travis-ci.org/meteor/ddp-graceful-shutdown) | ||
This npm package is designed for use with [Meteor](https://www.meteor.com/) apps | ||
running on platforms such as Galaxy which send SIGTERM signals and wait a grace | ||
@@ -10,5 +12,14 @@ period before killing processes. | ||
``` javascript | ||
import {DDPGracefulShutdown} from ' | ||
import {DDPGracefulShutdown} from '@meteorjs/ddp-graceful-shutdown'; | ||
import {Meteor} from 'meteor/meteor'; | ||
new DDPGracefulShutdown({ | ||
gracePeriodMillis: 1000 * process.METEOR_SIGTERM_GRACE_PERIOD_SECONDS, | ||
server: Meteor.server, | ||
}).installSIGTERMHandler(); | ||
``` | ||
... finish | ||
This registers a SIGTERM handler which will call | ||
`ddpGracefulShutdown.closeConnections({log: true})`. To trigger on a different | ||
signal, disable logging, or only trigger after some other clean up, just call | ||
that method yourself from an appropriate handler. |
@@ -10,6 +10,4 @@ // DDPGracefulShutdown is a class which tracks open connections in a DDP server | ||
// Meteor.server. On Galaxy, a good value for gracePeriodMillis is 1000 * | ||
// process.env.METEOR_SIGTERM_GRACE_PERIOD_SECONDS. If installSIGTERMHandler | ||
// is set to false, you are responsibile for calling this.closeConnections | ||
// yourself (perhaps without the log argument). | ||
constructor({gracePeriodMillis, server, installSIGTERMHandler = true}) { | ||
// process.env.METEOR_SIGTERM_GRACE_PERIOD_SECONDS. | ||
constructor({gracePeriodMillis, server}) { | ||
this.gracePeriodMillis = gracePeriodMillis; | ||
@@ -23,12 +21,16 @@ this.connections = new Map; | ||
}); | ||
if (installSIGTERMHandler) { | ||
process.on('SIGTERM', () => { | ||
this.closeConnections({log: true}); | ||
}); | ||
} | ||
} | ||
// closeConnections is called when SIGTERM is received (unless you configure | ||
// this class with installSIGTERMHandler: false). It calculates an interval | ||
// for closing connections and starts doing so. If log is specified (the | ||
// Sets up a SIGTERM handler to call closeConnections with logging. You should | ||
// either call this function or arrange for closeConnections to be called in | ||
// some other way. | ||
installSIGTERMHandler() { | ||
process.on('SIGTERM', () => { | ||
this.closeConnections({log: true}); | ||
}); | ||
} | ||
// closeConnections calculates an interval for closing connections and starts | ||
// doing so. It is intended to be called when SIGTERM is received; | ||
// installSIGTERMHandler arranges for this to happen. If log is specified (the | ||
// default if this is called from the default SIGTERM handler) it will log one | ||
@@ -48,13 +50,15 @@ // line to stdout as well. | ||
// the code in a Fiber by calling it from meteor-promise's queue. | ||
async closeOneConnectionAndScheduleNext(delay) { | ||
const {done, value} = this.connections.entries().next(); | ||
if (done) { | ||
return; | ||
} | ||
const [id, conn] = value; | ||
this.connections.delete(id); | ||
conn.close(); | ||
if (this.connections.size > 0) { | ||
setTimeout(() => this.closeOneConnectionAndScheduleNext(delay), delay); | ||
} | ||
closeOneConnectionAndScheduleNext(delay) { | ||
Promise.resolve().then(() => { | ||
const {done, value} = this.connections.entries().next(); | ||
if (done) { | ||
return; | ||
} | ||
const [id, conn] = value; | ||
this.connections.delete(id); | ||
conn.close(); | ||
if (this.connections.size > 0) { | ||
setTimeout(() => this.closeOneConnectionAndScheduleNext(delay), delay); | ||
} | ||
}); | ||
} | ||
@@ -61,0 +65,0 @@ } |
6
25
10480
150