@milaboratories/pl-deployments 2.18.0 → 3.0.1

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/local/pl.cjs CHANGED
@@ -10,10 +10,9 @@ let upath = require("upath");
10
10
  upath = require_runtime.__toESM(upath);
11
11
  let node_os = require("node:os");
12
12
  node_os = require_runtime.__toESM(node_os);
13
- let node_net = require("node:net");
14
- node_net = require_runtime.__toESM(node_net);
15
13
  let _milaboratories_pl_http = require("@milaboratories/pl-http");
16
14
  let _milaboratories_pl_model_common = require("@milaboratories/pl-model-common");
15
+ let _milaboratories_pl_healthcheck = require("@milaboratories/pl-healthcheck");
17
16
  //#region src/local/pl.ts
18
17
  const LocalConfigYaml = "config-local.yaml";
19
18
  /**
@@ -74,10 +73,59 @@ var LocalPl = class {
74
73
  stopped() {
75
74
  return this.wasStopped;
76
75
  }
77
- async isAlive() {
78
- if (!await require_process.isProcessAlive((0, _milaboratories_ts_helpers.notEmpty)(this.pid))) return false;
79
- if (this.grpcPort === void 0) return true;
80
- return await tcpProbe("127.0.0.1", this.grpcPort);
76
+ /**
77
+ * Polls the OS until the backend process is observed alive.
78
+ * Throws on timeout. Does NOT touch gRPC — use {@link isReady} for that.
79
+ */
80
+ async isAlive(opts) {
81
+ const deadline = Date.now() + (opts?.timeoutMs ?? 12e4);
82
+ const pollMs = opts?.pollIntervalMs ?? 500;
83
+ while (true) {
84
+ if (Date.now() >= deadline) throw new Error("Timeout waiting for backend process to be alive");
85
+ await (0, _milaboratories_ts_helpers.sleep)(pollMs);
86
+ if (!this.pid) continue;
87
+ if (await require_process.isProcessAlive(this.pid)) return;
88
+ }
89
+ }
90
+ /**
91
+ * Polls `grpc.health.v1.Health/Check` until the backend reports SERVING.
92
+ * Throws on timeout, on missing `grpcPort`, or if the process dies mid-wait.
93
+ *
94
+ * Calling this method is an explicit statement that the caller wants a real
95
+ * readiness signal; if `grpcPort` is undefined we throw rather than silently
96
+ * degrade to a process-only check (use {@link isAlive} for that).
97
+ */
98
+ async isReady(opts) {
99
+ const grpcPort = opts?.grpcPort ?? this.grpcPort;
100
+ if (grpcPort === void 0) throw new Error("isReady requires grpcPort to be configured — caller asked for a real readiness check but no gRPC port is available");
101
+ const deadline = Date.now() + (opts?.timeoutMs ?? 12e4);
102
+ const pollMs = opts?.pollIntervalMs ?? 500;
103
+ const requestTimeoutMs = Math.max(500, Math.min(pollMs, 2e3));
104
+ let processAlive = false;
105
+ let wasAlive = false;
106
+ let isFirstCheck = true;
107
+ while (true) {
108
+ if (Date.now() >= deadline) {
109
+ if (!processAlive) throw new Error("Timeout waiting for backend process to be alive");
110
+ throw new Error("Timeout waiting for backend to be ready");
111
+ }
112
+ if (!isFirstCheck) await (0, _milaboratories_ts_helpers.sleep)(pollMs);
113
+ isFirstCheck = false;
114
+ if (!this.pid) continue;
115
+ processAlive = await require_process.isProcessAlive(this.pid);
116
+ wasAlive = wasAlive || processAlive;
117
+ if (!processAlive) {
118
+ if (wasAlive) throw new Error("Backend process died while waiting for readiness");
119
+ continue;
120
+ }
121
+ try {
122
+ if (await (0, _milaboratories_pl_healthcheck.plHealthCheck)({
123
+ address: `127.0.0.1:${grpcPort}`,
124
+ defaultRequestTimeout: requestTimeoutMs,
125
+ service: "GRPC"
126
+ }) === _milaboratories_pl_healthcheck.ServingStatus.SERVING) return;
127
+ } catch {}
128
+ }
81
129
  }
