Skip to content

Commit

Permalink
Terminate handler registry implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
ardatan committed Mar 14, 2024
1 parent 01dec58 commit e2fb7ed
Show file tree
Hide file tree
Showing 12 changed files with 69 additions and 52 deletions.
5 changes: 5 additions & 0 deletions .changeset/modern-carrots-deny.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@graphql-mesh/utils": patch
---

Register terminate handler utility
9 changes: 9 additions & 0 deletions .changeset/nervous-lions-buy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"@graphql-mesh/plugin-prometheus": patch
"@graphql-mesh/utils": patch
"@graphql-mesh/plugin-hive": patch
"@graphql-mesh/cli": patch
"@graphql-mesh/serve-cli": patch
---

Terminate handler registry
2 changes: 1 addition & 1 deletion packages/legacy/cli/src/commands/serve/serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import { process } from '@graphql-mesh/cross-helpers';
import { createMeshHTTPHandler } from '@graphql-mesh/http';
import { MeshInstance, ServeMeshOptions } from '@graphql-mesh/runtime';
import type { Logger } from '@graphql-mesh/types';
import { registerTerminateHandler } from '@graphql-mesh/utils';
import { handleFatalError } from '../../handleFatalError.js';
import { GraphQLMeshCLIParams } from '../../index.js';
import { registerTerminateHandler } from '../../terminateHandler.js';

