Skip to content

Commit f43927d

Browse files
fix: register/unregister microservice & remote middleware on all workers
1 parent 651bafa commit f43927d

File tree

4 files changed

+16
-13
lines changed

4 files changed

+16
-13
lines changed

__tests__/services/microservice-test.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,13 @@ describe('services/microservice', () => {
6060
const stubbed = sinon.stub(ms, 'sendRequest').resolves();
6161

6262
await ms.gatewayRegisterCancel('gateway');
63-
await ms.gatewayRegisterCancel('gateway', false);
6463

6564
stubbed.restore();
6665

6766
const args = stubbed.firstCall.lastArg;
68-
const args2 = stubbed.secondCall.lastArg;
6967

70-
expect(stubbed).to.callCount(2);
71-
expect(args.reqParams.headers).to.include({ type: 'async' });
72-
expect(args2.reqParams.headers).to.not.include({ type: 'async' });
68+
expect(stubbed).to.callCount(1);
69+
expect(args.reqParams.headers).to.include({ type: 'pub' });
7370
});
7471

7572
it('should throw register remote middleware - before/after', () => {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@lomray/microservice-nodejs-lib",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"description": "Package for create microservice architecture based on NodeJS & inverted json.",
55
"main": "lib/index.js",
66
"types": "lib/index.d.ts",

src/services/microservice.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,8 @@ class Microservice extends AbstractMicroservice {
149149
`${gatewayName}.${this.autoRegistrationEndpoint}`,
150150
{ action: AutoRegistrationAction.ADD },
151151
{
152-
// timeout 10 min - wait until gateway becomes available
153-
reqParams: { timeout },
152+
// timeout 10 min - wait until gateway becomes available, type: pub - message will be received by all gateways
153+
reqParams: { timeout, headers: { type: 'pub' } },
154154
},
155155
);
156156

@@ -164,13 +164,18 @@ class Microservice extends AbstractMicroservice {
164164
/**
165165
* Cancel microservice registration at gateway
166166
*/
167-
public gatewayRegisterCancel(gatewayName: string, isAsync = true): Promise<MicroserviceResponse> {
167+
public gatewayRegisterCancel(gatewayName: string): Promise<MicroserviceResponse> {
168168
return this.sendRequest(
169169
`${gatewayName}.${this.autoRegistrationEndpoint}`,
170170
{
171171
action: AutoRegistrationAction.REMOVE,
172172
},
173-
{ reqParams: { headers: isAsync ? { type: 'async' } : {} } },
173+
{
174+
reqParams: {
175+
// type: pub - message will be received by all gateways
176+
headers: { type: 'pub' },
177+
},
178+
},
174179
);
175180
}
176181

src/services/remote-middleware.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,8 @@ class RemoteMiddleware {
175175
const { timeout = 1000 * 60 * 5, shouldCancelRegister = true } = params;
176176

177177
const result = await this.microservice.sendRequest(`${microservice}.${this.endpoint}`, data, {
178-
// timeout 5 min - wait until microservice becomes available
179-
reqParams: { timeout },
178+
// timeout 5 min - wait until gateway becomes available, type: pub - message will be received by all gateways
179+
reqParams: { timeout, headers: { type: 'pub' } },
180180
});
181181

182182
if (shouldCancelRegister) {
@@ -207,7 +207,8 @@ class RemoteMiddleware {
207207
action: RemoteMiddlewareActionType.REMOVE,
208208
method,
209209
},
210-
{ reqParams: { headers: { type: 'async' } } },
210+
// type: pub - message will be received by all gateways
211+
{ reqParams: { headers: { type: 'pub' } } },
211212
);
212213
});
213214

0 commit comments

Comments
 (0)