82
130
  debugInfo() {
83
131
  return {
@@ -182,30 +230,6 @@ function mergeDefaultOps(ops, numCpu) {
182
230
  ...withoutSpawnOps
183
231
  };
184
232
  }
185
- /** Resolves true if a TCP connect to host:port succeeds within timeoutMs. Used by
186
- * {@link LocalPl.isAlive} to verify the gRPC listener has actually bound — the backend
187
- * process can be alive long before its server sockets are accepting connections. */
188
- async function tcpProbe(host, port, timeoutMs = 500) {
189
- return await new Promise((resolve) => {
190
- const socket = node_net.default.connect({
191
- host,
192
- port
193
- });
194
- const finalize = (ok) => {
195
- socket.destroy();
196
- resolve(ok);
197
- };
198
- const timer = setTimeout(() => finalize(false), timeoutMs);
199
- socket.once("connect", () => {
200
- clearTimeout(timer);
201
- finalize(true);
202
- });
203
- socket.once("error", () => {
204
- clearTimeout(timer);
205
- finalize(false);
206
- });
207
- });
208
- }
209
233
  /** Gets default options for a platforma local binary
210
234
  * and overrides them with the provided options. */
211
235
  function plProcessOps(binaryPath, configPath, ops, workDir, defaultEnv) {
@@ -1 +1 @@
1
- {"version":3,"file":"pl.cjs","names":["withTrace","processRun","filePid","writePid","processWaitStopped","isProcessAlive","os","fsp","resolveLocalPlBinaryPath","readPid","processStop","newDefaultPlBinarySource","net"],"sources":["../../src/local/pl.ts"],"sourcesContent":["import type { ProcessOptions } from \"./process\";\nimport { isProcessAlive, processStop, processWaitStopped, processRun } from \"./process\";\nimport type { PlBinarySource } from \"../common/pl_binary\";\nimport { newDefaultPlBinarySource, resolveLocalPlBinaryPath } from \"../common/pl_binary\";\nimport type { MiLogger } from \"@milaboratories/ts-helpers\";\nimport { notEmpty } from \"@milaboratories/ts-helpers\";\nimport type { ChildProcess, SpawnOptions } from \"node:child_process\";\nimport { filePid, readPid, writePid } from \"./pid\";\nimport type { Trace } from \"./trace\";\nimport { withTrace } from \"./trace\";\nimport upath from \"upath\";\nimport fsp from \"node:fs/promises\";\nimport net from \"node:net\";\nimport type { Required } from \"utility-types\";\nimport * as os from \"node:os\";\nimport type { ProxySettings } from \"@milaboratories/pl-http\";\nimport { defaultHttpDispatcher } from \"@milaboratories/pl-http\";\nimport { parseHttpAuth } from \"@milaboratories/pl-model-common\";\n\nexport const LocalConfigYaml = \"config-local.yaml\";\n\n/**\n * Represents a local running pl-core,\n * and has methods to start, check if it's running, stop and wait for stopping it.\n * Also, a hook on pl-core closed can be provided.\n */\nexport class LocalPl {\n private instance?: ChildProcess;\n public pid?: number;\n private nRuns: number = 0;\n private lastRunHistory: Trace = {};\n private wasStopped = false;\n\n constructor(\n private readonly logger: MiLogger,\n private readonly workingDir: string,\n private readonly startOptions: ProcessOptions,\n private readonly initialStartHistory: Trace,\n private readonly grpcPort?: number,\n private readonly onClose?: (pl: LocalPl) => Promise<void>,\n private readonly onError?: (pl: LocalPl) => Promise<void>,\n private readonly onCloseAndError?: (pl: LocalPl) => Promise<void>,\n private readonly onCloseAndErrorNoStop?: (pl: LocalPl) => Promise<void>,\n ) {}\n\n async start() {\n await withTrace(this.logger, async (trace, t) => {\n this.wasStopped = false;\n const instance = processRun(this.logger, this.startOptions);\n instance.on(\"error\", (e: any) => {\n this.logger.error(\n `error '${e}', while running platforma, started opts: ${JSON.stringify(this.debugInfo())}`,\n );\n\n // keep in mind there are no awaits here, it will be asynchronous\n if (this.onError !== undefined) void this.onError(this);\n if (this.onCloseAndError !== undefined) void this.onCloseAndError(this);\n if (this.onCloseAndErrorNoStop !== undefined && !this.wasStopped)\n void this.onCloseAndErrorNoStop(this);\n });\n instance.on(\"close\", () => {\n this.logger.warn(`platforma was closed, started opts: ${JSON.stringify(this.debugInfo())}`);\n\n // keep in mind there are no awaits here, it will be asynchronous\n if (this.onClose !== undefined) void this.onClose(this);\n if (this.onCloseAndError !== undefined) void this.onCloseAndError(this);\n if (this.onCloseAndErrorNoStop !== undefined && !this.wasStopped)\n void this.onCloseAndErrorNoStop(this);\n });\n\n trace(\"started\", true);\n\n const pidFile = trace(\"pidFile\", filePid(this.workingDir));\n trace(\"pid\", notEmpty(instance.pid));\n trace(\"pidWritten\", await writePid(pidFile, notEmpty(instance.pid)));\n\n this.nRuns++;\n this.instance = instance;\n this.pid = instance.pid;\n this.lastRunHistory = t;\n });\n }\n\n stop() {\n // TODO use this.instance to stop the process\n this.wasStopped = true;\n processStop(notEmpty(this.pid));\n }\n\n async waitStopped() {\n await processWaitStopped(notEmpty(this.pid), 15000);\n }\n\n stopped() {\n return this.wasStopped;\n }\n\n async isAlive(): Promise<boolean> {\n if (!(await isProcessAlive(notEmpty(this.pid)))) return false;\n if (this.grpcPort === undefined) return true;\n return await tcpProbe(\"127.0.0.1\", this.grpcPort);\n }\n\n debugInfo() {\n return {\n lastRunHistory: this.lastRunHistory,\n nRuns: this.nRuns,\n pid: this.pid,\n workingDir: this.workingDir,\n initialStartHistory: this.initialStartHistory,\n wasStopped: this.wasStopped,\n };\n }\n}\n\n/** Options to start a local pl-core. */\nexport type LocalPlOptions = {\n /** From what directory start a process. */\n readonly workingDir: string;\n /** A string representation of yaml config. */\n readonly config: string;\n /** How to get a binary, download it or get an existing one (default: download latest version) */\n readonly plBinary?: PlBinarySource;\n /** Additional options for a process, environments, stdout, stderr etc. */\n readonly spawnOptions?: SpawnOptions;\n /**\n * If the previous pl-core was started from the same directory,\n * we can check if it's still running and then stop it before starting a new one.\n * (default: true)\n */\n readonly closeOld?: boolean;\n /**\n * Proxy settings to use to fetch the binary and pass it down\n * as a HTTPS_PROXY/HTTP_PROXY environment variable;\n * Backend only supports Basic auth.\n */\n readonly proxy?: ProxySettings;\n\n /**\n * gRPC port the backend listens on (always on 127.0.0.1 for local).\n * When set, {@link LocalPl.isAlive} additionally probes this TCP port so callers\n * can distinguish a process that is alive but still binding from a process that is\n * actually accepting gRPC connections.\n */\n readonly grpcPort?: number;\n\n readonly onClose?: (pl: LocalPl) => Promise<void>;\n readonly onError?: (pl: LocalPl) => Promise<void>;\n readonly onCloseAndError?: (pl: LocalPl) => Promise<void>;\n readonly onCloseAndErrorNoStop?: (pl: LocalPl) => Promise<void>;\n};\n\nexport type LocalPlOptionsFull = Required<LocalPlOptions, \"plBinary\" | \"spawnOptions\" | \"closeOld\">;\n\n/**\n * Starts pl-core, if the option was provided downloads a binary, reads license environments etc.\n */\nexport async function localPlatformaInit(logger: MiLogger, _ops: LocalPlOptions): Promise<LocalPl> {\n // filling-in default values\n\n // Backend could consume a lot of CPU power,\n // we want to keep at least a couple for UI and other apps to work.\n const numCpu = Math.max(os.cpus().length - 2, 1);\n const ops = mergeDefaultOps(_ops, numCpu);\n\n return await withTrace(logger, async (trace, t) => {\n trace(\"startOptions\", { ...ops, config: \"too wordy\" });\n\n const workDir = upath.resolve(ops.workingDir);\n\n if (ops.closeOld) {\n trace(\"closeOld\", await localPlatformaReadPidAndStop(logger, workDir));\n }\n\n const configPath = upath.join(workDir, LocalConfigYaml);\n\n logger.info(`writing configuration '${configPath}'...`);\n await fsp.writeFile(configPath, ops.config);\n\n const plBinPath = upath.join(workDir, \"binaries\");\n const baseBinaryPath = await resolveLocalPlBinaryPath({\n logger,\n downloadDir: plBinPath,\n src: ops.plBinary,\n dispatcher: defaultHttpDispatcher(ops.proxy),\n });\n const binaryPath = trace(\"binaryPath\", upath.join(\"binaries\", baseBinaryPath));\n\n const env = { ...process.env };\n\n if (ops.proxy?.url) {\n const url = new URL(ops.proxy.url);\n if (ops.proxy.auth) {\n const parsed = parseHttpAuth(ops.proxy.auth);\n if (parsed.scheme !== \"Basic\") {\n throw new Error(`\\\nUnsupported auth scheme: ${parsed.scheme}. \\\nOnly Basic auth is supported by the backend.`);\n }\n url.username = parsed.username;\n url.password = parsed.password;\n }\n env.http_proxy = url.toString();\n env.https_proxy = url.toString();\n }\n\n const processOpts = plProcessOps(binaryPath, configPath, ops, workDir, env);\n trace(\"processOpts\", {\n cmd: processOpts.cmd,\n args: processOpts.args,\n cwd: processOpts.opts.cwd,\n });\n\n const pl = new LocalPl(\n logger,\n ops.workingDir,\n processOpts,\n t,\n ops.grpcPort,\n ops.onClose,\n ops.onError,\n ops.onCloseAndError,\n ops.onCloseAndErrorNoStop,\n );\n await pl.start();\n\n return pl;\n });\n}\n\n/** Reads a pid of the old pl-core if it was started in the same working directory,\n * and closes it. */\nasync function localPlatformaReadPidAndStop(\n logger: MiLogger,\n workingDir: string,\n): Promise<Record<string, any>> {\n return await withTrace(logger, async (trace, t) => {\n const file = trace(\"pidFilePath\", filePid(workingDir));\n\n const oldPid = trace(\"pid\", await readPid(file));\n const alive = trace(\"wasAlive\", await isProcessAlive(oldPid));\n\n if (oldPid !== undefined && alive) {\n trace(\"stopped\", processStop(oldPid));\n try {\n trace(\"waitStopped\", await processWaitStopped(oldPid, 15_000)); // larger, that 10s we provide to backend in config.\n } catch {\n trace(\"forceStopped\", processStop(oldPid, true));\n trace(\"waitForceStopped\", await processWaitStopped(oldPid, 5_000));\n }\n }\n\n return t;\n });\n}\n\n/** Gets default options for the whole init process\n * and overrides them with the provided options. */\nexport function mergeDefaultOps(ops: LocalPlOptions, numCpu: number): LocalPlOptionsFull {\n const result: {\n plBinary: PlBinarySource;\n spawnOptions: SpawnOptions;\n closeOld: boolean;\n } = {\n plBinary: newDefaultPlBinarySource(),\n spawnOptions: {\n env: {\n GOMAXPROCS: String(numCpu),\n },\n },\n closeOld: true,\n };\n\n if (ops.spawnOptions?.env) {\n result.spawnOptions.env = { ...result.spawnOptions.env, ...ops.spawnOptions.env };\n }\n\n if (ops.spawnOptions) {\n const withoutEnv = { ...ops.spawnOptions };\n delete withoutEnv[\"env\"];\n result.spawnOptions = { ...result.spawnOptions, ...withoutEnv };\n }\n\n const withoutSpawnOps = { ...ops };\n delete withoutSpawnOps[\"spawnOptions\"];\n\n return { ...result, ...withoutSpawnOps };\n}\n\n/** Resolves true if a TCP connect to host:port succeeds within timeoutMs. Used by\n * {@link LocalPl.isAlive} to verify the gRPC listener has actually bound — the backend\n * process can be alive long before its server sockets are accepting connections. */\nasync function tcpProbe(host: string, port: number, timeoutMs = 500): Promise<boolean> {\n return await new Promise<boolean>((resolve) => {\n const socket = net.connect({ host, port });\n const finalize = (ok: boolean) => {\n socket.destroy();\n resolve(ok);\n };\n const timer = setTimeout(() => finalize(false), timeoutMs);\n socket.once(\"connect\", () => {\n clearTimeout(timer);\n finalize(true);\n });\n socket.once(\"error\", () => {\n clearTimeout(timer);\n finalize(false);\n });\n });\n}\n\n/** Gets default options for a platforma local binary\n * and overrides them with the provided options. */\nexport function plProcessOps(\n binaryPath: any,\n configPath: string,\n ops: LocalPlOptionsFull,\n workDir: string,\n defaultEnv: Record<string, string | undefined>,\n): ProcessOptions {\n const result: ProcessOptions = {\n cmd: binaryPath,\n args: [\"--config\", configPath],\n opts: {\n env: { ...defaultEnv },\n cwd: workDir,\n stdio: [\"pipe\", \"ignore\", \"inherit\"],\n windowsHide: true, // hide a terminal on Windows\n },\n };\n\n if (ops.spawnOptions?.env) {\n result.opts.env = { ...result.opts.env, ...ops.spawnOptions.env };\n }\n\n const withoutEnv = { ...ops.spawnOptions };\n delete withoutEnv[\"env\"];\n result.opts = { ...result.opts, ...withoutEnv };\n\n return result;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAmBA,MAAa,kBAAkB;;;;;;AAO/B,IAAa,UAAb,MAAqB;CACnB;CACA;CACA,QAAwB;CACxB,iBAAgC,EAAE;CAClC,aAAqB;CAErB,YACE,QACA,YACA,cACA,qBACA,UACA,SACA,SACA,iBACA,uBACA;AATiB,OAAA,SAAA;AACA,OAAA,aAAA;AACA,OAAA,eAAA;AACA,OAAA,sBAAA;AACA,OAAA,WAAA;AACA,OAAA,UAAA;AACA,OAAA,UAAA;AACA,OAAA,kBAAA;AACA,OAAA,wBAAA;;CAGnB,MAAM,QAAQ;AACZ,QAAMA,cAAAA,UAAU,KAAK,QAAQ,OAAO,OAAO,MAAM;AAC/C,QAAK,aAAa;GAClB,MAAM,WAAWC,gBAAAA,WAAW,KAAK,QAAQ,KAAK,aAAa;AAC3D,YAAS,GAAG,UAAU,MAAW;AAC/B,SAAK,OAAO,MACV,UAAU,EAAE,4CAA4C,KAAK,UAAU,KAAK,WAAW,CAAC,GACzF;AAGD,QAAI,KAAK,YAAY,KAAA,EAAgB,MAAK,QAAQ,KAAK;AACvD,QAAI,KAAK,oBAAoB,KAAA,EAAgB,MAAK,gBAAgB,KAAK;AACvE,QAAI,KAAK,0BAA0B,KAAA,KAAa,CAAC,KAAK,WAC/C,MAAK,sBAAsB,KAAK;KACvC;AACF,YAAS,GAAG,eAAe;AACzB,SAAK,OAAO,KAAK,uCAAuC,KAAK,UAAU,KAAK,WAAW,CAAC,GAAG;AAG3F,QAAI,KAAK,YAAY,KAAA,EAAgB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;GAAM;GAAM,CAAC;EAC1C,MAAM,YAAY,OAAgB;AAChC,UAAO,SAAS;AAChB,WAAQ,GAAG;;EAEb,MAAM,QAAQ,iBAAiB,SAAS,MAAM,EAAE,UAAU;AAC1D,SAAO,KAAK,iBAAiB;AAC3B,gBAAa,MAAM;AACnB,YAAS,KAAK;IACd;AACF,SAAO,KAAK,eAAe;AACzB,gBAAa,MAAM;AACnB,YAAS,MAAM;IACf;GACF;;;;AAKJ,SAAgB,aACd,YACA,YACA,KACA,SACA,YACgB;CAChB,MAAM,SAAyB;EAC7B,KAAK;EACL,MAAM,CAAC,YAAY,WAAW;EAC9B,MAAM;GACJ,KAAK,EAAE,GAAG,YAAY;GACtB,KAAK;GACL,OAAO;IAAC;IAAQ;IAAU;IAAU;GACpC,aAAa;GACd;EACF;AAED,KAAI,IAAI,cAAc,IACpB,QAAO,KAAK,MAAM;EAAE,GAAG,OAAO,KAAK;EAAK,GAAG,IAAI,aAAa;EAAK;CAGnE,MAAM,aAAa,EAAE,GAAG,IAAI,cAAc;AAC1C,QAAO,WAAW;AAClB,QAAO,OAAO;EAAE,GAAG,OAAO;EAAM,GAAG;EAAY;AAE/C,QAAO"}
1
+ {"version":3,"file":"pl.cjs","names":["withTrace","processRun","filePid","writePid","processWaitStopped","isProcessAlive","ServingStatus","os","fsp","resolveLocalPlBinaryPath","readPid","processStop","newDefaultPlBinarySource"],"sources":["../../src/local/pl.ts"],"sourcesContent":["import type { ProcessOptions } from \"./process\";\nimport { isProcessAlive, processStop, processWaitStopped, processRun } from \"./process\";\nimport type { PlBinarySource } from \"../common/pl_binary\";\nimport { newDefaultPlBinarySource, resolveLocalPlBinaryPath } from \"../common/pl_binary\";\nimport type { MiLogger } from \"@milaboratories/ts-helpers\";\nimport { notEmpty } from \"@milaboratories/ts-helpers\";\nimport type { ChildProcess, SpawnOptions } from \"node:child_process\";\nimport { filePid, readPid, writePid } from \"./pid\";\nimport type { Trace } from \"./trace\";\nimport { withTrace } from \"./trace\";\nimport upath from \"upath\";\nimport fsp from \"node:fs/promises\";\nimport type { Required } from \"utility-types\";\nimport * as os from \"node:os\";\nimport { sleep } from \"@milaboratories/ts-helpers\";\nimport type { ProxySettings } from \"@milaboratories/pl-http\";\nimport { defaultHttpDispatcher } from \"@milaboratories/pl-http\";\nimport { parseHttpAuth } from \"@milaboratories/pl-model-common\";\nimport { plHealthCheck, ServingStatus } from \"@milaboratories/pl-healthcheck\";\n\nexport const LocalConfigYaml = \"config-local.yaml\";\n\n/**\n * Represents a local running pl-core,\n * and has methods to start, check if it's running, stop and wait for stopping it.\n * Also, a hook on pl-core closed can be provided.\n */\nexport class LocalPl {\n private instance?: ChildProcess;\n public pid?: number;\n private nRuns: number = 0;\n private lastRunHistory: Trace = {};\n private wasStopped = false;\n\n constructor(\n private readonly logger: MiLogger,\n private readonly workingDir: string,\n private readonly startOptions: ProcessOptions,\n private readonly initialStartHistory: Trace,\n private readonly grpcPort?: number,\n private readonly onClose?: (pl: LocalPl) => Promise<void>,\n private readonly onError?: (pl: LocalPl) => Promise<void>,\n private readonly onCloseAndError?: (pl: LocalPl) => Promise<void>,\n private readonly onCloseAndErrorNoStop?: (pl: LocalPl) => Promise<void>,\n ) {}\n\n async start() {\n await withTrace(this.logger, async (trace, t) => {\n this.wasStopped = false;\n const instance = processRun(this.logger, this.startOptions);\n instance.on(\"error\", (e: any) => {\n this.logger.error(\n `error '${e}', while running platforma, started opts: ${JSON.stringify(this.debugInfo())}`,\n );\n\n // keep in mind there are no awaits here, it will be asynchronous\n if (this.onError !== undefined) void this.onError(this);\n if (this.onCloseAndError !== undefined) void this.onCloseAndError(this);\n if (this.onCloseAndErrorNoStop !== undefined && !this.wasStopped)\n void this.onCloseAndErrorNoStop(this);\n });\n instance.on(\"close\", () => {\n this.logger.warn(`platforma was closed, started opts: ${JSON.stringify(this.debugInfo())}`);\n\n // keep in mind there are no awaits here, it will be asynchronous\n if (this.onClose !== undefined) void this.onClose(this);\n if (this.onCloseAndError !== undefined) void this.onCloseAndError(this);\n if (this.onCloseAndErrorNoStop !== undefined && !this.wasStopped)\n void this.onCloseAndErrorNoStop(this);\n });\n\n trace(\"started\", true);\n\n const pidFile = trace(\"pidFile\", filePid(this.workingDir));\n trace(\"pid\", notEmpty(instance.pid));\n trace(\"pidWritten\", await writePid(pidFile, notEmpty(instance.pid)));\n\n this.nRuns++;\n this.instance = instance;\n this.pid = instance.pid;\n this.lastRunHistory = t;\n });\n }\n\n stop() {\n // TODO use this.instance to stop the process\n this.wasStopped = true;\n processStop(notEmpty(this.pid));\n }\n\n async waitStopped() {\n await processWaitStopped(notEmpty(this.pid), 15000);\n }\n\n stopped() {\n return this.wasStopped;\n }\n\n /**\n * Polls the OS until the backend process is observed alive.\n * Throws on timeout. Does NOT touch gRPC — use {@link isReady} for that.\n */\n async isAlive(opts?: { timeoutMs?: number; pollIntervalMs?: number }): Promise<void> {\n const deadline = Date.now() + (opts?.timeoutMs ?? 120_000);\n const pollMs = opts?.pollIntervalMs ?? 500;\n\n while (true) {\n if (Date.now() >= deadline) {\n throw new Error(\"Timeout waiting for backend process to be alive\");\n }\n await sleep(pollMs);\n if (!this.pid) continue;\n if (await isProcessAlive(this.pid)) return;\n }\n }\n\n /**\n * Polls `grpc.health.v1.Health/Check` until the backend reports SERVING.\n * Throws on timeout, on missing `grpcPort`, or if the process dies mid-wait.\n *\n * Calling this method is an explicit statement that the caller wants a real\n * readiness signal; if `grpcPort` is undefined we throw rather than silently\n * degrade to a process-only check (use {@link isAlive} for that).\n */\n async isReady(opts?: {\n grpcPort?: number;\n timeoutMs?: number;\n pollIntervalMs?: number;\n }): Promise<void> {\n const grpcPort = opts?.grpcPort ?? this.grpcPort;\n\n if (grpcPort === undefined) {\n throw new Error(\n \"isReady requires grpcPort to be configured — caller asked for a real readiness check but no gRPC port is available\",\n );\n }\n\n const deadline = Date.now() + (opts?.timeoutMs ?? 120_000);\n const pollMs = opts?.pollIntervalMs ?? 500;\n const requestTimeoutMs = Math.max(500, Math.min(pollMs, 2_000));\n\n let processAlive = false;\n let wasAlive = false;\n let isFirstCheck = true;\n\n while (true) {\n if (Date.now() >= deadline) {\n if (!processAlive) {\n throw new Error(\"Timeout waiting for backend process to be alive\");\n }\n throw new Error(\"Timeout waiting for backend to be ready\");\n }\n\n if (!isFirstCheck) await sleep(pollMs);\n isFirstCheck = false;\n\n if (!this.pid) continue;\n\n processAlive = await isProcessAlive(this.pid);\n wasAlive = wasAlive || processAlive;\n if (!processAlive) {\n if (wasAlive) {\n // saw the process alive at least once, now it's gone — definitive failure\n throw new Error(\"Backend process died while waiting for readiness\");\n }\n continue;\n }\n\n try {\n const status = await plHealthCheck({\n address: `127.0.0.1:${grpcPort}`,\n defaultRequestTimeout: requestTimeoutMs,\n service: \"GRPC\",\n });\n if (status === ServingStatus.SERVING) return;\n } catch {\n // not yet serving\n }\n }\n }\n\n debugInfo() {\n return {\n lastRunHistory: this.lastRunHistory,\n nRuns: this.nRuns,\n pid: this.pid,\n workingDir: this.workingDir,\n initialStartHistory: this.initialStartHistory,\n wasStopped: this.wasStopped,\n };\n }\n}\n\n/** Options to start a local pl-core. */\nexport type LocalPlOptions = {\n /** From what directory start a process. */\n readonly workingDir: string;\n /** A string representation of yaml config. */\n readonly config: string;\n /** How to get a binary, download it or get an existing one (default: download latest version) */\n readonly plBinary?: PlBinarySource;\n /** Additional options for a process, environments, stdout, stderr etc. */\n readonly spawnOptions?: SpawnOptions;\n /**\n * If the previous pl-core was started from the same directory,\n * we can check if it's still running and then stop it before starting a new one.\n * (default: true)\n */\n readonly closeOld?: boolean;\n /**\n * Proxy settings to use to fetch the binary and pass it down\n * as a HTTPS_PROXY/HTTP_PROXY environment variable;\n * Backend only supports Basic auth.\n */\n readonly proxy?: ProxySettings;\n\n /**\n * gRPC port the backend listens on (always on 127.0.0.1 for local).\n * Required by {@link LocalPl.isReady}, which polls\n * `grpc.health.v1.Health/Check` against this port to distinguish a process\n * that is alive but still initializing from one actually serving gRPC.\n */\n readonly grpcPort?: number;\n\n readonly onClose?: (pl: LocalPl) => Promise<void>;\n readonly onError?: (pl: LocalPl) => Promise<void>;\n readonly onCloseAndError?: (pl: LocalPl) => Promise<void>;\n readonly onCloseAndErrorNoStop?: (pl: LocalPl) => Promise<void>;\n};\n\nexport type LocalPlOptionsFull = Required<LocalPlOptions, \"plBinary\" | \"spawnOptions\" | \"closeOld\">;\n\n/**\n * Starts pl-core, if the option was provided downloads a binary, reads license environments etc.\n */\nexport async function localPlatformaInit(logger: MiLogger, _ops: LocalPlOptions): Promise<LocalPl> {\n // filling-in default values\n\n // Backend could consume a lot of CPU power,\n // we want to keep at least a couple for UI and other apps to work.\n const numCpu = Math.max(os.cpus().length - 2, 1);\n const ops = mergeDefaultOps(_ops, numCpu);\n\n return await withTrace(logger, async (trace, t) => {\n trace(\"startOptions\", { ...ops, config: \"too wordy\" });\n\n const workDir = upath.resolve(ops.workingDir);\n\n if (ops.closeOld) {\n trace(\"closeOld\", await localPlatformaReadPidAndStop(logger, workDir));\n }\n\n const configPath = upath.join(workDir, LocalConfigYaml);\n\n logger.info(`writing configuration '${configPath}'...`);\n await fsp.writeFile(configPath, ops.config);\n\n const plBinPath = upath.join(workDir, \"binaries\");\n const baseBinaryPath = await resolveLocalPlBinaryPath({\n logger,\n downloadDir: plBinPath,\n src: ops.plBinary,\n dispatcher: defaultHttpDispatcher(ops.proxy),\n });\n const binaryPath = trace(\"binaryPath\", upath.join(\"binaries\", baseBinaryPath));\n\n const env = { ...process.env };\n\n if (ops.proxy?.url) {\n const url = new URL(ops.proxy.url);\n if (ops.proxy.auth) {\n const parsed = parseHttpAuth(ops.proxy.auth);\n if (parsed.scheme !== \"Basic\") {\n throw new Error(`\\\nUnsupported auth scheme: ${parsed.scheme}. \\\nOnly Basic auth is supported by the backend.`);\n }\n url.username = parsed.username;\n url.password = parsed.password;\n }\n env.http_proxy = url.toString();\n env.https_proxy = url.toString();\n }\n\n const processOpts = plProcessOps(binaryPath, configPath, ops, workDir, env);\n trace(\"processOpts\", {\n cmd: processOpts.cmd,\n args: processOpts.args,\n cwd: processOpts.opts.cwd,\n });\n\n const pl = new LocalPl(\n logger,\n ops.workingDir,\n processOpts,\n t,\n ops.grpcPort,\n ops.onClose,\n ops.onError,\n ops.onCloseAndError,\n ops.onCloseAndErrorNoStop,\n );\n await pl.start();\n\n return pl;\n });\n}\n\n/** Reads a pid of the old pl-core if it was started in the same working directory,\n * and closes it. */\nasync function localPlatformaReadPidAndStop(\n logger: MiLogger,\n workingDir: string,\n): Promise<Record<string, any>> {\n return await withTrace(logger, async (trace, t) => {\n const file = trace(\"pidFilePath\", filePid(workingDir));\n\n const oldPid = trace(\"pid\", await readPid(file));\n const alive = trace(\"wasAlive\", await isProcessAlive(oldPid));\n\n if (oldPid !== undefined && alive) {\n trace(\"stopped\", processStop(oldPid));\n try {\n trace(\"waitStopped\", await processWaitStopped(oldPid, 15_000)); // larger, that 10s we provide to backend in config.\n } catch {\n trace(\"forceStopped\", processStop(oldPid, true));\n trace(\"waitForceStopped\", await processWaitStopped(oldPid, 5_000));\n }\n }\n\n return t;\n });\n}\n\n/** Gets default options for the whole init process\n * and overrides them with the provided options. */\nexport function mergeDefaultOps(ops: LocalPlOptions, numCpu: number): LocalPlOptionsFull {\n const result: {\n plBinary: PlBinarySource;\n spawnOptions: SpawnOptions;\n closeOld: boolean;\n } = {\n plBinary: newDefaultPlBinarySource(),\n spawnOptions: {\n env: {\n GOMAXPROCS: String(numCpu),\n },\n },\n closeOld: true,\n };\n\n if (ops.spawnOptions?.env) {\n result.spawnOptions.env = { ...result.spawnOptions.env, ...ops.spawnOptions.env };\n }\n\n if (ops.spawnOptions) {\n const withoutEnv = { ...ops.spawnOptions };\n delete withoutEnv[\"env\"];\n result.spawnOptions = { ...result.spawnOptions, ...withoutEnv };\n }\n\n const withoutSpawnOps = { ...ops };\n delete withoutSpawnOps[\"spawnOptions\"];\n\n return { ...result, ...withoutSpawnOps };\n}\n\n/** Gets default options for a platforma local binary\n * and overrides them with the provided options. */\nexport function plProcessOps(\n binaryPath: any,\n configPath: string,\n ops: LocalPlOptionsFull,\n workDir: string,\n defaultEnv: Record<string, string | undefined>,\n): ProcessOptions {\n const result: ProcessOptions = {\n cmd: binaryPath,\n args: [\"--config\", configPath],\n opts: {\n env: { ...defaultEnv },\n cwd: workDir,\n stdio: [\"pipe\", \"ignore\", \"inherit\"],\n windowsHide: true, // hide a terminal on Windows\n },\n };\n\n if (ops.spawnOptions?.env) {\n result.opts.env = { ...result.opts.env, ...ops.spawnOptions.env };\n }\n\n const withoutEnv = { ...ops.spawnOptions };\n delete withoutEnv[\"env\"];\n result.opts = { ...result.opts, ...withoutEnv };\n\n return result;\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAoBA,MAAa,kBAAkB;;;;;;AAO/B,IAAa,UAAb,MAAqB;CACnB;CACA;CACA,QAAwB;CACxB,iBAAgC,EAAE;CAClC,aAAqB;CAErB,YACE,QACA,YACA,cACA,qBACA,UACA,SACA,SACA,iBACA,uBACA;AATiB,OAAA,SAAA;AACA,OAAA,aAAA;AACA,OAAA,eAAA;AACA,OAAA,sBAAA;AACA,OAAA,WAAA;AACA,OAAA,UAAA;AACA,OAAA,UAAA;AACA,OAAA,kBAAA;AACA,OAAA,wBAAA;;CAGnB,MAAM,QAAQ;AACZ,QAAMA,cAAAA,UAAU,KAAK,QAAQ,OAAO,OAAO,MAAM;AAC/C,QAAK,aAAa;GAClB,MAAM,WAAWC,gBAAAA,WAAW,KAAK,QAAQ,KAAK,aAAa;AAC3D,YAAS,GAAG,UAAU,MAAW;AAC/B,SAAK,OAAO,MACV,UAAU,EAAE,4CAA4C,KAAK,UAAU,KAAK,WAAW,CAAC,GACzF;AAGD,QAAI,KAAK,YAAY,KAAA,EAAgB,MAAK,QAAQ,KAAK;AACvD,QAAI,KAAK,oBAAoB,KAAA,EAAgB,MAAK,gBAAgB,KAAK;AACvE,QAAI,KAAK,0BAA0B,KAAA,KAAa,CAAC,KAAK,WAC/C,MAAK,sBAAsB,KAAK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,MAAM;EAAE,GAAG,OAAO,KAAK;EAAK,GAAG,IAAI,aAAa;EAAK;CAGnE,MAAM,aAAa,EAAE,GAAG,IAAI,cAAc;AAC1C,QAAO,WAAW;AAClB,QAAO,OAAO;EAAE,GAAG,OAAO;EAAM,GAAG;EAAY;AAE/C,QAAO"}
@@ -33,7 +33,27 @@ declare class LocalPl {
33
33
  stop(): void;
34
34
  waitStopped(): Promise<void>;
35
35
  stopped(): boolean;
36
- isAlive(): Promise<boolean>;
36
+ /**
37
+ * Polls the OS until the backend process is observed alive.
38
+ * Throws on timeout. Does NOT touch gRPC — use {@link isReady} for that.
39
+ */
40
+ isAlive(opts?: {
41
+ timeoutMs?: number;
42
+ pollIntervalMs?: number;
43
+ }): Promise<void>;
44
+ /**
45
+ * Polls `grpc.health.v1.Health/Check` until the backend reports SERVING.
46
+ * Throws on timeout, on missing `grpcPort`, or if the process dies mid-wait.
47
+ *
48
+ * Calling this method is an explicit statement that the caller wants a real
49
+ * readiness signal; if `grpcPort` is undefined we throw rather than silently
50
+ * degrade to a process-only check (use {@link isAlive} for that).
51
+ */
52
+ isReady(opts?: {
53
+ grpcPort?: number;
54
+ timeoutMs?: number;
55
+ pollIntervalMs?: number;
56
+ }): Promise<void>;
37
57
  debugInfo(): {
38
58
  lastRunHistory: Trace;
39
59
  nRuns: number;
@@ -63,9 +83,9 @@ type LocalPlOptions = {
63
83
  readonly proxy?: ProxySettings;
64
84
  /**
65
85
  * gRPC port the backend listens on (always on 127.0.0.1 for local).
66
- * When set, {@link LocalPl.isAlive} additionally probes this TCP port so callers
67
- * can distinguish a process that is alive but still binding from a process that is
68
- * actually accepting gRPC connections.
86
+ * Required by {@link LocalPl.isReady}, which polls
87
+ * `grpc.health.v1.Health/Check` against this port to distinguish a process
88
+ * that is alive but still initializing from one actually serving gRPC.
69
89
  */
70
90
  readonly grpcPort?: number;
71
91
  readonly onClose?: (pl: LocalPl) => Promise<void>;
@@ -1 +1 @@
1
- {"version":3,"file":"pl.d.ts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;IACA,cAAA;EAAA,IACE,OAAA;EAqDJ,SAAA,CAAA;;;;;;;;;;KAaU,cAAA;EAzJoC,oDA2JrC,UAAA,UA1JiC;EAAA,SA4JjC,MAAA,UA5J6C;EAAA,SA8J7C,QAAA,GAAW,cAAA,EA7J4B;EAAA,SA+JvC,YAAA,GAAe,YAAA;EA/JoC;;;;;EAAA,SAqKnD,QAAA;EAtHQ;;;;;EAAA,SA4HR,KAAA,GAAQ,aAAA;EAhHsD;;;;;;EAAA,SAwH9D,QAAA;EAAA,SAEA,OAAA,IAAW,EAAA,EAAI,OAAA,KAAY,OAAA;EAAA,SAC3B,OAAA,IAAW,EAAA,EAAI,OAAA,KAAY,OAAA;EAAA,SAC3B,eAAA,IAAmB,EAAA,EAAI,OAAA,KAAY,OAAA;EAAA,SACnC,qBAAA,IAAyB,EAAA,EAAI,OAAA,KAAY,OAAA;AAAA;AAAA,KAGxC,kBAAA,GAAqB,QAAA,CAAS,cAAA;;;;iBAKpB,kBAAA,CAAmB,MAAA,EAAQ,QAAA,EAAU,IAAA,EAAM,cAAA,GAAiB,OAAA,CAAQ,OAAA;;AAzC1F;iBA8IgB,eAAA,CAAgB,GAAA,EAAK,cAAA,EAAgB,MAAA,WAAiB,kBAAA;;;iBAiCtD,YAAA,CACd,UAAA,OACA,UAAA,UACA,GAAA,EAAK,kBAAA,EACL,OAAA,UACA,UAAA,EAAY,MAAA,+BACX,cAAA"}
package/dist/local/pl.js CHANGED
@@ -2,13 +2,13 @@ import { isProcessAlive, processRun, processStop, processWaitStopped } from "./p
2
2
  import { newDefaultPlBinarySource, resolveLocalPlBinaryPath } from "../common/pl_binary.js";
3
3
  import { filePid, readPid, writePid } from "./pid.js";
4
4
  import { withTrace } from "./trace.js";
5
- import { notEmpty } from "@milaboratories/ts-helpers";
5
+ import { notEmpty, sleep } from "@milaboratories/ts-helpers";
6
6
  import fsp from "node:fs/promises";
7
7
  import upath from "upath";
8
8
  import * as os$1 from "node:os";
9
- import net from "node:net";
10
9
  import { defaultHttpDispatcher } from "@milaboratories/pl-http";
11
10
  import { parseHttpAuth } from "@milaboratories/pl-model-common";
11
+ import { ServingStatus, plHealthCheck } from "@milaboratories/pl-healthcheck";
12
12
  //#region src/local/pl.ts
13
13
  const LocalConfigYaml = "config-local.yaml";
14
14
  /**
@@ -69,10 +69,59 @@ var LocalPl = class {
69
69
  stopped() {
70
70
  return this.wasStopped;
71
71
  }
72
- async isAlive() {
73
- if (!await isProcessAlive(notEmpty(this.pid))) return false;
74
- if (this.grpcPort === void 0) return true;
75
- return await tcpProbe("127.0.0.1", this.grpcPort);
72
+ /**
73
+ * Polls the OS until the backend process is observed alive.
74
+ * Throws on timeout. Does NOT touch gRPC — use {@link isReady} for that.
75
+ */
76
+ async isAlive(opts) {
77
+ const deadline = Date.now() + (opts?.timeoutMs ?? 12e4);
78
+ const pollMs = opts?.pollIntervalMs ?? 500;
79
+ while (true) {
80
+ if (Date.now() >= deadline) throw new Error("Timeout waiting for backend process to be alive");
81
+ await sleep(pollMs);
82
+ if (!this.pid) continue;
83
+ if (await isProcessAlive(this.pid)) return;
84
+ }
85
+ }
86
+ /**
87
+ * Polls `grpc.health.v1.Health/Check` until the backend reports SERVING.
88
+ * Throws on timeout, on missing `grpcPort`, or if the process dies mid-wait.
89
+ *
90
+ * Calling this method is an explicit statement that the caller wants a real
91
+ * readiness signal; if `grpcPort` is undefined we throw rather than silently
92
+ * degrade to a process-only check (use {@link isAlive} for that).
93
+ */
94
+ async isReady(opts) {
95
+ const grpcPort = opts?.grpcPort ?? this.grpcPort;
96
+ if (grpcPort === void 0) throw new Error("isReady requires grpcPort to be configured — caller asked for a real readiness check but no gRPC port is available");
97
+ const deadline = Date.now() + (opts?.timeoutMs ?? 12e4);
98
+ const pollMs = opts?.pollIntervalMs ?? 500;
99
+ const requestTimeoutMs = Math.max(500, Math.min(pollMs, 2e3));
100
+ let processAlive = false;
101
+ let wasAlive = false;
102
+ let isFirstCheck = true;
103
+ while (true) {
104
+ if (Date.now() >= deadline) {
105
+ if (!processAlive) throw new Error("Timeout waiting for backend process to be alive");
106
+ throw new Error("Timeout waiting for backend to be ready");
107
+ }
108
+ if (!isFirstCheck) await sleep(pollMs);
109
+ isFirstCheck = false;
110
+ if (!this.pid) continue;
111
+ processAlive = await isProcessAlive(this.pid);
112
+ wasAlive = wasAlive || processAlive;
113
+ if (!processAlive) {
114
+ if (wasAlive) throw new Error("Backend process died while waiting for readiness");
115
+ continue;
116
+ }
117
+ try {
118
+ if (await plHealthCheck({
119
+ address: `127.0.0.1:${grpcPort}`,
120
+ defaultRequestTimeout: requestTimeoutMs,
121
+ service: "GRPC"
122
+ }) === ServingStatus.SERVING) return;
123
+ } catch {}
124
+ }
76
125
  }
77
126
  debugInfo() {
78
127
  return {
@@ -177,30 +226,6 @@ function mergeDefaultOps(ops, numCpu) {
177
226
  ...withoutSpawnOps
178
227
  };
179
228
  }
180
- /** Resolves true if a TCP connect to host:port succeeds within timeoutMs. Used by
181
- * {@link LocalPl.isAlive} to verify the gRPC listener has actually bound — the backend
182
- * process can be alive long before its server sockets are accepting connections. */
183
- async function tcpProbe(host, port, timeoutMs = 500) {
184
- return await new Promise((resolve) => {
185
- const socket = net.connect({
186
- host,
187
- port
188
- });
189
- const finalize = (ok) => {
190
- socket.destroy();
191
- resolve(ok);
192
- };
193
- const timer = setTimeout(() => finalize(false), timeoutMs);
194
- socket.once("connect", () => {
195
- clearTimeout(timer);
196
- finalize(true);
197
- });
198
- socket.once("error", () => {
199
- clearTimeout(timer);
200
- finalize(false);
201
- });
202
- });
203
- }
204
229
  /** Gets default options for a platforma local binary
205
230
  * and overrides them with the provided options. */
206
231
  function plProcessOps(binaryPath, configPath, ops, workDir, defaultEnv) {
@@ -1 +1 @@
1
- {"version":3,"file":"pl.js","names":["os"],"sources":["../../src/local/pl.ts"],"sourcesContent":["import type { ProcessOptions } from \"./process\";\nimport { isProcessAlive, processStop, processWaitStopped, processRun } from \"./process\";\nimport type { PlBinarySource } from \"../common/pl_binary\";\nimport { newDefaultPlBinarySource, resolveLocalPlBinaryPath } from \"../common/pl_binary\";\nimport type { MiLogger } from \"@milaboratories/ts-helpers\";\nimport { notEmpty } from \"@milaboratories/ts-helpers\";\nimport type { ChildProcess, SpawnOptions } from \"node:child_process\";\nimport { filePid, readPid, writePid } from \"./pid\";\nimport type { Trace } from \"./trace\";\nimport { withTrace } from \"./trace\";\nimport upath from \"upath\";\nimport fsp from \"node:fs/promises\";\nimport net from \"node:net\";\nimport type { Required } from \"utility-types\";\nimport * as os from \"node:os\";\nimport type { ProxySettings } from \"@milaboratories/pl-http\";\nimport { defaultHttpDispatcher } from \"@milaboratories/pl-http\";\nimport { parseHttpAuth } from \"@milaboratories/pl-model-common\";\n\nexport const LocalConfigYaml = \"config-local.yaml\";\n\n/**\n * Represents a local running pl-core,\n * and has methods to start, check if it's running, stop and wait for stopping it.\n * Also, a hook on pl-core closed can be provided.\n */\nexport class LocalPl {\n private instance?: ChildProcess;\n public pid?: number;\n private nRuns: number = 0;\n private lastRunHistory: Trace = {};\n private wasStopped = false;\n\n constructor(\n private readonly logger: MiLogger,\n private readonly workingDir: string,\n private readonly startOptions: ProcessOptions,\n private readonly initialStartHistory: Trace,\n private readonly grpcPort?: number,\n private readonly onClose?: (pl: LocalPl) => Promise<void>,\n private readonly onError?: (pl: LocalPl) => Promise<void>,\n private readonly onCloseAndError?: (pl: LocalPl) => Promise<void>,\n private readonly onCloseAndErrorNoStop?: (pl: LocalPl) => Promise<void>,\n ) {}\n\n async start() {\n await withTrace(this.logger, async (trace, t) => {\n this.wasStopped = false;\n const instance = processRun(this.logger, this.startOptions);\n instance.on(\"error\", (e: any) => {\n this.logger.error(\n `error '${e}', while running platforma, started opts: ${JSON.stringify(this.debugInfo())}`,\n );\n\n // keep in mind there are no awaits here, it will be asynchronous\n if (this.onError !== undefined) void this.onError(this);\n if (this.onCloseAndError !== undefined) void this.onCloseAndError(this);\n if (this.onCloseAndErrorNoStop !== undefined && !this.wasStopped)\n void this.onCloseAndErrorNoStop(this);\n });\n instance.on(\"close\", () => {\n this.logger.warn(`platforma was closed, started opts: ${JSON.stringify(this.debugInfo())}`);\n\n // keep in mind there are no awaits here, it will be asynchronous\n if (this.onClose !== undefined) void this.onClose(this);\n if (this.onCloseAndError !== undefined) void this.onCloseAndError(this);\n if (this.onCloseAndErrorNoStop !== undefined && !this.wasStopped)\n void this.onCloseAndErrorNoStop(this);\n });\n\n trace(\"started\", true);\n\n const pidFile = trace(\"pidFile\", filePid(this.workingDir));\n trace(\"pid\", notEmpty(instance.pid));\n trace(\"pidWritten\", await writePid(pidFile, notEmpty(instance.pid)));\n\n this.nRuns++;\n this.instance = instance;\n this.pid = instance.pid;\n this.lastRunHistory = t;\n });\n }\n\n stop() {\n // TODO use this.instance to stop the process\n this.wasStopped = true;\n processStop(notEmpty(this.pid));\n }\n\n async waitStopped() {\n await processWaitStopped(notEmpty(this.pid), 15000);\n }\n\n stopped() {\n return this.wasStopped;\n }\n\n async isAlive(): Promise<boolean> {\n if (!(await isProcessAlive(notEmpty(this.pid)))) return false;\n if (this.grpcPort === undefined) return true;\n return await tcpProbe(\"127.0.0.1\", this.grpcPort);\n }\n\n debugInfo() {\n return {\n lastRunHistory: this.lastRunHistory,\n nRuns: this.nRuns,\n pid: this.pid,\n workingDir: this.workingDir,\n initialStartHistory: this.initialStartHistory,\n wasStopped: this.wasStopped,\n };\n }\n}\n\n/** Options to start a local pl-core. */\nexport type LocalPlOptions = {\n /** From what directory start a process. */\n readonly workingDir: string;\n /** A string representation of yaml config. */\n readonly config: string;\n /** How to get a binary, download it or get an existing one (default: download latest version) */\n readonly plBinary?: PlBinarySource;\n /** Additional options for a process, environments, stdout, stderr etc. */\n readonly spawnOptions?: SpawnOptions;\n /**\n * If the previous pl-core was started from the same directory,\n * we can check if it's still running and then stop it before starting a new one.\n * (default: true)\n */\n readonly closeOld?: boolean;\n /**\n * Proxy settings to use to fetch the binary and pass it down\n * as a HTTPS_PROXY/HTTP_PROXY environment variable;\n * Backend only supports Basic auth.\n */\n readonly proxy?: ProxySettings;\n\n /**\n * gRPC port the backend listens on (always on 127.0.0.1 for local).\n * When set, {@link LocalPl.isAlive} additionally probes this TCP port so callers\n * can distinguish a process that is alive but still binding from a process that is\n * actually accepting gRPC connections.\n */\n readonly grpcPort?: number;\n\n readonly onClose?: (pl: LocalPl) => Promise<void>;\n readonly onError?: (pl: LocalPl) => Promise<void>;\n readonly onCloseAndError?: (pl: LocalPl) => Promise<void>;\n readonly onCloseAndErrorNoStop?: (pl: LocalPl) => Promise<void>;\n};\n\nexport type LocalPlOptionsFull = Required<LocalPlOptions, \"plBinary\" | \"spawnOptions\" | \"closeOld\">;\n\n/**\n * Starts pl-core, if the option was provided downloads a binary, reads license environments etc.\n */\nexport async function localPlatformaInit(logger: MiLogger, _ops: LocalPlOptions): Promise<LocalPl> {\n // filling-in default values\n\n // Backend could consume a lot of CPU power,\n // we want to keep at least a couple for UI and other apps to work.\n const numCpu = Math.max(os.cpus().length - 2, 1);\n const ops = mergeDefaultOps(_ops, numCpu);\n\n return await withTrace(logger, async (trace, t) => {\n trace(\"startOptions\", { ...ops, config: \"too wordy\" });\n\n const workDir = upath.resolve(ops.workingDir);\n\n if (ops.closeOld) {\n trace(\"closeOld\", await localPlatformaReadPidAndStop(logger, workDir));\n }\n\n const configPath = upath.join(workDir, LocalConfigYaml);\n\n logger.info(`writing configuration '${configPath}'...`);\n await fsp.writeFile(configPath, ops.config);\n\n const plBinPath = upath.join(workDir, \"binaries\");\n const baseBinaryPath = await resolveLocalPlBinaryPath({\n logger,\n downloadDir: plBinPath,\n src: ops.plBinary,\n dispatcher: defaultHttpDispatcher(ops.proxy),\n });\n const binaryPath = trace(\"binaryPath\", upath.join(\"binaries\", baseBinaryPath));\n\n const env = { ...process.env };\n\n if (ops.proxy?.url) {\n const url = new URL(ops.proxy.url);\n if (ops.proxy.auth) {\n const parsed = parseHttpAuth(ops.proxy.auth);\n if (parsed.scheme !== \"Basic\") {\n throw new Error(`\\\nUnsupported auth scheme: ${parsed.scheme}. \\\nOnly Basic auth is supported by the backend.`);\n }\n url.username = parsed.username;\n url.password = parsed.password;\n }\n env.http_proxy = url.toString();\n env.https_proxy = url.toString();\n }\n\n const processOpts = plProcessOps(binaryPath, configPath, ops, workDir, env);\n trace(\"processOpts\", {\n cmd: processOpts.cmd,\n args: processOpts.args,\n cwd: processOpts.opts.cwd,\n });\n\n const pl = new LocalPl(\n logger,\n ops.workingDir,\n processOpts,\n t,\n ops.grpcPort,\n ops.onClose,\n ops.onError,\n ops.onCloseAndError,\n ops.onCloseAndErrorNoStop,\n );\n await pl.start();\n\n return pl;\n });\n}\n\n/** Reads a pid of the old pl-core if it was started in the same working directory,\n * and closes it. */\nasync function localPlatformaReadPidAndStop(\n logger: MiLogger,\n workingDir: string,\n): Promise<Record<string, any>> {\n return await withTrace(logger, async (trace, t) => {\n const file = trace(\"pidFilePath\", filePid(workingDir));\n\n const oldPid = trace(\"pid\", await readPid(file));\n const alive = trace(\"wasAlive\", await isProcessAlive(oldPid));\n\n if (oldPid !== undefined && alive) {\n trace(\"stopped\", processStop(oldPid));\n try {\n trace(\"waitStopped\", await processWaitStopped(oldPid, 15_000)); // larger, that 10s we provide to backend in config.\n } catch {\n trace(\"forceStopped\", processStop(oldPid, true));\n trace(\"waitForceStopped\", await processWaitStopped(oldPid, 5_000));\n }\n }\n\n return t;\n });\n}\n\n/** Gets default options for the whole init process\n * and overrides them with the provided options. */\nexport function mergeDefaultOps(ops: LocalPlOptions, numCpu: number): LocalPlOptionsFull {\n const result: {\n plBinary: PlBinarySource;\n spawnOptions: SpawnOptions;\n closeOld: boolean;\n } = {\n plBinary: newDefaultPlBinarySource(),\n spawnOptions: {\n env: {\n GOMAXPROCS: String(numCpu),\n },\n },\n closeOld: true,\n };\n\n if (ops.spawnOptions?.env) {\n result.spawnOptions.env = { ...result.spawnOptions.env, ...ops.spawnOptions.env };\n }\n\n if (ops.spawnOptions) {\n const withoutEnv = { ...ops.spawnOptions };\n delete withoutEnv[\"env\"];\n result.spawnOptions = { ...result.spawnOptions, ...withoutEnv };\n }\n\n const withoutSpawnOps = { ...ops };\n delete withoutSpawnOps[\"spawnOptions\"];\n\n return { ...result, ...withoutSpawnOps };\n}\n\n/** Resolves true if a TCP connect to host:port succeeds within timeoutMs. Used by\n * {@link LocalPl.isAlive} to verify the gRPC listener has actually bound — the backend\n * process can be alive long before its server sockets are accepting connections. */\nasync function tcpProbe(host: string, port: number, timeoutMs = 500): Promise<boolean> {\n return await new Promise<boolean>((resolve) => {\n const socket = net.connect({ host, port });\n const finalize = (ok: boolean) => {\n socket.destroy();\n resolve(ok);\n };\n const timer = setTimeout(() => finalize(false), timeoutMs);\n socket.once(\"connect\", () => {\n clearTimeout(timer);\n finalize(true);\n });\n socket.once(\"error\", () => {\n clearTimeout(timer);\n finalize(false);\n });\n });\n}\n\n/** Gets default options for a platforma local binary\n * and overrides them with the provided options. */\nexport function plProcessOps(\n binaryPath: any,\n configPath: string,\n ops: LocalPlOptionsFull,\n workDir: string,\n defaultEnv: Record<string, string | undefined>,\n): ProcessOptions {\n const result: ProcessOptions = {\n cmd: binaryPath,\n args: [\"--config\", configPath],\n opts: {\n env: { ...defaultEnv },\n cwd: workDir,\n stdio: [\"pipe\", \"ignore\", \"inherit\"],\n windowsHide: true, // hide a terminal on Windows\n },\n };\n\n if (ops.spawnOptions?.env) {\n result.opts.env = { ...result.opts.env, ...ops.spawnOptions.env };\n }\n\n const withoutEnv = { ...ops.spawnOptions };\n delete withoutEnv[\"env\"];\n result.opts = { ...result.opts, ...withoutEnv };\n\n return result;\n}\n"],"mappings":";;;;;;;;;;;;AAmBA,MAAa,kBAAkB;;;;;;AAO/B,IAAa,UAAb,MAAqB;CACnB;CACA;CACA,QAAwB;CACxB,iBAAgC,EAAE;CAClC,aAAqB;CAErB,YACE,QACA,YACA,cACA,qBACA,UACA,SACA,SACA,iBACA,uBACA;AATiB,OAAA,SAAA;AACA,OAAA,aAAA;AACA,OAAA,eAAA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AAG,IAAI,aAAa;EAAK;AAGnF,KAAI,IAAI,cAAc;EACpB,MAAM,aAAa,EAAE,GAAG,IAAI,cAAc;AAC1C,SAAO,WAAW;AAClB,SAAO,eAAe;GAAE,GAAG,OAAO;GAAc,GAAG;GAAY;;CAGjE,MAAM,kBAAkB,EAAE,GAAG,KAAK;AAClC,QAAO,gBAAgB;AAEvB,QAAO;EAAE,GAAG;EAAQ,GAAG;EAAiB;;;;;AAM1C,eAAe,SAAS,MAAc,MAAc,YAAY,KAAuB;AACrF,QAAO,MAAM,IAAI,SAAkB,YAAY;EAC7C,MAAM,SAAS,IAAI,QAAQ;GAAE;GAAM;GAAM,CAAC;EAC1C,MAAM,YAAY,OAAgB;AAChC,UAAO,SAAS;AAChB,WAAQ,GAAG;;EAEb,MAAM,QAAQ,iBAAiB,SAAS,MAAM,EAAE,UAAU;AAC1D,SAAO,KAAK,iBAAiB;AAC3B,gBAAa,MAAM;AACnB,YAAS,KAAK;IACd;AACF,SAAO,KAAK,eAAe;AACzB,gBAAa,MAAM;AACnB,YAAS,MAAM;IACf;GACF;;;;AAKJ,SAAgB,aACd,YACA,YACA,KACA,SACA,YACgB;CAChB,MAAM,SAAyB;EAC7B,KAAK;EACL,MAAM,CAAC,YAAY,WAAW;EAC9B,MAAM;GACJ,KAAK,EAAE,GAAG,YAAY;GACtB,KAAK;GACL,OAAO;IAAC;IAAQ;IAAU;IAAU;GACpC,aAAa;GACd;EACF;AAED,KAAI,IAAI,cAAc,IACpB,QAAO,KAAK,MAAM;EAAE,GAAG,OAAO,KAAK;EAAK,GAAG,IAAI,aAAa;EAAK;CAGnE,MAAM,aAAa,EAAE,GAAG,IAAI,cAAc;AAC1C,QAAO,WAAW;AAClB,QAAO,OAAO;EAAE,GAAG,OAAO;EAAM,GAAG;EAAY;AAE/C,QAAO"}
1
+ {"version":3,"file":"pl.js","names":["os"],"sources":["../../src/local/pl.ts"],"sourcesContent":["import type { ProcessOptions } from \"./process\";\nimport { isProcessAlive, processStop, processWaitStopped, processRun } from \"./process\";\nimport type { PlBinarySource } from \"../common/pl_binary\";\nimport { newDefaultPlBinarySource, resolveLocalPlBinaryPath } from \"../common/pl_binary\";\nimport type { MiLogger } from \"@milaboratories/ts-helpers\";\nimport { notEmpty } from \"@milaboratories/ts-helpers\";\nimport type { ChildProcess, SpawnOptions } from \"node:child_process\";\nimport { filePid, readPid, writePid } from \"./pid\";\nimport type { Trace } from \"./trace\";\nimport { withTrace } from \"./trace\";\nimport upath from \"upath\";\nimport fsp from \"node:fs/promises\";\nimport type { Required } from \"utility-types\";\nimport * as os from \"node:os\";\nimport { sleep } from \"@milaboratories/ts-helpers\";\nimport type { ProxySettings } from \"@milaboratories/pl-http\";\nimport { defaultHttpDispatcher } from \"@milaboratories/pl-http\";\nimport { parseHttpAuth } from \"@milaboratories/pl-model-common\";\nimport { plHealthCheck, ServingStatus } from \"@milaboratories/pl-healthcheck\";\n\nexport const LocalConfigYaml = \"config-local.yaml\";\n\n/**\n * Represents a local running pl-core,\n * and has methods to start, check if it's running, stop and wait for stopping it.\n * Also, a hook on pl-core closed can be provided.\n */\nexport class LocalPl {\n private instance?: ChildProcess;\n public pid?: number;\n private nRuns: number = 0;\n private lastRunHistory: Trace = {};\n private wasStopped = false;\n\n constructor(\n private readonly logger: MiLogger,\n private readonly workingDir: string,\n private readonly startOptions: ProcessOptions,\n private readonly initialStartHistory: Trace,\n private readonly grpcPort?: number,\n private readonly onClose?: (pl: LocalPl) => Promise<void>,\n private readonly onError?: (pl: LocalPl) => Promise<void>,\n private readonly onCloseAndError?: (pl: LocalPl) => Promise<void>,\n private readonly onCloseAndErrorNoStop?: (pl: LocalPl) => Promise<void>,\n ) {}\n\n async start() {\n await withTrace(this.logger, async (trace, t) => {\n this.wasStopped = false;\n const instance = processRun(this.logger, this.startOptions);\n instance.on(\"error\", (e: any) => {\n this.logger.error(\n `error '${e}', while running platforma, started opts: ${JSON.stringify(this.debugInfo())}`,\n );\n\n // keep in mind there are no awaits here, it will be asynchronous\n if (this.onError !== undefined) void this.onError(this);\n if (this.onCloseAndError !== undefined) void this.onCloseAndError(this);\n if (this.onCloseAndErrorNoStop !== undefined && !this.wasStopped)\n void this.onCloseAndErrorNoStop(this);\n });\n instance.on(\"close\", () => {\n this.logger.warn(`platforma was closed, started opts: ${JSON.stringify(this.debugInfo())}`);\n\n // keep in mind there are no awaits here, it will be asynchronous\n if (this.onClose !== undefined) void this.onClose(this);\n if (this.onCloseAndError !== undefined) void this.onCloseAndError(this);\n if (this.onCloseAndErrorNoStop !== undefined && !this.wasStopped)\n void this.onCloseAndErrorNoStop(this);\n });\n\n trace(\"started\", true);\n\n const pidFile = trace(\"pidFile\", filePid(this.workingDir));\n trace(\"pid\", notEmpty(instance.pid));\n trace(\"pidWritten\", await writePid(pidFile, notEmpty(instance.pid)));\n\n this.nRuns++;\n this.instance = instance;\n this.pid = instance.pid;\n this.lastRunHistory = t;\n });\n }\n\n stop() {\n // TODO use this.instance to stop the process\n this.wasStopped = true;\n processStop(notEmpty(this.pid));\n }\n\n async waitStopped() {\n await processWaitStopped(notEmpty(this.pid), 15000);\n }\n\n stopped() {\n return this.wasStopped;\n }\n\n /**\n * Polls the OS until the backend process is observed alive.\n * Throws on timeout. Does NOT touch gRPC — use {@link isReady} for that.\n */\n async isAlive(opts?: { timeoutMs?: number; pollIntervalMs?: number }): Promise<void> {\n const deadline = Date.now() + (opts?.timeoutMs ?? 120_000);\n const pollMs = opts?.pollIntervalMs ?? 500;\n\n while (true) {\n if (Date.now() >= deadline) {\n throw new Error(\"Timeout waiting for backend process to be alive\");\n }\n await sleep(pollMs);\n if (!this.pid) continue;\n if (await isProcessAlive(this.pid)) return;\n }\n }\n\n /**\n * Polls `grpc.health.v1.Health/Check` until the backend reports SERVING.\n * Throws on timeout, on missing `grpcPort`, or if the process dies mid-wait.\n *\n * Calling this method is an explicit statement that the caller wants a real\n * readiness signal; if `grpcPort` is undefined we throw rather than silently\n * degrade to a process-only check (use {@link isAlive} for that).\n */\n async isReady(opts?: {\n grpcPort?: number;\n timeoutMs?: number;\n pollIntervalMs?: number;\n }): Promise<void> {\n const grpcPort = opts?.grpcPort ?? this.grpcPort;\n\n if (grpcPort === undefined) {\n throw new Error(\n \"isReady requires grpcPort to be configured — caller asked for a real readiness check but no gRPC port is available\",\n );\n }\n\n const deadline = Date.now() + (opts?.timeoutMs ?? 120_000);\n const pollMs = opts?.pollIntervalMs ?? 500;\n const requestTimeoutMs = Math.max(500, Math.min(pollMs, 2_000));\n\n let processAlive = false;\n let wasAlive = false;\n let isFirstCheck = true;\n\n while (true) {\n if (Date.now() >= deadline) {\n if (!processAlive) {\n throw new Error(\"Timeout waiting for backend process to be alive\");\n }\n throw new Error(\"Timeout waiting for backend to be ready\");\n }\n\n if (!isFirstCheck) await sleep(pollMs);\n isFirstCheck = false;\n\n if (!this.pid) continue;\n\n processAlive = await isProcessAlive(this.pid);\n wasAlive = wasAlive || processAlive;\n if (!processAlive) {\n if (wasAlive) {\n // saw the process alive at least once, now it's gone — definitive failure\n throw new Error(\"Backend process died while waiting for readiness\");\n }\n continue;\n }\n\n try {\n const status = await plHealthCheck({\n address: `127.0.0.1:${grpcPort}`,\n defaultRequestTimeout: requestTimeoutMs,\n service: \"GRPC\",\n });\n if (status === ServingStatus.SERVING) return;\n } catch {\n // not yet serving\n }\n }\n }\n\n debugInfo() {\n return {\n lastRunHistory: this.lastRunHistory,\n nRuns: this.nRuns,\n pid: this.pid,\n workingDir: this.workingDir,\n initialStartHistory: this.initialStartHistory,\n wasStopped: this.wasStopped,\n };\n }\n}\n\n/** Options to start a local pl-core. */\nexport type LocalPlOptions = {\n /** From what directory start a process. */\n readonly workingDir: string;\n /** A string representation of yaml config. */\n readonly config: string;\n /** How to get a binary, download it or get an existing one (default: download latest version) */\n readonly plBinary?: PlBinarySource;\n /** Additional options for a process, environments, stdout, stderr etc. */\n readonly spawnOptions?: SpawnOptions;\n /**\n * If the previous pl-core was started from the same directory,\n * we can check if it's still running and then stop it before starting a new one.\n * (default: true)\n */\n readonly closeOld?: boolean;\n /**\n * Proxy settings to use to fetch the binary and pass it down\n * as a HTTPS_PROXY/HTTP_PROXY environment variable;\n * Backend only supports Basic auth.\n */\n readonly proxy?: ProxySettings;\n\n /**\n * gRPC port the backend listens on (always on 127.0.0.1 for local).\n * Required by {@link LocalPl.isReady}, which polls\n * `grpc.health.v1.Health/Check` against this port to distinguish a process\n * that is alive but still initializing from one actually serving gRPC.\n */\n readonly grpcPort?: number;\n\n readonly onClose?: (pl: LocalPl) => Promise<void>;\n readonly onError?: (pl: LocalPl) => Promise<void>;\n readonly onCloseAndError?: (pl: LocalPl) => Promise<void>;\n readonly onCloseAndErrorNoStop?: (pl: LocalPl) => Promise<void>;\n};\n\nexport type LocalPlOptionsFull = Required<LocalPlOptions, \"plBinary\" | \"spawnOptions\" | \"closeOld\">;\n\n/**\n * Starts pl-core, if the option was provided downloads a binary, reads license environments etc.\n */\nexport async function localPlatformaInit(logger: MiLogger, _ops: LocalPlOptions): Promise<LocalPl> {\n // filling-in default values\n\n // Backend could consume a lot of CPU power,\n // we want to keep at least a couple for UI and other apps to work.\n const numCpu = Math.max(os.cpus().length - 2, 1);\n const ops = mergeDefaultOps(_ops, numCpu);\n\n return await withTrace(logger, async (trace, t) => {\n trace(\"startOptions\", { ...ops, config: \"too wordy\" });\n\n const workDir = upath.resolve(ops.workingDir);\n\n if (ops.closeOld) {\n trace(\"closeOld\", await localPlatformaReadPidAndStop(logger, workDir));\n }\n\n const configPath = upath.join(workDir, LocalConfigYaml);\n\n logger.info(`writing configuration '${configPath}'...`);\n await fsp.writeFile(configPath, ops.config);\n\n const plBinPath = upath.join(workDir, \"binaries\");\n const baseBinaryPath = await resolveLocalPlBinaryPath({\n logger,\n downloadDir: plBinPath,\n src: ops.plBinary,\n dispatcher: defaultHttpDispatcher(ops.proxy),\n });\n const binaryPath = trace(\"binaryPath\", upath.join(\"binaries\", baseBinaryPath));\n\n const env = { ...process.env };\n\n if (ops.proxy?.url) {\n const url = new URL(ops.proxy.url);\n if (ops.proxy.auth) {\n const parsed = parseHttpAuth(ops.proxy.auth);\n if (parsed.scheme !== \"Basic\") {\n throw new Error(`\\\nUnsupported auth scheme: ${parsed.scheme}. \\\nOnly Basic auth is supported by the backend.`);\n }\n url.username = parsed.username;\n url.password = parsed.password;\n }\n env.http_proxy = url.toString();\n env.https_proxy = url.toString();\n }\n\n const processOpts = plProcessOps(binaryPath, configPath, ops, workDir, env);\n trace(\"processOpts\", {\n cmd: processOpts.cmd,\n args: processOpts.args,\n cwd: processOpts.opts.cwd,\n });\n\n const pl = new LocalPl(\n logger,\n ops.workingDir,\n processOpts,\n t,\n ops.grpcPort,\n ops.onClose,\n ops.onError,\n ops.onCloseAndError,\n ops.onCloseAndErrorNoStop,\n );\n await pl.start();\n\n return pl;\n });\n}\n\n/** Reads a pid of the old pl-core if it was started in the same working directory,\n * and closes it. */\nasync function localPlatformaReadPidAndStop(\n logger: MiLogger,\n workingDir: string,\n): Promise<Record<string, any>> {\n return await withTrace(logger, async (trace, t) => {\n const file = trace(\"pidFilePath\", filePid(workingDir));\n\n const oldPid = trace(\"pid\", await readPid(file));\n const alive = trace(\"wasAlive\", await isProcessAlive(oldPid));\n\n if (oldPid !== undefined && alive) {\n trace(\"stopped\", processStop(oldPid));\n try {\n trace(\"waitStopped\", await processWaitStopped(oldPid, 15_000)); // larger, that 10s we provide to backend in config.\n } catch {\n trace(\"forceStopped\", processStop(oldPid, true));\n trace(\"waitForceStopped\", await processWaitStopped(oldPid, 5_000));\n }\n }\n\n return t;\n });\n}\n\n/** Gets default options for the whole init process\n * and overrides them with the provided options. */\nexport function mergeDefaultOps(ops: LocalPlOptions, numCpu: number): LocalPlOptionsFull {\n const result: {\n plBinary: PlBinarySource;\n spawnOptions: SpawnOptions;\n closeOld: boolean;\n } = {\n plBinary: newDefaultPlBinarySource(),\n spawnOptions: {\n env: {\n GOMAXPROCS: String(numCpu),\n },\n },\n closeOld: true,\n };\n\n if (ops.spawnOptions?.env) {\n result.spawnOptions.env = { ...result.spawnOptions.env, ...ops.spawnOptions.env };\n }\n\n if (ops.spawnOptions) {\n const withoutEnv = { ...ops.spawnOptions };\n delete withoutEnv[\"env\"];\n result.spawnOptions = { ...result.spawnOptions, ...withoutEnv };\n }\n\n const withoutSpawnOps = { ...ops };\n delete withoutSpawnOps[\"spawnOptions\"];\n\n return { ...result, ...withoutSpawnOps };\n}\n\n/** Gets default options for a platforma local binary\n * and overrides them with the provided options. */\nexport function plProcessOps(\n binaryPath: any,\n configPath: string,\n ops: LocalPlOptionsFull,\n workDir: string,\n defaultEnv: Record<string, string | undefined>,\n): ProcessOptions {\n const result: ProcessOptions = {\n cmd: binaryPath,\n args: [\"--config\", configPath],\n opts: {\n env: { ...defaultEnv },\n cwd: workDir,\n stdio: [\"pipe\", \"ignore\", \"inherit\"],\n windowsHide: true, // hide a terminal on Windows\n },\n };\n\n if (ops.spawnOptions?.env) {\n result.opts.env = { ...result.opts.env, ...ops.spawnOptions.env };\n }\n\n const withoutEnv = { ...ops.spawnOptions };\n delete withoutEnv[\"env\"];\n result.opts = { ...result.opts, ...withoutEnv };\n\n return result;\n}\n"],"mappings":";;;;;;;;;;;;AAoBA,MAAa,kBAAkB;;;;;;AAO/B,IAAa,UAAb,MAAqB;CACnB;CACA;CACA,QAAwB;CACxB,iBAAgC,EAAE;CAClC,aAAqB;CAErB,YACE,QACA,YACA,cACA,qBACA,UACA,SACA,SACA,iBACA,uBACA;AATiB,OAAA,SAAA;AACA,OAAA,aAAA;AACA,OAAA,eAAA;AACA,OAAA,sBAAA;AACA,OAAA,WAAA;AACA,OAAA,UAAA;AACA,OAAA,UAAA;AACA,OAAA,kBAAA;AACA,OAAA,wBAAA;;CAGnB,MAAM,QAAQ;AACZ,QAAM,UAAU,KAAK,QAAQ,OAAO,OAAO,MAAM;AAC/C,QAAK,aAAa;GAClB,MAAM,WAAW,WAAW,KAAK,QAAQ,KAAK,aAAa;AAC3D,YAAS,GAAG,UAAU,MAAW;A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EAAK;CAGnE,MAAM,aAAa,EAAE,GAAG,IAAI,cAAc;AAC1C,QAAO,WAAW;AAClB,QAAO,OAAO;EAAE,GAAG,OAAO;EAAM,GAAG;EAAY;AAE/C,QAAO"}
package/dist/ssh/pl.cjs CHANGED
@@ -8,9 +8,9 @@ const require_connection_info = require("./connection_info.cjs");
8
8
  let _milaboratories_ts_helpers = require("@milaboratories/ts-helpers");
9
9
  let upath = require("upath");
10
10
  upath = require_runtime.__toESM(upath);
11
+ let _milaboratories_pl_http = require("@milaboratories/pl-http");
11
12
  let node_net = require("node:net");
12
13
  node_net = require_runtime.__toESM(node_net);
13
- let _milaboratories_pl_http = require("@milaboratories/pl-http");
14
14
  let _milaboratories_pl_config = require("@milaboratories/pl-config");
15
15
  //#region src/ssh/pl.ts
16
16
  const minRequiredGlibcVersion = 2.28;
package/dist/ssh/pl.js CHANGED
@@ -6,8 +6,8 @@ import { generateSupervisordConfig, generateSupervisordConfigWithMinio, isAllAli
6
6
  import { newConnectionInfo, parseConnectionInfo, stringifyConnectionInfo } from "./connection_info.js";
7
7
  import { notEmpty, sleep } from "@milaboratories/ts-helpers";
8
8
  import upath from "upath";
9
- import net from "node:net";
10
9
  import { defaultHttpDispatcher } from "@milaboratories/pl-http";
10
+ import net from "node:net";
11
11
  import { generateSshPlConfigs, getFreePort } from "@milaboratories/pl-config";
12
12
  //#region src/ssh/pl.ts
13
13
  const minRequiredGlibcVersion = 2.28;
package/dist/ssh/ssh.cjs CHANGED
@@ -6,10 +6,10 @@ node_fs = require_runtime.__toESM(node_fs);
6
6
  let node_fs_promises = require("node:fs/promises");
7
7
  let upath = require("upath");
8
8
  upath = require_runtime.__toESM(upath);
9
- let node_net = require("node:net");
10
- node_net = require_runtime.__toESM(node_net);
11
9
  let ssh2 = require("ssh2");
12
10
  ssh2 = require_runtime.__toESM(ssh2);
11
+ let node_net = require("node:net");
12
+ node_net = require_runtime.__toESM(node_net);
13
13
  let node_dns = require("node:dns");
14
14
  node_dns = require_runtime.__toESM(node_dns);
15
15
  let node_crypto = require("node:crypto");
package/dist/ssh/ssh.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { MiLogger } from "@milaboratories/ts-helpers";
2
- import net from "node:net";
3
2
  import ssh, { Client, ConnectConfig, SFTPWrapper } from "ssh2";
3
+ import net from "node:net";
4
4
 
5
5
  //#region src/ssh/ssh.d.ts
6
6
  type SshAuthMethods = "publickey" | "password";
package/dist/ssh/ssh.js CHANGED
@@ -3,8 +3,8 @@ import { Retry3TimesWithDelay, RetryablePromise, retry } from "@milaboratories/t
3
3
  import fs from "node:fs";
4
4
  import { readFile } from "node:fs/promises";
5
5
  import upath from "upath";
6
- import net from "node:net";
7
6
  import ssh, { Client } from "ssh2";
7
+ import net from "node:net";
8
8
  import dns from "node:dns";
9
9
  import { randomBytes } from "node:crypto";
10
10
  //#region src/ssh/ssh.ts
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@milaboratories/pl-deployments",
3
- "version": "2.18.0",
3
+ "version": "3.0.1",
4
4
  "description": "MiLaboratories Platforma Backend code service run wrapper",
5
5
  "keywords": [],
6
6
  "license": "UNLICENSED",
@@ -32,10 +32,11 @@
32
32
  "upath": "^2.0.1",
33
33
  "yaml": "^2.8.0",
34
34
  "zod": "~3.25.76",
35
- "@milaboratories/pl-http": "1.2.4",
35
+ "@milaboratories/pl-config": "1.8.1",
36
+ "@milaboratories/pl-healthcheck": "1.0.0",
36
37
  "@milaboratories/ts-helpers": "1.8.2",
37
38
  "@milaboratories/pl-model-common": "1.42.0",
38
- "@milaboratories/pl-config": "1.8.1"
39
+ "@milaboratories/pl-http": "1.2.4"
39
40
  },
40
41
  "devDependencies": {
41
42
  "@types/decompress": "^4.2.7",
@@ -32,7 +32,7 @@ test(
32
32
 
33
33
  console.log(`Platforma: %o`, pl.debugInfo());
34
34
 
35
- expect(await pl.isAlive()).toBeTruthy();
35
+ await pl.isAlive();
36
36
  expect(pl.pid).not.toBeUndefined();
37
37
 
38
38
  pl.stop();
@@ -57,14 +57,14 @@ test(
57
57
  await sleep(5000);
58
58
  console.log(`OldPlatforma: %o`, oldPl.debugInfo());
59
59
 
60
- expect(await oldPl.isAlive()).toBeTruthy();
60
+ await oldPl.isAlive();
61
61
  const newPl = await localPlatformaInit(logger, options);
62
- expect(await oldPl.isAlive()).toBeFalsy();
62
+ await expect(oldPl.isAlive({ timeoutMs: 1000 })).rejects.toThrow();
63
63
  await sleep(5000);
64
64
 
65
65
  console.log(`NewPlatforma: %o`, newPl.debugInfo());
66
66
 
67
- expect(await newPl.isAlive()).toBeTruthy();
67
+ await newPl.isAlive();
68
68
  expect(newPl.pid).not.toBeUndefined();
69
69
  newPl.stop();
70
70
  await newPl.waitStopped();
@@ -87,17 +87,17 @@ test(
87
87
  });
88
88
  await sleep(1000);
89
89
 
90
- expect(await pl.isAlive()).toBeTruthy();
90
+ await pl.isAlive();
91
91
  processStop(pl.pid!);
92
92
  await sleep(3000);
93
93
  console.log(`Platforma after first stop: %o`, pl.debugInfo());
94
94
 
95
- expect(await pl.isAlive()).toBeTruthy();
95
+ await pl.isAlive();
96
96
  processStop(pl.pid!);
97
97
  await sleep(3000);
98
98
  console.log(`Platforma after second stop: %o`, pl.debugInfo());
99
99
 
100
- expect(await pl.isAlive()).toBeTruthy();
100
+ await pl.isAlive();
101
101
  expect(pl.debugInfo().nRuns).toEqual(3);
102
102
 
103
103
  pl.stop();
package/src/local/pl.ts CHANGED
@@ -10,12 +10,13 @@ import type { Trace } from "./trace";
10
10
  import { withTrace } from "./trace";
11
11
  import upath from "upath";
12
12
  import fsp from "node:fs/promises";
13
- import net from "node:net";
14
13
  import type { Required } from "utility-types";
15
14
  import * as os from "node:os";
15
+ import { sleep } from "@milaboratories/ts-helpers";
16
16
  import type { ProxySettings } from "@milaboratories/pl-http";
17
17
  import { defaultHttpDispatcher } from "@milaboratories/pl-http";
18
18
  import { parseHttpAuth } from "@milaboratories/pl-model-common";
19
+ import { plHealthCheck, ServingStatus } from "@milaboratories/pl-healthcheck";
19
20
 
20
21
  export const LocalConfigYaml = "config-local.yaml";
21
22
 
@@ -95,10 +96,87 @@ export class LocalPl {
95
96
  return this.wasStopped;
96
97
  }
97
98
 
98
- async isAlive(): Promise<boolean> {
99
- if (!(await isProcessAlive(notEmpty(this.pid)))) return false;
100
- if (this.grpcPort === undefined) return true;
101
- return await tcpProbe("127.0.0.1", this.grpcPort);
99
+ /**
100
+ * Polls the OS until the backend process is observed alive.
101
+ * Throws on timeout. Does NOT touch gRPC — use {@link isReady} for that.
102
+ */
103
+ async isAlive(opts?: { timeoutMs?: number; pollIntervalMs?: number }): Promise<void> {
104
+ const deadline = Date.now() + (opts?.timeoutMs ?? 120_000);
105
+ const pollMs = opts?.pollIntervalMs ?? 500;
106
+
107
+ while (true) {
108
+ if (Date.now() >= deadline) {
109
+ throw new Error("Timeout waiting for backend process to be alive");
110
+ }
111
+ await sleep(pollMs);
112
+ if (!this.pid) continue;
113
+ if (await isProcessAlive(this.pid)) return;
114
+ }
115
+ }
116
+
117
+ /**
118
+ * Polls `grpc.health.v1.Health/Check` until the backend reports SERVING.
119
+ * Throws on timeout, on missing `grpcPort`, or if the process dies mid-wait.
120
+ *
121
+ * Calling this method is an explicit statement that the caller wants a real
122
+ * readiness signal; if `grpcPort` is undefined we throw rather than silently
123
+ * degrade to a process-only check (use {@link isAlive} for that).
124
+ */
125
+ async isReady(opts?: {
126
+ grpcPort?: number;
127
+ timeoutMs?: number;
128
+ pollIntervalMs?: number;
129
+ }): Promise<void> {
130
+ const grpcPort = opts?.grpcPort ?? this.grpcPort;
131
+
132
+ if (grpcPort === undefined) {
133
+ throw new Error(
134
+ "isReady requires grpcPort to be configured — caller asked for a real readiness check but no gRPC port is available",
135
+ );
136
+ }
137
+
138
+ const deadline = Date.now() + (opts?.timeoutMs ?? 120_000);
139
+ const pollMs = opts?.pollIntervalMs ?? 500;
140
+ const requestTimeoutMs = Math.max(500, Math.min(pollMs, 2_000));
141
+
142
+ let processAlive = false;
143
+ let wasAlive = false;
144
+ let isFirstCheck = true;
145
+
146
+ while (true) {
147
+ if (Date.now() >= deadline) {
148
+ if (!processAlive) {
149
+ throw new Error("Timeout waiting for backend process to be alive");
150
+ }
151
+ throw new Error("Timeout waiting for backend to be ready");
152
+ }
153
+
154
+ if (!isFirstCheck) await sleep(pollMs);
155
+ isFirstCheck = false;
156
+
157
+ if (!this.pid) continue;
158
+
159
+ processAlive = await isProcessAlive(this.pid);
160
+ wasAlive = wasAlive || processAlive;
161
+ if (!processAlive) {
162
+ if (wasAlive) {
163
+ // saw the process alive at least once, now it's gone — definitive failure
164
+ throw new Error("Backend process died while waiting for readiness");
165
+ }
166
+ continue;
167
+ }
168
+
169
+ try {
170
+ const status = await plHealthCheck({
171
+ address: `127.0.0.1:${grpcPort}`,
172
+ defaultRequestTimeout: requestTimeoutMs,
173
+ service: "GRPC",
174
+ });
175
+ if (status === ServingStatus.SERVING) return;
176
+ } catch {
177
+ // not yet serving
178
+ }
179
+ }
102
180
  }
103
181
 
104
182
  debugInfo() {
@@ -138,9 +216,9 @@ export type LocalPlOptions = {
138
216
 
139
217
  /**
140
218
  * gRPC port the backend listens on (always on 127.0.0.1 for local).
141
- * When set, {@link LocalPl.isAlive} additionally probes this TCP port so callers
142
- * can distinguish a process that is alive but still binding from a process that is
143
- * actually accepting gRPC connections.
219
+ * Required by {@link LocalPl.isReady}, which polls
220
+ * `grpc.health.v1.Health/Check` against this port to distinguish a process
221
+ * that is alive but still initializing from one actually serving gRPC.
144
222
  */
145
223
  readonly grpcPort?: number;
146
224
 
@@ -287,28 +365,6 @@ export function mergeDefaultOps(ops: LocalPlOptions, numCpu: number): LocalPlOpt
287
365
  return { ...result, ...withoutSpawnOps };
288
366
  }
289
367
 
290
- /** Resolves true if a TCP connect to host:port succeeds within timeoutMs. Used by
291
- * {@link LocalPl.isAlive} to verify the gRPC listener has actually bound — the backend
292
- * process can be alive long before its server sockets are accepting connections. */
293
- async function tcpProbe(host: string, port: number, timeoutMs = 500): Promise<boolean> {
294
- return await new Promise<boolean>((resolve) => {
295
- const socket = net.connect({ host, port });
296
- const finalize = (ok: boolean) => {
297
- socket.destroy();
298
- resolve(ok);
299
- };
300
- const timer = setTimeout(() => finalize(false), timeoutMs);
301
- socket.once("connect", () => {
302
- clearTimeout(timer);
303
- finalize(true);
304
- });
305
- socket.once("error", () => {
306
- clearTimeout(timer);
307
- finalize(false);
308
- });
309
- });
310
- }
311
-
312
368
  /** Gets default options for a platforma local binary
313
369
  * and overrides them with the provided options. */
314
370
  export function plProcessOps(