function portSelectorFn(sources: [number, number, number], logger: Logger) {
const port = sources.find(source => Boolean(source)) || 4000;
Expand Down
10 changes: 8 additions & 2 deletions packages/legacy/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,19 @@ import { fs, path as pathModule, process } from '@graphql-mesh/cross-helpers';
import { getMesh, GetMeshOptions, MeshInstance, ServeMeshOptions } from '@graphql-mesh/runtime';
import { FsStoreStorageAdapter, MeshStore } from '@graphql-mesh/store';
import { Logger, YamlConfig } from '@graphql-mesh/types';
import { defaultImportFn, DefaultLogger, pathExists, rmdirs, writeFile } from '@graphql-mesh/utils';
import {
defaultImportFn,
DefaultLogger,
pathExists,
registerTerminateHandler,
rmdirs,
writeFile,
} from '@graphql-mesh/utils';
import { printSchemaWithDirectives } from '@graphql-tools/utils';
import { serveMesh } from './commands/serve/serve.js';
import { generateTsArtifacts } from './commands/ts-artifacts.js';
import { findAndParseConfig } from './config.js';
import { handleFatalError } from './handleFatalError.js';
import { registerTerminateHandler } from './terminateHandler.js';

export { generateTsArtifacts, serveMesh, findAndParseConfig, handleFatalError };

Expand Down
19 changes: 0 additions & 19 deletions packages/legacy/cli/src/terminateHandler.ts

This file was deleted.

1 change: 1 addition & 0 deletions packages/legacy/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ export * from './sanitize-name-for-graphql.js';
export * from './with-cancel.js';
export * from './with-cookies.js';
export * from './wrapFetchWithHooks.js';
export * from './registerTerminateHandler.js';
30 changes: 30 additions & 0 deletions packages/legacy/utils/src/registerTerminateHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const terminateEvents = ['SIGINT', 'SIGTERM'] as const;

type TerminateEvents = (typeof terminateEvents)[number];
type TerminateHandler = (eventName: TerminateEvents) => void;

let eventListenerRegistered = false;

const terminateHandlers = new Set<TerminateHandler>();

function registerEventListener() {
for (const eventName of terminateEvents) {
globalThis.process?.once(eventName, () => {
for (const handler of terminateHandlers) {
handler(eventName);
terminateHandlers.delete(handler);
}
});
}
}

export function registerTerminateHandler(callback: TerminateHandler) {
if (!eventListenerRegistered) {
registerEventListener();
eventListenerRegistered = true;
}
terminateHandlers.add(callback);
return () => {
terminateHandlers.delete(callback);
};
}
6 changes: 2 additions & 4 deletions packages/plugins/hive/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,8 @@
},
"dependencies": {
"@graphql-hive/client": "^0.28.0",
"@graphql-mesh/string-interpolation": "0.5.3"
},
"devDependencies": {
"@types/http-cache-semantics": "4.0.4"
"@graphql-mesh/string-interpolation": "0.5.3",
"@graphql-mesh/utils": "^0.97.3"
},
"publishConfig": {
"access": "public",
Expand Down
15 changes: 10 additions & 5 deletions packages/plugins/hive/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { createHive, HivePluginOptions, useYogaHive } from '@graphql-hive/client
import { process } from '@graphql-mesh/cross-helpers';
import { stringInterpolator } from '@graphql-mesh/string-interpolation';
import { MeshPlugin, MeshPluginOptions, YamlConfig } from '@graphql-mesh/types';
import { registerTerminateHandler } from '@graphql-mesh/utils';

export default function useMeshHive(
pluginOptions: MeshPluginOptions<YamlConfig.HivePlugin>,
Expand Down Expand Up @@ -95,12 +96,16 @@ export default function useMeshHive(
reporting,
selfHosting,
});
const id = pluginOptions.pubsub.subscribe('destroy', () => {
hiveClient
function onTerminate() {
return hiveClient
.dispose()
.catch(e => pluginOptions.logger.error(`Hive client failed to dispose`, e))
.finally(() => pluginOptions.pubsub.unsubscribe(id));
});
.catch(e => pluginOptions.logger.error(`Hive client failed to dispose`, e));
}
const id: number = pluginOptions.pubsub.subscribe('destroy', () =>
onTerminate().finally(() => pluginOptions.pubsub.unsubscribe(id)),
);
registerTerminateHandler(onTerminate);

return {
onPluginInit({ addPlugin }) {
addPlugin(useYogaHive(hiveClient));
Expand Down
1 change: 1 addition & 0 deletions packages/plugins/prometheus/tests/prometheus.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable import/no-extraneous-dependencies */
import { createSchema } from 'graphql-yoga';
import { register as registry } from 'prom-client';
import { composeSubgraphs } from '@graphql-mesh/fusion-composition';
Expand Down
22 changes: 1 addition & 21 deletions packages/serve-cli/src/runServeCLI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { dirname, isAbsolute, join, relative } from 'path';
import { App, SSLApp } from 'uWebSockets.js';
import { createServeRuntime, UnifiedGraphConfig } from '@graphql-mesh/serve-runtime';
// eslint-disable-next-line import/no-extraneous-dependencies
import { DefaultLogger } from '@graphql-mesh/utils';
import { DefaultLogger, registerTerminateHandler } from '@graphql-mesh/utils';
import { GitLoader } from '@graphql-tools/git-loader';
import { GithubLoader } from '@graphql-tools/github-loader';
import { GraphQLFileLoader } from '@graphql-tools/graphql-file-loader';
Expand Down Expand Up @@ -196,23 +196,3 @@ export async function runServeCLI({
}
});
}

const terminateEvents = ['SIGINT', 'SIGTERM'] as const;
type TerminateEvents = (typeof terminateEvents)[number];
type TerminateHandler = (eventName: TerminateEvents) => void;
const terminateHandlers = new Set<TerminateHandler>();
for (const eventName of terminateEvents) {
process.once(eventName, () => {
for (const handler of terminateHandlers) {
handler(eventName);
terminateHandlers.delete(handler);
}
});
}

function registerTerminateHandler(callback: TerminateHandler) {
terminateHandlers.add(callback);
return () => {
terminateHandlers.delete(callback);
};
}
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5161,6 +5161,7 @@ __metadata:
dependencies:
"@graphql-hive/client": "npm:^0.28.0"
"@graphql-mesh/string-interpolation": "npm:0.5.3"
"@graphql-mesh/utils": "npm:^0.97.3"
"@types/http-cache-semantics": "npm:4.0.4"
peerDependencies:
"@graphql-mesh/cross-helpers": ^0.4.1
Expand Down

0 comments on commit e2fb7ed

Please sign in to comment.