@ricsam/r5d-worker 0.0.18 → 0.0.20

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.
package/dist/cjs/main.cjs CHANGED
@@ -44,6 +44,7 @@ var import_node_crypto = require("node:crypto");
44
44
  var import_node_os = __toESM(require("node:os"), 1);
45
45
  var import_node_child_process = require("node:child_process");
46
46
  var import_git_identity = require("./git-identity.cjs");
47
+ var import_heartbeat = require("./heartbeat.cjs");
47
48
  const import_meta = {};
48
49
  const DEFAULT_BASE_URL = "https://r5d.dev";
49
50
  const WORKER_PACKAGE_NAME = "@ricsam/r5d-worker";
@@ -2170,8 +2171,26 @@ async function startWorker(options) {
2170
2171
  Authorization: `Bearer ${token}`
2171
2172
  }
2172
2173
  });
2174
+ let lastServerHeartbeatAt = null;
2175
+ let heartbeatTimedOut = false;
2176
+ let heartbeatWatchdog;
2177
+ const stopHeartbeatWatchdog = () => {
2178
+ if (heartbeatWatchdog) {
2179
+ clearInterval(heartbeatWatchdog);
2180
+ heartbeatWatchdog = void 0;
2181
+ }
2182
+ };
2173
2183
  ws.addEventListener("open", () => {
2174
2184
  currentWorkerSocket = ws;
2185
+ heartbeatWatchdog = setInterval(() => {
2186
+ if (heartbeatTimedOut || !(0, import_heartbeat.hasWorkerHeartbeatTimedOut)(lastServerHeartbeatAt)) {
2187
+ return;
2188
+ }
2189
+ heartbeatTimedOut = true;
2190
+ stopHeartbeatWatchdog();
2191
+ process.stderr.write("[r5d-worker] server heartbeat timed out; reconnecting\n");
2192
+ ws.terminate();
2193
+ }, import_heartbeat.WORKER_HEARTBEAT_INTERVAL_MS);
2175
2194
  const hello = {
2176
2195
  type: "hello",
2177
2196
  hostInfo: {
@@ -2243,6 +2262,7 @@ async function startWorker(options) {
2243
2262
  return;
2244
2263
  }
2245
2264
  if (message.type === "ping") {
2265
+ lastServerHeartbeatAt = Date.now();
2246
2266
  ws.send(JSON.stringify({ type: "pong" }));
2247
2267
  return;
2248
2268
  }
@@ -2391,6 +2411,7 @@ async function startWorker(options) {
2391
2411
  });
2392
2412
  });
2393
2413
  ws.addEventListener("close", (event) => {
2414
+ stopHeartbeatWatchdog();
2394
2415
  if (currentWorkerSocket === ws) {
2395
2416
  currentWorkerSocket = null;
2396
2417
  }
@@ -1,5 +1,5 @@
1
1
  {
2
2
  "name": "@ricsam/r5d-worker",
3
- "version": "0.0.18",
3
+ "version": "0.0.20",
4
4
  "type": "commonjs"
5
5
  }
package/dist/mjs/main.mjs CHANGED
@@ -5,6 +5,7 @@ import { createHash } from "node:crypto";
5
5
  import os, { hostname } from "node:os";
6
6
  import { spawn as spawnChildProcess } from "node:child_process";
7
7
  import { configureVisibleGitIdentity } from "./git-identity.mjs";
8
+ import { hasWorkerHeartbeatTimedOut, WORKER_HEARTBEAT_INTERVAL_MS } from "./heartbeat.mjs";
8
9
  const DEFAULT_BASE_URL = "https://r5d.dev";
9
10
  const WORKER_PACKAGE_NAME = "@ricsam/r5d-worker";
10
11
  const LABEL_RE = /^[A-Za-z0-9][A-Za-z0-9_.-]{0,62}$/;
@@ -2130,8 +2131,26 @@ async function startWorker(options) {
2130
2131
  Authorization: `Bearer ${token}`
2131
2132
  }
2132
2133
  });
2134
+ let lastServerHeartbeatAt = null;
2135
+ let heartbeatTimedOut = false;
2136
+ let heartbeatWatchdog;
2137
+ const stopHeartbeatWatchdog = () => {
2138
+ if (heartbeatWatchdog) {
2139
+ clearInterval(heartbeatWatchdog);
2140
+ heartbeatWatchdog = void 0;
2141
+ }
2142
+ };
2133
2143
  ws.addEventListener("open", () => {
2134
2144
  currentWorkerSocket = ws;
2145
+ heartbeatWatchdog = setInterval(() => {
2146
+ if (heartbeatTimedOut || !hasWorkerHeartbeatTimedOut(lastServerHeartbeatAt)) {
2147
+ return;
2148
+ }
2149
+ heartbeatTimedOut = true;
2150
+ stopHeartbeatWatchdog();
2151
+ process.stderr.write("[r5d-worker] server heartbeat timed out; reconnecting\n");
2152
+ ws.terminate();
2153
+ }, WORKER_HEARTBEAT_INTERVAL_MS);
2135
2154
  const hello = {
2136
2155
  type: "hello",
2137
2156
  hostInfo: {
@@ -2203,6 +2222,7 @@ async function startWorker(options) {
2203
2222
  return;
2204
2223
  }
2205
2224
  if (message.type === "ping") {
2225
+ lastServerHeartbeatAt = Date.now();
2206
2226
  ws.send(JSON.stringify({ type: "pong" }));
2207
2227
  return;
2208
2228
  }
@@ -2351,6 +2371,7 @@ async function startWorker(options) {
2351
2371
  });
2352
2372
  });
2353
2373
  ws.addEventListener("close", (event) => {
2374
+ stopHeartbeatWatchdog();
2354
2375
  if (currentWorkerSocket === ws) {
2355
2376
  currentWorkerSocket = null;
2356
2377
  }
@@ -1,5 +1,5 @@
1
1
  {
2
2
  "name": "@ricsam/r5d-worker",
3
- "version": "0.0.18",
3
+ "version": "0.0.20",
4
4
  "type": "module"
5
5
  }
@@ -0,0 +1,3 @@
1
+ export declare const WORKER_HEARTBEAT_INTERVAL_MS = 15000;
2
+ export declare const WORKER_HEARTBEAT_TIMEOUT_MS = 45000;
3
+ export declare function hasWorkerHeartbeatTimedOut(lastHeartbeatAt: number | null, now?: number, timeoutMs?: number): boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ricsam/r5d-worker",
3
- "version": "0.0.18",
3
+ "version": "0.0.20",
4
4
  "type": "module",
5
5
  "main": "./dist/cjs/main.cjs",
6
6
  "module": "./dist/mjs/main.mjs",