@hadoku/service-kernel 0.1.174 → 0.1.176

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -65,6 +65,7 @@ export interface GameServerConfig {
65
65
  deadlineMs?: number;
66
66
  requestGraceMs?: number;
67
67
  restartFlushMs?: number;
68
+ drainMs?: number;
68
69
  };
69
70
  /**
70
71
  * A listening socket to serve instead of binding `port`, and the pipe to say so on: the deploy
@@ -102,6 +103,8 @@ export interface GameContext<E extends EventMap, S> {
102
103
  export interface PluginHooks {
103
104
  /** Runs at shutdown, in reverse setup order; what it returns is added to `shutdown-done`. */
104
105
  shutdown?: () => Record<string, unknown> | void;
106
+ /** Awaited once the server has closed, before the process exits: writes still on their way to disk. */
107
+ drain?: () => Promise<void>;
105
108
  /** Registered on the erase route in setup order. */
106
109
  erase?: EraseStep[];
107
110
  /** Runs once the server listens, in setup order, before the `listen` phase. */
@@ -37,8 +37,9 @@
37
37
  * plugin's `shutdown` in REVERSE setup order (the last thing built goes first), then tells
38
38
  * every socket the game's restart event, closes socket.io and the HTTP server, and lets
39
39
  * in-flight requests finish within `requestGraceMs`. It exits on the close or at
40
- * `deadlineMs`, whichever comes first, logging `shutdown-done` with the fields the plugins'
41
- * shutdowns returned.
40
+ * `deadlineMs`, whichever comes first. Before it exits it awaits every plugin's `drain` (what is
41
+ * still on its way to disk), for at most `drainMs`, and logs `shutdown-done` with the fields the
42
+ * plugins' shutdowns returned.
42
43
  *
43
44
  * ## A socket handed over
44
45
  *
@@ -74,7 +75,7 @@ export function handoverFromEnv(env = process.env) {
74
75
  const ready = env.HADOKU_READY_FD;
75
76
  return { listenFd: fd, readyFd: ready === undefined || ready === '' ? null : Number(ready) };
76
77
  }
77
- const DEFAULT_SHUTDOWN = { deadlineMs: 1_300, requestGraceMs: 300, restartFlushMs: 500 };
78
+ const DEFAULT_SHUTDOWN = { deadlineMs: 1_300, requestGraceMs: 300, restartFlushMs: 500, drainMs: 2_000 };
78
79
  export async function createGameServer({ id, config, state, plugins }) {
79
80
  const { log } = config;
80
81
  const stamp = bootClock(log);
@@ -174,6 +175,7 @@ export async function createGameServer({ id, config, state, plugins }) {
174
175
  busy: (read) => void busyReaders.push(read),
175
176
  };
176
177
  const stoppers = [];
178
+ const drains = [];
177
179
  const listeners = [];
178
180
  const runPhase = async (phase) => {
179
181
  for (const plugin of plugins) {
@@ -182,6 +184,8 @@ export async function createGameServer({ id, config, state, plugins }) {
182
184
  const hooks = (await plugin.setup(ctx)) ?? {};
183
185
  if (hooks.shutdown !== undefined)
184
186
  stoppers.push({ name: plugin.name, stop: hooks.shutdown });
187
+ if (hooks.drain !== undefined)
188
+ drains.push(hooks.drain);
185
189
  for (const step of hooks.erase ?? [])
186
190
  accountErase.register(step);
187
191
  if (hooks.onListen !== undefined)
@@ -232,9 +236,17 @@ export async function createGameServer({ id, config, state, plugins }) {
232
236
  if (finished)
233
237
  return;
234
238
  finished = true;
235
- log({ evt: 'shutdown-done', signal, why, ms: Date.now() - startedAt, ...fields });
236
- done(why);
237
- exit(0);
239
+ // Requests finishing in the grace above may still have written; they land before the exit.
240
+ let cap;
241
+ void Promise.race([
242
+ Promise.allSettled(drains.map((drain) => drain())).then(() => 'drained'),
243
+ new Promise((resolve) => (cap = setTimeout(() => resolve('timeout'), timing.drainMs))),
244
+ ]).then((drained) => {
245
+ clearTimeout(cap);
246
+ log({ evt: 'shutdown-done', signal, why, drained, ms: Date.now() - startedAt, ...fields });
247
+ done(why);
248
+ exit(0);
249
+ });
238
250
  };
239
251
  // Referenced on purpose: it holds the process up for the closes below, and ends it if one never returns.
240
252
  const deadline = setTimeout(() => finish('deadline'), timing.deadlineMs);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@hadoku/service-kernel",
3
- "version": "0.1.174",
4
- "gitHead": "87d9aed75c7c074353d8c16d8f224dd84304e87e",
3
+ "version": "0.1.176",
4
+ "gitHead": "f2fd3e93488a55fed9cb4b9c523fcf9ea91b9f84",
5
5
  "type": "module",
6
6
  "license": "MIT",
7
7
  "description": "The server parts any game keeps: edge auth and the tier gate, base path and bundle check, health and the busy probe, lifecycle, the JSON logger, the state directory, telemetry and capacity, per-caller limits, the browser relays, the erase registry, and createGameServer, which boots a game from its config and plugins. Knows no game; the app binds its service id, prefix and env names. MIT; imports only node built-ins, express, socket.io, platform-wire and @wolffm/*.",
@@ -56,7 +56,7 @@
56
56
  }
57
57
  },
58
58
  "dependencies": {
59
- "@wolffm/platform-wire": "npm:@hadoku/platform-wire@0.1.141",
59
+ "@wolffm/platform-wire": "npm:@hadoku/platform-wire@0.1.143",
60
60
  "express": "^5.1.0",
61
61
  "socket.io": "^4.8.3"
62
62
  },