Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore: Skip local services changes when shutting down duplicated services #24810

Merged
merged 2 commits into from
Mar 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 41 additions & 35 deletions ee/app/license/server/license.internalService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,41 +32,47 @@ export class LicenseService extends ServiceClassInternal implements ILicense {
* So we list the services and nodes, and if there is more than
* one, we inform the service that it should be disabled.
*/
this.onEvent(
'$services.changed',
debounce(async () => {
if (hasLicense('scalability')) {
return;
}

const services: {
name: string;
nodes: string[];
}[] = await api.call('$node.services');

// Filter only the duplicated services
const duplicated = services.filter((service) => {
return service.name !== '$node' && service.nodes.length > 1;
});

if (!duplicated.length) {
return;
}

const brokers: Record<string, string[]> = Object.fromEntries(
duplicated.map((service) => {
// remove the first node from the list
const [, ...nodes] = service.nodes;
return [service.name, nodes];
}),
);

const duplicatedServicesNames = duplicated.map((service) => service.name);

// send shutdown signal to the duplicated services
api.broadcastToServices(duplicatedServicesNames, 'shutdown', brokers);
}, 1000),
);
const shutdownServices = debounce(async () => {
if (hasLicense('scalability')) {
return;
}

const services: {
name: string;
nodes: string[];
}[] = await api.call('$node.services');

// Filter only the duplicated services
const duplicated = services.filter((service) => {
return service.name !== '$node' && service.nodes.length > 1;
});

if (!duplicated.length) {
return;
}

const brokers: Record<string, string[]> = Object.fromEntries(
duplicated.map((service) => {
// remove the first node from the list
const [, ...nodes] = service.nodes;
return [service.name, nodes];
}),
);

const duplicatedServicesNames = duplicated.map((service) => service.name);

// send shutdown signal to the duplicated services
api.broadcastToServices(duplicatedServicesNames, 'shutdown', brokers);
}, 1000);

this.onEvent('$services.changed', async ({ localService }) => {
// since this is an internal service, we can ignore this event if the service that triggered is a local service.
// this will also prevent race conditions if a license is not in place when this process is starting up.
if (localService) {
return;
}
shutdownServices();
});
}

async started(): Promise<void> {
Expand Down
2 changes: 1 addition & 1 deletion server/sdk/lib/Events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type ClientAction = 'inserted' | 'updated' | 'removed' | 'changed';

export type EventSignatures = {
'shutdown': (params: Record<string, string[]>) => void;
'$services.changed': (info: unknown) => void;
'$services.changed': (info: { localService: boolean }) => void;
'accounts.login': (info: { userId: string; connection: ISocketConnection }) => void;
'accounts.logout': (info: { userId: string; connection: ISocketConnection }) => void;
'authorization.guestPermissions': (permissions: string[]) => void;
Expand Down