Skip to content

Commit 33c24bb

Browse files
author
ionut.stan
committed
Added watchForUpgrade()
1 parent 8067c40 commit 33c24bb

File tree

5 files changed

+70
-4
lines changed

5 files changed

+70
-4
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "jsonrpc-bidirectional",
33
"description": "Bidirectional JSONRPC over web sockets or HTTP with extensive plugin support.",
4-
"version": "5.6.7",
4+
"version": "5.7.0",
55
"scripts": {
66
"build": "node build.js",
77
"test": "node --expose-gc --max-old-space-size=1024 tests/main.js",
@@ -35,6 +35,7 @@
3535
},
3636
"dependencies": {
3737
"extendable-error-class": "^0.1.1",
38+
"fs-promise": "^2.0.3",
3839
"node-fetch": "^1.6.3",
3940
"sleep-promise": "^2.0.0"
4041
},

src/NodeClusterBase/MasterEndpoint.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
const cluster = require("cluster");
22
const os = require("os");
3+
const assert = require("assert");
4+
const fs = require("fs-promise");
35

46
const sleep = require("sleep-promise");
57
const JSONRPC = {
@@ -47,6 +49,7 @@ class MasterEndpoint extends JSONRPC.EndpointBase
4749
this.objWorkerIDToState = {};
4850

4951
this._bWorkersStarted = false;
52+
this._bWatchingForUpgrade = false;
5053
}
5154

5255

@@ -146,6 +149,64 @@ class MasterEndpoint extends JSONRPC.EndpointBase
146149
}
147150

148151

152+
/**
153+
* If the version in package.json changes, this.gracefulExit() will be invoked.
154+
*
155+
* @param {string} strPackageJSONPath
156+
*
157+
* @returns {undefined}
158+
*/
159+
async watchForUpgrade(strPackageJSONPath)
160+
{
161+
assert(typeof strPackageJSONPath === "string");
162+
163+
if(this._bWatchingForUpgrade)
164+
{
165+
return;
166+
}
167+
168+
this._bWatchingForUpgrade = true;
169+
170+
const strVersion = JSON.parse(await fs.readFile(strPackageJSONPath, "utf8")).version;
171+
let nPackageJSONModificationTime = (await fs.stat(strPackageJSONPath)).mtime.getTime();
172+
let strVersionNew = strVersion;
173+
174+
const nIntervalMilliseconds = 10 * 1000;
175+
176+
const nIntervalID = setInterval(
177+
async () => {
178+
try
179+
{
180+
if(nPackageJSONModificationTime !== (await fs.stat(strPackageJSONPath)).mtime.getTime())
181+
{
182+
nPackageJSONModificationTime = (await fs.stat(strPackageJSONPath)).mtime.getTime();
183+
strVersionNew = JSON.parse(await fs.readFile(strPackageJSONPath, "utf8")).version;
184+
}
185+
186+
if(strVersionNew !== strVersion)
187+
{
188+
clearInterval(nIntervalID);
189+
190+
console.log(`
191+
Updated.
192+
Detected new version ${strVersionNew}.
193+
Old version ${strVersion}.
194+
Attempting to exit gracefully to allow starting with new version.
195+
`.replace(/^\t+/gm, ""));
196+
197+
await this.gracefulExit(null);
198+
}
199+
}
200+
catch(error)
201+
{
202+
console.error(error);
203+
}
204+
},
205+
nIntervalMilliseconds
206+
);
207+
}
208+
209+
149210
/**
150211
* Signals a worker's JSONRPC endpoint is ready to receive calls.
151212
*

src/NodeClusterBase/WorkerClient.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class WorkerClient extends JSONRPC.Client
1313
*/
1414
async gracefulExit()
1515
{
16-
return this.rpc("gracefulExit", []);
16+
return this.rpc("gracefulExit", [], /*bNotification*/ true);
1717
}
1818
};
1919

src/Plugins/Client/WorkerTransport.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,6 @@ class WorkerTransport extends JSONRPC.ClientPluginBase
178178
*/
179179
async beforeJSONDecode(outgoingRequest)
180180
{
181-
outgoingRequest.responseObject = outgoingRequest.responseBody;
182181
}
183182

184183

tests/main_NodeClusterBase.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
const JSONRPC = require("..");
2+
23
const cluster = require("cluster");
34
const assert = require("assert");
5+
const path = require("path");
6+
47
const sleep = require("sleep-promise");
58

9+
610
process.on(
711
"unhandledRejection",
812
(reason, promise) =>
@@ -33,6 +37,7 @@ setInterval(() => {}, 10000);
3337
{
3438
const endpoint = new JSONRPC.NodeClusterBase.MasterEndpoint(JSONRPC.NodeClusterBase.WorkerClient);
3539
await endpoint.start();
40+
await endpoint.watchForUpgrade(path.join(path.dirname(__dirname), "package.json"));
3641

3742
let bNotReady;
3843
console.log("Waiting for workers to all signal they are ready.");
@@ -62,7 +67,7 @@ setInterval(() => {}, 10000);
6267
assert(await endpoint.masterClient.ping("Test") === "Test", "Calling MasterEndpoint.ping() returned the wrong thing.");
6368

6469
console.log("Will call masterClient.gracefulExit().");
65-
await sleep(4000);
70+
await sleep(40000);
6671
// This will call all worker's gracefulExit() methods.
6772
await endpoint.masterClient.gracefulExit();
6873
}

0 commit comments

Comments
 (0)