Skip to content

Commit

Permalink
fixes #1121
Browse files Browse the repository at this point in the history
  • Loading branch information
icebob committed Oct 1, 2022
1 parent 3c8a0ed commit 73e16ba
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
51 changes: 51 additions & 0 deletions dev/issue-1121.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const ServiceBroker = require("../src/service-broker");
const fs = require("fs");

const broker = new ServiceBroker();

broker.createService({
name: "service-creator",
actions: {
createService(ctx) {
return this.broker.createService({
name: `${ctx.params.name}-${ctx.params.id}`
});
}
},
events: {
hello(ctx) {}
}
});

broker.start().then(() => {
broker.repl();

let id = 1;
setInterval(() => {
broker
.call("service-creator.createService", { name: "Service", id: id++ })
.then(res => {
broker.logger.info(res.name);
return res;
})
.then(async res => broker.destroyService(res))
.then(() => {
broker.logger.info(
`broker.registry.nodes.localNode.services.length: ${broker.registry.nodes.localNode.services.length}`
);
broker.logger.info(
`broker.registry.actions.actions.length: ${broker.registry.actions.actions.size}`
);
broker.logger.info(
`broker.registry.events.events.length: ${broker.registry.events.events.size}`
);
broker.logger.info(`broker.services.length: ${broker.services.length}`);

// fs.writeFileSync(
// "./nodeInfo.json",
// JSON.stringify(broker.registry.getLocalNodeInfo(), null, 2)
// );
})
.catch(err => broker.logger.error(err.message));
}, 1000);
});
11 changes: 9 additions & 2 deletions src/registry/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,9 +377,16 @@ class Registry {
* @memberof Registry
*/
unregisterService(fullName, nodeID) {
this.services.remove(fullName, nodeID || this.broker.nodeID);
nodeID = nodeID || this.broker.nodeID;
this.services.remove(fullName, nodeID);

if (!nodeID || nodeID == this.broker.nodeID) {
if (nodeID == this.broker.nodeID) {
// Clean the local node services
const idx = this.nodes.localNode.services.findIndex(svc => svc.fullName === fullName);
if (idx !== -1) this.nodes.localNode.services.splice(idx, 1);
}

if (nodeID == this.broker.nodeID) {
this.regenerateLocalRawInfo(true);
}
}
Expand Down
4 changes: 4 additions & 0 deletions test/unit/registry/registry.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -418,9 +418,13 @@ describe("Test Registry.unregisterService & unregisterServicesByNode", () => {
it("should call services remove method with local nodeID", () => {
registry.regenerateLocalRawInfo.mockClear();
registry.services.remove.mockClear();
registry.nodes.localNode.services.push({ name: "posts", version: 2, fullName: "v2.posts" });
expect(registry.nodes.localNode.services.length).toBe(2);

registry.unregisterService("v2.posts");

expect(registry.nodes.localNode.services.length).toBe(1);

expect(registry.services.remove).toHaveBeenCalledTimes(1);
expect(registry.services.remove).toHaveBeenCalledWith("v2.posts", broker.nodeID);

Expand Down

0 comments on commit 73e16ba

Please sign in to comment.