Skip to content

Commit

Permalink
chore: create network broker package (RocketChat#33338)
Browse files Browse the repository at this point in the history
  • Loading branch information
sampaiodiego authored Sep 23, 2024
1 parent 827850d commit a6b9152
Show file tree
Hide file tree
Showing 36 changed files with 145 additions and 20 deletions.
1 change: 1 addition & 0 deletions _templates/service/new/package.json.ejs.t
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ to: ee/apps/<%= name %>/package.json
"dependencies": {
"@rocket.chat/core-services": "workspace:^",
"@rocket.chat/core-typings": "workspace:^",
"@rocket.chat/network-broker": "workspace:^",
"@rocket.chat/emitter": "next",
"@rocket.chat/model-typings": "workspace:^",
"@rocket.chat/models": "workspace:^",
Expand Down
4 changes: 2 additions & 2 deletions _templates/service/new/service.ejs.t
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
---
to: ee/apps/<%= name %>/src/service.ts
---
import { api } from '@rocket.chat/core-services';
import { broker } from '@rocket.chat/network-broker';
import type { Document } from 'mongodb';
import polka from 'polka';
import { api } from '@rocket.chat/core-services';

import { broker } from '../../../../apps/meteor/ee/server/startup/broker';
import { Collections, getCollection, getConnection } from '../../../../apps/meteor/ee/server/services/mongo';
import { registerServiceModels } from '../../../../apps/meteor/ee/server/lib/registerServiceModels';

Expand Down
3 changes: 1 addition & 2 deletions apps/meteor/ee/server/services/ecdh-proxy/service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import '../../startup/broker';

import '@rocket.chat/network-broker';
import { api } from '@rocket.chat/core-services';

import { ECDHProxy } from './ECDHProxy';
Expand Down
1 change: 1 addition & 0 deletions apps/meteor/ee/server/services/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"@rocket.chat/message-parser": "workspace:^",
"@rocket.chat/model-typings": "workspace:^",
"@rocket.chat/models": "workspace:^",
"@rocket.chat/network-broker": "workspace:^",
"@rocket.chat/rest-typings": "workspace:^",
"@rocket.chat/string-helpers": "~0.31.25",
"@rocket.chat/ui-kit": "workspace:~",
Expand Down
2 changes: 1 addition & 1 deletion apps/meteor/ee/server/startup/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { isRunningMs } from '../../../server/lib/isRunningMs';
export const registerEEBroker = async (): Promise<void> => {
// only starts network broker if running in micro services mode
if (isRunningMs()) {
const { broker } = await import('./broker');
const { broker } = await import('@rocket.chat/network-broker');

api.setBroker(broker);
void api.start();
Expand Down
1 change: 1 addition & 0 deletions apps/meteor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@
"@rocket.chat/model-typings": "workspace:^",
"@rocket.chat/models": "workspace:^",
"@rocket.chat/mp3-encoder": "0.24.0",
"@rocket.chat/network-broker": "workspace:^",
"@rocket.chat/omnichannel-services": "workspace:^",
"@rocket.chat/onboarding-ui": "~0.33.3",
"@rocket.chat/password-policies": "workspace:^",
Expand Down
3 changes: 3 additions & 0 deletions ee/apps/account-service/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ COPY ./packages/logger/dist packages/logger/dist

COPY ./packages/server-cloud-communication/ packages/server-cloud-communication/

COPY ./ee/packages/network-broker/package.json ee/packages/network-broker/package.json
COPY ./ee/packages/network-broker/dist ee/packages/network-broker/dist

COPY ./ee/packages/license/package.json packages/license/package.json
COPY ./ee/packages/license/dist packages/license/dist

Expand Down
1 change: 1 addition & 0 deletions ee/apps/account-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"@rocket.chat/emitter": "~0.31.25",
"@rocket.chat/model-typings": "workspace:^",
"@rocket.chat/models": "workspace:^",
"@rocket.chat/network-broker": "workspace:^",
"@rocket.chat/rest-typings": "workspace:^",
"@rocket.chat/string-helpers": "~0.31.25",
"@rocket.chat/tools": "workspace:^",
Expand Down
2 changes: 1 addition & 1 deletion ee/apps/account-service/src/service.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { api } from '@rocket.chat/core-services';
import { broker } from '@rocket.chat/network-broker';
import type { Document } from 'mongodb';
import polka from 'polka';

import { registerServiceModels } from '../../../../apps/meteor/ee/server/lib/registerServiceModels';
import { Collections, getCollection, getConnection } from '../../../../apps/meteor/ee/server/services/mongo';
import { broker } from '../../../../apps/meteor/ee/server/startup/broker';

const PORT = process.env.PORT || 3033;

Expand Down
3 changes: 3 additions & 0 deletions ee/apps/authorization-service/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ COPY ./packages/logger/dist packages/logger/dist

COPY ./packages/server-cloud-communication/ packages/server-cloud-communication/

COPY ./ee/packages/network-broker/package.json ee/packages/network-broker/package.json
COPY ./ee/packages/network-broker/dist ee/packages/network-broker/dist

COPY ./ee/packages/license/package.json packages/license/package.json
COPY ./ee/packages/license/dist packages/license/dist

Expand Down
1 change: 1 addition & 0 deletions ee/apps/authorization-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"@rocket.chat/emitter": "~0.31.25",
"@rocket.chat/model-typings": "workspace:^",
"@rocket.chat/models": "workspace:^",
"@rocket.chat/network-broker": "workspace:^",
"@rocket.chat/rest-typings": "workspace:^",
"@rocket.chat/string-helpers": "~0.31.25",
"@types/node": "^14.18.63",
Expand Down
2 changes: 1 addition & 1 deletion ee/apps/authorization-service/src/service.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { api } from '@rocket.chat/core-services';
import { broker } from '@rocket.chat/network-broker';
import type { Document } from 'mongodb';
import polka from 'polka';

import { registerServiceModels } from '../../../../apps/meteor/ee/server/lib/registerServiceModels';
import { Collections, getCollection, getConnection } from '../../../../apps/meteor/ee/server/services/mongo';
import { broker } from '../../../../apps/meteor/ee/server/startup/broker';

const PORT = process.env.PORT || 3034;

Expand Down
3 changes: 3 additions & 0 deletions ee/apps/ddp-streamer/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ COPY ./packages/logger/dist packages/logger/dist

COPY ./packages/server-cloud-communication/ packages/server-cloud-communication/

COPY ./ee/packages/network-broker/package.json ee/packages/network-broker/package.json
COPY ./ee/packages/network-broker/dist ee/packages/network-broker/dist

COPY ./ee/packages/license/package.json packages/license/package.json
COPY ./ee/packages/license/dist packages/license/dist

Expand Down
1 change: 1 addition & 0 deletions ee/apps/ddp-streamer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"@rocket.chat/logger": "workspace:^",
"@rocket.chat/model-typings": "workspace:^",
"@rocket.chat/models": "workspace:^",
"@rocket.chat/network-broker": "workspace:^",
"@rocket.chat/rest-typings": "workspace:^",
"@rocket.chat/string-helpers": "~0.31.25",
"colorette": "^1.4.0",
Expand Down
2 changes: 1 addition & 1 deletion ee/apps/ddp-streamer/src/service.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { api } from '@rocket.chat/core-services';
import { broker } from '@rocket.chat/network-broker';
import type { Document } from 'mongodb';

import { registerServiceModels } from '../../../../apps/meteor/ee/server/lib/registerServiceModels';
import { Collections, getCollection, getConnection } from '../../../../apps/meteor/ee/server/services/mongo';
import { broker } from '../../../../apps/meteor/ee/server/startup/broker';

(async () => {
const db = await getConnection();
Expand Down
3 changes: 3 additions & 0 deletions ee/apps/omnichannel-transcript/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ COPY ./packages/logger/dist packages/logger/dist

COPY ./packages/server-cloud-communication/ packages/server-cloud-communication/

COPY ./ee/packages/network-broker/package.json ee/packages/network-broker/package.json
COPY ./ee/packages/network-broker/dist ee/packages/network-broker/dist

COPY ./ee/packages/license/package.json packages/license/package.json
COPY ./ee/packages/license/dist packages/license/dist

Expand Down
1 change: 1 addition & 0 deletions ee/apps/omnichannel-transcript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"@rocket.chat/logger": "workspace:^",
"@rocket.chat/model-typings": "workspace:^",
"@rocket.chat/models": "workspace:^",
"@rocket.chat/network-broker": "workspace:^",
"@rocket.chat/omnichannel-services": "workspace:^",
"@rocket.chat/pdf-worker": "workspace:^",
"@rocket.chat/tools": "workspace:^",
Expand Down
2 changes: 1 addition & 1 deletion ee/apps/omnichannel-transcript/src/service.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { api } from '@rocket.chat/core-services';
import { Logger } from '@rocket.chat/logger';
import { broker } from '@rocket.chat/network-broker';
import type { Document } from 'mongodb';
import polka from 'polka';

import { registerServiceModels } from '../../../../apps/meteor/ee/server/lib/registerServiceModels';
import { Collections, getCollection, getConnection } from '../../../../apps/meteor/ee/server/services/mongo';
import { broker } from '../../../../apps/meteor/ee/server/startup/broker';

const PORT = process.env.PORT || 3036;

Expand Down
3 changes: 3 additions & 0 deletions ee/apps/presence-service/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ COPY ./packages/logger/dist packages/logger/dist

COPY ./packages/server-cloud-communication/ packages/server-cloud-communication/

COPY ./ee/packages/network-broker/package.json ee/packages/network-broker/package.json
COPY ./ee/packages/network-broker/dist ee/packages/network-broker/dist

COPY ./ee/packages/license/package.json packages/license/package.json
COPY ./ee/packages/license/dist packages/license/dist

Expand Down
1 change: 1 addition & 0 deletions ee/apps/presence-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"@rocket.chat/emitter": "~0.31.25",
"@rocket.chat/model-typings": "workspace:^",
"@rocket.chat/models": "workspace:^",
"@rocket.chat/network-broker": "workspace:^",
"@rocket.chat/presence": "workspace:^",
"@rocket.chat/string-helpers": "~0.31.25",
"@types/node": "^14.18.63",
Expand Down
2 changes: 1 addition & 1 deletion ee/apps/presence-service/src/service.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { api } from '@rocket.chat/core-services';
import { broker } from '@rocket.chat/network-broker';
import type { Document } from 'mongodb';
import polka from 'polka';

import { registerServiceModels } from '../../../../apps/meteor/ee/server/lib/registerServiceModels';
import { Collections, getCollection, getConnection } from '../../../../apps/meteor/ee/server/services/mongo';
import { broker } from '../../../../apps/meteor/ee/server/startup/broker';

const PORT = process.env.PORT || 3031;

Expand Down
3 changes: 3 additions & 0 deletions ee/apps/queue-worker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ COPY ./packages/logger/dist packages/logger/dist

COPY ./packages/server-cloud-communication/ packages/server-cloud-communication/

COPY ./ee/packages/network-broker/package.json ee/packages/network-broker/package.json
COPY ./ee/packages/network-broker/dist ee/packages/network-broker/dist

COPY ./ee/packages/license/package.json packages/license/package.json
COPY ./ee/packages/license/dist packages/license/dist

Expand Down
1 change: 1 addition & 0 deletions ee/apps/queue-worker/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"@rocket.chat/logger": "workspace:^",
"@rocket.chat/model-typings": "workspace:^",
"@rocket.chat/models": "workspace:^",
"@rocket.chat/network-broker": "workspace:^",
"@rocket.chat/omnichannel-services": "workspace:^",
"@types/node": "^14.18.63",
"ejson": "^2.2.3",
Expand Down
2 changes: 1 addition & 1 deletion ee/apps/queue-worker/src/service.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { api } from '@rocket.chat/core-services';
import { Logger } from '@rocket.chat/logger';
import { broker } from '@rocket.chat/network-broker';
import type { Document } from 'mongodb';
import polka from 'polka';

import { registerServiceModels } from '../../../../apps/meteor/ee/server/lib/registerServiceModels';
import { Collections, getCollection, getConnection } from '../../../../apps/meteor/ee/server/services/mongo';
import { broker } from '../../../../apps/meteor/ee/server/startup/broker';

const PORT = process.env.PORT || 3038;

Expand Down
3 changes: 3 additions & 0 deletions ee/apps/stream-hub-service/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ COPY ./packages/logger/dist packages/logger/dist

COPY ./packages/server-cloud-communication/ packages/server-cloud-communication/

COPY ./ee/packages/network-broker/package.json ee/packages/network-broker/package.json
COPY ./ee/packages/network-broker/dist ee/packages/network-broker/dist

COPY ./ee/packages/license/package.json packages/license/package.json
COPY ./ee/packages/license/dist packages/license/dist

Expand Down
1 change: 1 addition & 0 deletions ee/apps/stream-hub-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"@rocket.chat/logger": "workspace:^",
"@rocket.chat/model-typings": "workspace:^",
"@rocket.chat/models": "workspace:^",
"@rocket.chat/network-broker": "workspace:^",
"@rocket.chat/string-helpers": "~0.31.25",
"@types/node": "^14.18.63",
"ejson": "^2.2.3",
Expand Down
2 changes: 1 addition & 1 deletion ee/apps/stream-hub-service/src/service.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { api } from '@rocket.chat/core-services';
import { Logger } from '@rocket.chat/logger';
import { broker } from '@rocket.chat/network-broker';
import type { Document } from 'mongodb';
import polka from 'polka';

import { registerServiceModels } from '../../../../apps/meteor/ee/server/lib/registerServiceModels';
import { Collections, getCollection, getConnection } from '../../../../apps/meteor/ee/server/services/mongo';
import { broker } from '../../../../apps/meteor/ee/server/startup/broker';
import { DatabaseWatcher } from '../../../../apps/meteor/server/database/DatabaseWatcher';
import { StreamHub } from './StreamHub';

Expand Down
4 changes: 4 additions & 0 deletions ee/packages/network-broker/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": ["@rocket.chat/eslint-config"],
"ignorePatterns": ["**/dist"]
}
6 changes: 6 additions & 0 deletions ee/packages/network-broker/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import server from '@rocket.chat/jest-presets/server';
import type { Config } from 'jest';

export default {
preset: server.preset,
} satisfies Config;
39 changes: 39 additions & 0 deletions ee/packages/network-broker/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"name": "@rocket.chat/network-broker",
"version": "0.1.0",
"private": true,
"devDependencies": {
"@rocket.chat/eslint-config": "workspace:^",
"@types/chai": "~4.3.19",
"@types/ejson": "^2.2.2",
"@types/node": "^14.18.63",
"@types/sinon": "^10.0.20",
"chai": "^4.3.10",
"eslint": "~8.45.0",
"jest": "~29.7.0",
"sinon": "^14.0.2",
"typescript": "~5.5.4"
},
"scripts": {
"lint": "eslint src",
"lint:fix": "eslint src --fix",
"test": "jest",
"build": "tsc",
"testunit": "jest",
"typecheck": "tsc --noEmit --skipLibCheck"
},
"main": "./dist/index.js",
"typings": "./dist/index.d.ts",
"files": [
"/dist"
],
"volta": {
"extends": "../../../package.json"
},
"dependencies": {
"@rocket.chat/core-services": "workspace:^",
"ejson": "^2.2.3",
"moleculer": "^0.14.34",
"pino": "^8.15.0"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { ServiceClass } from '@rocket.chat/core-services';
import { expect } from 'chai';
import sinon from 'sinon';

import { BrokerMocked } from '../../../../tests/mocks/server/BrokerMocked';
import { NetworkBroker } from '../../../server/NetworkBroker';
import { BrokerMocked } from '../../../../apps/meteor/tests/mocks/server/BrokerMocked';
import { NetworkBroker } from './NetworkBroker';

class DelayedStopBroker extends BrokerMocked {
async destroyService(name: string) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { asyncLocalStorage } from '@rocket.chat/core-services';
import type { IBroker, IBrokerNode, IServiceMetrics, IServiceClass, EventSignatures } from '@rocket.chat/core-services';
import type { ServiceBroker, Context, ServiceSchema } from 'moleculer';

import { EnterpriseCheck } from './lib/EnterpriseCheck';
import { EnterpriseCheck } from './EnterpriseCheck';

const events: { [k: string]: string } = {
onNodeConnected: '$node.connected',
Expand All @@ -25,7 +25,7 @@ const waitForServicesTimeout = parseInt(WAIT_FOR_SERVICES_TIMEOUT, 10) || 10000;
export class NetworkBroker implements IBroker {
private broker: ServiceBroker;

private started: Promise<void>;
private started: Promise<boolean> = Promise.resolve(false);

metrics: IServiceMetrics;

Expand All @@ -36,7 +36,9 @@ export class NetworkBroker implements IBroker {
}

async call(method: string, data: any): Promise<any> {
await this.started;
if (!(await this.started)) {
return;
}

const context = asyncLocalStorage.getStore();

Expand All @@ -54,7 +56,9 @@ export class NetworkBroker implements IBroker {
}

async waitAndCall(method: string, data: any): Promise<any> {
await this.started;
if (!(await this.started)) {
return;
}

try {
await this.broker.waitForServices(method.split('.')[0], waitForServicesTimeout);
Expand Down Expand Up @@ -182,6 +186,8 @@ export class NetworkBroker implements IBroker {
}

async start(): Promise<void> {
this.started = this.broker.start();
await this.broker.start();

this.started = Promise.resolve(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import EJSON from 'ejson';
import { Errors, Serializers, ServiceBroker } from 'moleculer';
import { pino } from 'pino';

import { NetworkBroker } from '../NetworkBroker';
import { NetworkBroker } from './NetworkBroker';

const {
MS_NAMESPACE = '',
Expand Down
9 changes: 9 additions & 0 deletions ee/packages/network-broker/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../../../tsconfig.base.server.json",
"compilerOptions": {
"declaration": true,
"outDir": "./dist",
"rootDir": "./src",
},
"files": ["./src/index.ts"]
}
Loading

0 comments on commit a6b9152

Please sign in to comment.