@milaboratories/pl-deployments 2.15.16 → 2.15.18
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/_virtual/_rolldown/runtime.cjs +29 -0
- package/dist/common/os_and_arch.cjs +22 -28
- package/dist/common/os_and_arch.cjs.map +1 -1
- package/dist/common/os_and_arch.js +23 -26
- package/dist/common/os_and_arch.js.map +1 -1
- package/dist/common/pl_binary.cjs +35 -33
- package/dist/common/pl_binary.cjs.map +1 -1
- package/dist/common/pl_binary.d.ts +13 -18
- package/dist/common/pl_binary.js +33 -31
- package/dist/common/pl_binary.js.map +1 -1
- package/dist/common/pl_binary_download.cjs +143 -155
- package/dist/common/pl_binary_download.cjs.map +1 -1
- package/dist/common/pl_binary_download.d.ts +15 -48
- package/dist/common/pl_binary_download.js +138 -133
- package/dist/common/pl_binary_download.js.map +1 -1
- package/dist/common/pl_version.cjs +5 -6
- package/dist/common/pl_version.cjs.map +1 -1
- package/dist/common/pl_version.d.ts +4 -1
- package/dist/common/pl_version.js +5 -4
- package/dist/common/pl_version.js.map +1 -1
- package/dist/index.cjs +21 -25
- package/dist/index.d.ts +6 -6
- package/dist/index.js +7 -6
- package/dist/local/pid.cjs +14 -13
- package/dist/local/pid.cjs.map +1 -1
- package/dist/local/pid.js +11 -11
- package/dist/local/pid.js.map +1 -1
- package/dist/local/pl.cjs +194 -223
- package/dist/local/pl.cjs.map +1 -1
- package/dist/local/pl.d.ts +65 -65
- package/dist/local/pl.js +190 -202
- package/dist/local/pl.js.map +1 -1
- package/dist/local/process.cjs +37 -59
- package/dist/local/process.cjs.map +1 -1
- package/dist/local/process.d.ts +10 -10
- package/dist/local/process.js +36 -57
- package/dist/local/process.js.map +1 -1
- package/dist/local/trace.cjs +14 -17
- package/dist/local/trace.cjs.map +1 -1
- package/dist/local/trace.d.ts +6 -7
- package/dist/local/trace.js +15 -15
- package/dist/local/trace.js.map +1 -1
- package/dist/package.cjs +12 -0
- package/dist/package.cjs.map +1 -0
- package/dist/package.js +6 -0
- package/dist/package.js.map +1 -0
- package/dist/ssh/connection_info.cjs +36 -53
- package/dist/ssh/connection_info.cjs.map +1 -1
- package/dist/ssh/connection_info.d.ts +691 -713
- package/dist/ssh/connection_info.js +35 -51
- package/dist/ssh/connection_info.js.map +1 -1
- package/dist/ssh/pl.cjs +551 -638
- package/dist/ssh/pl.cjs.map +1 -1
- package/dist/ssh/pl.d.ts +120 -117
- package/dist/ssh/pl.js +548 -636
- package/dist/ssh/pl.js.map +1 -1
- package/dist/ssh/pl_paths.cjs +22 -24
- package/dist/ssh/pl_paths.cjs.map +1 -1
- package/dist/ssh/pl_paths.js +21 -19
- package/dist/ssh/pl_paths.js.map +1 -1
- package/dist/ssh/ssh.cjs +554 -618
- package/dist/ssh/ssh.cjs.map +1 -1
- package/dist/ssh/ssh.d.ts +139 -136
- package/dist/ssh/ssh.js +548 -616
- package/dist/ssh/ssh.js.map +1 -1
- package/dist/ssh/ssh_errors.cjs +45 -60
- package/dist/ssh/ssh_errors.cjs.map +1 -1
- package/dist/ssh/ssh_errors.js +45 -58
- package/dist/ssh/ssh_errors.js.map +1 -1
- package/dist/ssh/supervisord.cjs +50 -68
- package/dist/ssh/supervisord.cjs.map +1 -1
- package/dist/ssh/supervisord.d.ts +11 -21
- package/dist/ssh/supervisord.js +50 -66
- package/dist/ssh/supervisord.js.map +1 -1
- package/package.json +11 -11
- package/dist/common/os_and_arch.d.ts +0 -9
- package/dist/common/os_and_arch.d.ts.map +0 -1
- package/dist/common/pl_binary.d.ts.map +0 -1
- package/dist/common/pl_binary_download.d.ts.map +0 -1
- package/dist/common/pl_version.d.ts.map +0 -1
- package/dist/index.cjs.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/local/options.d.ts +0 -31
- package/dist/local/options.d.ts.map +0 -1
- package/dist/local/pid.d.ts +0 -4
- package/dist/local/pid.d.ts.map +0 -1
- package/dist/local/pl.d.ts.map +0 -1
- package/dist/local/process.d.ts.map +0 -1
- package/dist/local/trace.d.ts.map +0 -1
- package/dist/package.json.cjs +0 -8
- package/dist/package.json.cjs.map +0 -1
- package/dist/package.json.js +0 -6
- package/dist/package.json.js.map +0 -1
- package/dist/ssh/__tests__/common-utils.d.ts +0 -12
- package/dist/ssh/__tests__/common-utils.d.ts.map +0 -1
- package/dist/ssh/connection_info.d.ts.map +0 -1
- package/dist/ssh/pl.d.ts.map +0 -1
- package/dist/ssh/pl_paths.d.ts +0 -20
- package/dist/ssh/pl_paths.d.ts.map +0 -1
- package/dist/ssh/ssh.d.ts.map +0 -1
- package/dist/ssh/ssh_errors.d.ts +0 -29
- package/dist/ssh/ssh_errors.d.ts.map +0 -1
- package/dist/ssh/supervisord.d.ts.map +0 -1
package/dist/local/pl.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pl.js","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 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 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 return await isProcessAlive(notEmpty(this.pid));\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 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.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"],"names":[],"mappings":";;;;;;;;;;;AAkBO,MAAM,eAAe,GAAG;AAE/B;;;;AAIG;MACU,OAAO,CAAA;AAQC,IAAA,MAAA;AACA,IAAA,UAAA;AACA,IAAA,YAAA;AACA,IAAA,mBAAA;AACA,IAAA,OAAA;AACA,IAAA,OAAA;AACA,IAAA,eAA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AAC,cAAc,CAAC;AAEtC,IAAA,OAAO,EAAE,GAAG,MAAM,EAAE,GAAG,eAAe,EAAE;AAC1C;AAEA;AACmD;AAC7C,SAAU,YAAY,CAC1B,UAAe,EACf,UAAkB,EAClB,GAAuB,EACvB,OAAe,EACf,UAA8C,EAAA;AAE9C,IAAA,MAAM,MAAM,GAAmB;AAC7B,QAAA,GAAG,EAAE,UAAU;AACf,QAAA,IAAI,EAAE,CAAC,UAAU,EAAE,UAAU,CAAC;AAC9B,QAAA,IAAI,EAAE;AACJ,YAAA,GAAG,EAAE,EAAE,GAAG,UAAU,EAAE;AACtB,YAAA,GAAG,EAAE,OAAO;AACZ,YAAA,KAAK,EAAE,CAAC,MAAM,EAAE,QAAQ,EAAE,SAAS,CAAC;YACpC,WAAW,EAAE,IAAI;AAClB,SAAA;KACF;AAED,IAAA,IAAI,GAAG,CAAC,YAAY,EAAE,GAAG,EAAE;QACzB,MAAM,CAAC,IAAI,CAAC,GAAG,GAAG,EAAE,GAAG,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,GAAG,CAAC,YAAY,CAAC,GAAG,EAAE;IACnE;IAEA,MAAM,UAAU,GAAG,EAAE,GAAG,GAAG,CAAC,YAAY,EAAE;AAC1C,IAAA,OAAO,UAAU,CAAC,KAAK,CAAC;AACxB,IAAA,MAAM,CAAC,IAAI,GAAG,EAAE,GAAG,MAAM,CAAC,IAAI,EAAE,GAAG,UAAU,EAAE;AAE/C,IAAA,OAAO,MAAM;AACf;;;;"}
|
|
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 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 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 return await isProcessAlive(notEmpty(this.pid));\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 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.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":";;;;;;;;;;;;AAkBA,MAAa,kBAAkB;;;;;;AAO/B,IAAa,UAAb,MAAqB;CACnB,AAAQ;CACR,AAAO;CACP,AAAQ,QAAgB;CACxB,AAAQ,iBAAwB,EAAE;CAClC,AAAQ,aAAa;CAErB,YACE,AAAiB,QACjB,AAAiB,YACjB,AAAiB,cACjB,AAAiB,qBACjB,AAAiB,SACjB,AAAiB,SACjB,AAAiB,iBACjB,AAAiB,uBACjB;EARiB;EACA;EACA;EACA;EACA;EACA;EACA;EACA;;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;AAC/B,SAAK,OAAO,MACV,UAAU,EAAE,4CAA4C,KAAK,UAAU,KAAK,WAAW,CAAC,GACzF;AAGD,QAAI,KAAK,YAAY,OAAW,CAAK,KAAK,QAAQ,KAAK;AACvD,QAAI,KAAK,oBAAoB,OAAW,CAAK,KAAK,gBAAgB,KAAK;AACvE,QAAI,KAAK,0BAA0B,UAAa,CAAC,KAAK,WACpD,CAAK,KAAK,sBAAsB,KAAK;KACvC;AACF,YAAS,GAAG,eAAe;AACzB,SAAK,OAAO,KAAK,uCAAuC,KAAK,UAAU,KAAK,WAAW,CAAC,GAAG;AAG3F,QAAI,KAAK,YAAY,OAAW,CAAK,KAAK,QAAQ,KAAK;AACvD,QAAI,KAAK,oBAAoB,OAAW,CAAK,KAAK,gBAAgB,KAAK;AACvE,QAAI,KAAK,0BAA0B,UAAa,CAAC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;AAChD,UAAM,oBAAoB,MAAM,mBAAmB,QAAQ,IAAM,CAAC;;;AAItE,SAAO;GACP;;;;AAKJ,SAAgB,gBAAgB,KAAqB,QAAoC;CACvF,MAAM,SAIF;EACF,UAAU,0BAA0B;EACpC,cAAc,EACZ,KAAK,EACH,YAAY,OAAO,OAAO,EAC3B,EACF;EACD,UAAU;EACX;AAED,KAAI,IAAI,cAAc,IACpB,QAAO,aAAa,MAAM;EAAE,GAAG,OAAO,aAAa;EAAK,GAAG,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;;;;AAK1C,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"}
|
package/dist/local/process.cjs
CHANGED
|
@@ -1,77 +1,55 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
var tsHelpers = require('@milaboratories/ts-helpers');
|
|
1
|
+
const require_runtime = require('../_virtual/_rolldown/runtime.cjs');
|
|
2
|
+
let node_child_process = require("node:child_process");
|
|
3
|
+
let _milaboratories_ts_helpers = require("@milaboratories/ts-helpers");
|
|
5
4
|
|
|
5
|
+
//#region src/local/process.ts
|
|
6
6
|
function processRun(logger, opts) {
|
|
7
|
-
|
|
7
|
+
logger.info(`Running:
|
|
8
8
|
cmd: ${JSON.stringify([opts.cmd, ...opts.args])}
|
|
9
9
|
wd: ${opts.opts.cwd}`);
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
logger.info(" spawning child process");
|
|
11
|
+
return (0, node_child_process.spawn)(opts.cmd, opts.args, opts.opts);
|
|
12
12
|
}
|
|
13
|
-
// eslint-disable-next-line @typescript-eslint/require-await
|
|
14
13
|
async function isProcessAlive(pid) {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
// Process name can contain original path to the binary file or just its name.
|
|
24
|
-
return processName.endsWith("/platforma") || processName === "platforma";
|
|
25
|
-
}
|
|
26
|
-
catch {
|
|
27
|
-
return false;
|
|
28
|
-
}
|
|
14
|
+
try {
|
|
15
|
+
process.kill(pid, 0);
|
|
16
|
+
const processName = getProcessName(pid);
|
|
17
|
+
if (process.platform === "win32") return processName === "platforma.exe";
|
|
18
|
+
return processName.endsWith("/platforma") || processName === "platforma";
|
|
19
|
+
} catch {
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
29
22
|
}
|
|
30
23
|
function getProcessName(pid) {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
else {
|
|
46
|
-
// Unix-like systems: use ps command
|
|
47
|
-
const output = node_child_process.execSync(`ps -p ${pid} -o comm=`, { encoding: "utf8" });
|
|
48
|
-
const processName = output.trim();
|
|
49
|
-
return processName;
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
catch {
|
|
53
|
-
// If we can't get the process name, return empty string
|
|
54
|
-
return "";
|
|
55
|
-
}
|
|
56
|
-
return "";
|
|
24
|
+
try {
|
|
25
|
+
if (process.platform === "win32") {
|
|
26
|
+
const lines = (0, node_child_process.execSync)(`tasklist /FI "PID eq ${pid}" /FO CSV /NH`, { encoding: "utf8" }).trim().split("\n");
|
|
27
|
+
if (lines.length > 0 && lines[0].includes(",")) {
|
|
28
|
+
const parts = lines[0].split(",");
|
|
29
|
+
if (parts.length >= 1) return parts[0].replace(/^"|"$/g, "").trim();
|
|
30
|
+
}
|
|
31
|
+
} else return (0, node_child_process.execSync)(`ps -p ${pid} -o comm=`, { encoding: "utf8" }).trim();
|
|
32
|
+
} catch {
|
|
33
|
+
return "";
|
|
34
|
+
}
|
|
35
|
+
return "";
|
|
57
36
|
}
|
|
58
37
|
function processStop(pid, force = false) {
|
|
59
|
-
|
|
38
|
+
return process.kill(pid, force ? "SIGKILL" : "SIGINT");
|
|
60
39
|
}
|
|
61
40
|
async function processWaitStopped(pid, maxMs) {
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
}
|
|
70
|
-
}
|
|
41
|
+
const sleepMs = 100;
|
|
42
|
+
let total = 0;
|
|
43
|
+
while (await isProcessAlive(pid)) {
|
|
44
|
+
await (0, _milaboratories_ts_helpers.sleep)(sleepMs);
|
|
45
|
+
total += sleepMs;
|
|
46
|
+
if (total > maxMs) throw new Error(`The process did not stopped after ${maxMs} ms.`);
|
|
47
|
+
}
|
|
71
48
|
}
|
|
72
49
|
|
|
50
|
+
//#endregion
|
|
73
51
|
exports.isProcessAlive = isProcessAlive;
|
|
74
52
|
exports.processRun = processRun;
|
|
75
53
|
exports.processStop = processStop;
|
|
76
54
|
exports.processWaitStopped = processWaitStopped;
|
|
77
|
-
//# sourceMappingURL=process.cjs.map
|
|
55
|
+
//# sourceMappingURL=process.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"process.cjs","sources":["../../src/local/process.ts"],"sourcesContent":["import type { SpawnOptions, ChildProcess } from \"node:child_process\";\nimport { spawn } from \"node:child_process\";\nimport { execSync } from \"node:child_process\";\nimport type { MiLogger } from \"@milaboratories/ts-helpers\";\nimport { sleep } from \"@milaboratories/ts-helpers\";\n\nexport type ProcessOptions = {\n cmd: string;\n args: string[];\n opts: SpawnOptions;\n};\n\nexport function processRun(logger: MiLogger, opts: ProcessOptions): ChildProcess {\n logger.info(`Running:\ncmd: ${JSON.stringify([opts.cmd, ...opts.args])}\nwd: ${opts.opts.cwd}`);\n\n logger.info(\" spawning child process\");\n return spawn(opts.cmd, opts.args, opts.opts);\n}\n\n// eslint-disable-next-line @typescript-eslint/require-await\nexport async function isProcessAlive(pid: number) {\n try {\n process.kill(pid, 0);\n\n // Check we look at 'platforma' to not kill wrong process.\n const processName = getProcessName(pid);\n if (process.platform === \"win32\") {\n return processName === \"platforma.exe\"; // process name does not contain path to the file.\n }\n\n // Linux and Mac OS X behave differently (so can different Linux distributions).\n // Process name can contain original path to the binary file or just its name.\n return processName.endsWith(\"/platforma\") || processName === \"platforma\";\n } catch {\n return false;\n }\n}\n\nfunction getProcessName(pid: number): string {\n try {\n if (process.platform === \"win32\") {\n // Windows: use tasklist command\n const output = execSync(`tasklist /FI \"PID eq ${pid}\" /FO CSV /NH`, { encoding: \"utf8\" });\n const lines = output.trim().split(\"\\n\");\n if (lines.length > 0 && lines[0].includes(\",\")) {\n const parts = lines[0].split(\",\");\n if (parts.length >= 1) {\n // Remove quotes and get the executable name\n const exeName = parts[0].replace(/^\"|\"$/g, \"\").trim();\n return exeName;\n }\n }\n } else {\n // Unix-like systems: use ps command\n const output = execSync(`ps -p ${pid} -o comm=`, { encoding: \"utf8\" });\n const processName = output.trim();\n return processName;\n }\n } catch {\n // If we can't get the process name, return empty string\n return \"\";\n }\n return \"\";\n}\n\nexport function processStop(pid: number, force: boolean = false) {\n return process.kill(pid, force ? \"SIGKILL\" : \"SIGINT\");\n}\n\nexport async function processWaitStopped(pid: number, maxMs: number) {\n const sleepMs = 100;\n let total = 0;\n while (await isProcessAlive(pid)) {\n await sleep(sleepMs);\n total += sleepMs;\n if (total > maxMs) {\n throw new Error(`The process did not stopped after ${maxMs} ms.`);\n }\n }\n}\n"],"
|
|
1
|
+
{"version":3,"file":"process.cjs","names":[],"sources":["../../src/local/process.ts"],"sourcesContent":["import type { SpawnOptions, ChildProcess } from \"node:child_process\";\nimport { spawn } from \"node:child_process\";\nimport { execSync } from \"node:child_process\";\nimport type { MiLogger } from \"@milaboratories/ts-helpers\";\nimport { sleep } from \"@milaboratories/ts-helpers\";\n\nexport type ProcessOptions = {\n cmd: string;\n args: string[];\n opts: SpawnOptions;\n};\n\nexport function processRun(logger: MiLogger, opts: ProcessOptions): ChildProcess {\n logger.info(`Running:\ncmd: ${JSON.stringify([opts.cmd, ...opts.args])}\nwd: ${opts.opts.cwd}`);\n\n logger.info(\" spawning child process\");\n return spawn(opts.cmd, opts.args, opts.opts);\n}\n\n// eslint-disable-next-line @typescript-eslint/require-await\nexport async function isProcessAlive(pid: number) {\n try {\n process.kill(pid, 0);\n\n // Check we look at 'platforma' to not kill wrong process.\n const processName = getProcessName(pid);\n if (process.platform === \"win32\") {\n return processName === \"platforma.exe\"; // process name does not contain path to the file.\n }\n\n // Linux and Mac OS X behave differently (so can different Linux distributions).\n // Process name can contain original path to the binary file or just its name.\n return processName.endsWith(\"/platforma\") || processName === \"platforma\";\n } catch {\n return false;\n }\n}\n\nfunction getProcessName(pid: number): string {\n try {\n if (process.platform === \"win32\") {\n // Windows: use tasklist command\n const output = execSync(`tasklist /FI \"PID eq ${pid}\" /FO CSV /NH`, { encoding: \"utf8\" });\n const lines = output.trim().split(\"\\n\");\n if (lines.length > 0 && lines[0].includes(\",\")) {\n const parts = lines[0].split(\",\");\n if (parts.length >= 1) {\n // Remove quotes and get the executable name\n const exeName = parts[0].replace(/^\"|\"$/g, \"\").trim();\n return exeName;\n }\n }\n } else {\n // Unix-like systems: use ps command\n const output = execSync(`ps -p ${pid} -o comm=`, { encoding: \"utf8\" });\n const processName = output.trim();\n return processName;\n }\n } catch {\n // If we can't get the process name, return empty string\n return \"\";\n }\n return \"\";\n}\n\nexport function processStop(pid: number, force: boolean = false) {\n return process.kill(pid, force ? \"SIGKILL\" : \"SIGINT\");\n}\n\nexport async function processWaitStopped(pid: number, maxMs: number) {\n const sleepMs = 100;\n let total = 0;\n while (await isProcessAlive(pid)) {\n await sleep(sleepMs);\n total += sleepMs;\n if (total > maxMs) {\n throw new Error(`The process did not stopped after ${maxMs} ms.`);\n }\n }\n}\n"],"mappings":";;;;;AAYA,SAAgB,WAAW,QAAkB,MAAoC;AAC/E,QAAO,KAAK;OACP,KAAK,UAAU,CAAC,KAAK,KAAK,GAAG,KAAK,KAAK,CAAC,CAAC;MAC1C,KAAK,KAAK,MAAM;AAEpB,QAAO,KAAK,2BAA2B;AACvC,sCAAa,KAAK,KAAK,KAAK,MAAM,KAAK,KAAK;;AAI9C,eAAsB,eAAe,KAAa;AAChD,KAAI;AACF,UAAQ,KAAK,KAAK,EAAE;EAGpB,MAAM,cAAc,eAAe,IAAI;AACvC,MAAI,QAAQ,aAAa,QACvB,QAAO,gBAAgB;AAKzB,SAAO,YAAY,SAAS,aAAa,IAAI,gBAAgB;SACvD;AACN,SAAO;;;AAIX,SAAS,eAAe,KAAqB;AAC3C,KAAI;AACF,MAAI,QAAQ,aAAa,SAAS;GAGhC,MAAM,yCADkB,wBAAwB,IAAI,gBAAgB,EAAE,UAAU,QAAQ,CAAC,CACpE,MAAM,CAAC,MAAM,KAAK;AACvC,OAAI,MAAM,SAAS,KAAK,MAAM,GAAG,SAAS,IAAI,EAAE;IAC9C,MAAM,QAAQ,MAAM,GAAG,MAAM,IAAI;AACjC,QAAI,MAAM,UAAU,EAGlB,QADgB,MAAM,GAAG,QAAQ,UAAU,GAAG,CAAC,MAAM;;QAQzD,yCAFwB,SAAS,IAAI,YAAY,EAAE,UAAU,QAAQ,CAAC,CAC3C,MAAM;SAG7B;AAEN,SAAO;;AAET,QAAO;;AAGT,SAAgB,YAAY,KAAa,QAAiB,OAAO;AAC/D,QAAO,QAAQ,KAAK,KAAK,QAAQ,YAAY,SAAS;;AAGxD,eAAsB,mBAAmB,KAAa,OAAe;CACnE,MAAM,UAAU;CAChB,IAAI,QAAQ;AACZ,QAAO,MAAM,eAAe,IAAI,EAAE;AAChC,8CAAY,QAAQ;AACpB,WAAS;AACT,MAAI,QAAQ,MACV,OAAM,IAAI,MAAM,qCAAqC,MAAM,MAAM"}
|
package/dist/local/process.d.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
import { SpawnOptions } from "node:child_process";
|
|
2
|
+
import { MiLogger } from "@milaboratories/ts-helpers";
|
|
3
|
+
|
|
4
|
+
//#region src/local/process.d.ts
|
|
5
|
+
type ProcessOptions = {
|
|
6
|
+
cmd: string;
|
|
7
|
+
args: string[];
|
|
8
|
+
opts: SpawnOptions;
|
|
7
9
|
};
|
|
8
|
-
|
|
9
|
-
export
|
|
10
|
-
export declare function processStop(pid: number, force?: boolean): true;
|
|
11
|
-
export declare function processWaitStopped(pid: number, maxMs: number): Promise<void>;
|
|
10
|
+
//#endregion
|
|
11
|
+
export { ProcessOptions };
|
|
12
12
|
//# sourceMappingURL=process.d.ts.map
|
package/dist/local/process.js
CHANGED
|
@@ -1,72 +1,51 @@
|
|
|
1
|
-
import { execSync, spawn } from
|
|
2
|
-
import { sleep } from
|
|
1
|
+
import { execSync, spawn } from "node:child_process";
|
|
2
|
+
import { sleep } from "@milaboratories/ts-helpers";
|
|
3
3
|
|
|
4
|
+
//#region src/local/process.ts
|
|
4
5
|
function processRun(logger, opts) {
|
|
5
|
-
|
|
6
|
+
logger.info(`Running:
|
|
6
7
|
cmd: ${JSON.stringify([opts.cmd, ...opts.args])}
|
|
7
8
|
wd: ${opts.opts.cwd}`);
|
|
8
|
-
|
|
9
|
-
|
|
9
|
+
logger.info(" spawning child process");
|
|
10
|
+
return spawn(opts.cmd, opts.args, opts.opts);
|
|
10
11
|
}
|
|
11
|
-
// eslint-disable-next-line @typescript-eslint/require-await
|
|
12
12
|
async function isProcessAlive(pid) {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
// Process name can contain original path to the binary file or just its name.
|
|
22
|
-
return processName.endsWith("/platforma") || processName === "platforma";
|
|
23
|
-
}
|
|
24
|
-
catch {
|
|
25
|
-
return false;
|
|
26
|
-
}
|
|
13
|
+
try {
|
|
14
|
+
process.kill(pid, 0);
|
|
15
|
+
const processName = getProcessName(pid);
|
|
16
|
+
if (process.platform === "win32") return processName === "platforma.exe";
|
|
17
|
+
return processName.endsWith("/platforma") || processName === "platforma";
|
|
18
|
+
} catch {
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
27
21
|
}
|
|
28
22
|
function getProcessName(pid) {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
else {
|
|
44
|
-
// Unix-like systems: use ps command
|
|
45
|
-
const output = execSync(`ps -p ${pid} -o comm=`, { encoding: "utf8" });
|
|
46
|
-
const processName = output.trim();
|
|
47
|
-
return processName;
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
catch {
|
|
51
|
-
// If we can't get the process name, return empty string
|
|
52
|
-
return "";
|
|
53
|
-
}
|
|
54
|
-
return "";
|
|
23
|
+
try {
|
|
24
|
+
if (process.platform === "win32") {
|
|
25
|
+
const lines = execSync(`tasklist /FI "PID eq ${pid}" /FO CSV /NH`, { encoding: "utf8" }).trim().split("\n");
|
|
26
|
+
if (lines.length > 0 && lines[0].includes(",")) {
|
|
27
|
+
const parts = lines[0].split(",");
|
|
28
|
+
if (parts.length >= 1) return parts[0].replace(/^"|"$/g, "").trim();
|
|
29
|
+
}
|
|
30
|
+
} else return execSync(`ps -p ${pid} -o comm=`, { encoding: "utf8" }).trim();
|
|
31
|
+
} catch {
|
|
32
|
+
return "";
|
|
33
|
+
}
|
|
34
|
+
return "";
|
|
55
35
|
}
|
|
56
36
|
function processStop(pid, force = false) {
|
|
57
|
-
|
|
37
|
+
return process.kill(pid, force ? "SIGKILL" : "SIGINT");
|
|
58
38
|
}
|
|
59
39
|
async function processWaitStopped(pid, maxMs) {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
}
|
|
68
|
-
}
|
|
40
|
+
const sleepMs = 100;
|
|
41
|
+
let total = 0;
|
|
42
|
+
while (await isProcessAlive(pid)) {
|
|
43
|
+
await sleep(sleepMs);
|
|
44
|
+
total += sleepMs;
|
|
45
|
+
if (total > maxMs) throw new Error(`The process did not stopped after ${maxMs} ms.`);
|
|
46
|
+
}
|
|
69
47
|
}
|
|
70
48
|
|
|
49
|
+
//#endregion
|
|
71
50
|
export { isProcessAlive, processRun, processStop, processWaitStopped };
|
|
72
|
-
//# sourceMappingURL=process.js.map
|
|
51
|
+
//# sourceMappingURL=process.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"process.js","sources":["../../src/local/process.ts"],"sourcesContent":["import type { SpawnOptions, ChildProcess } from \"node:child_process\";\nimport { spawn } from \"node:child_process\";\nimport { execSync } from \"node:child_process\";\nimport type { MiLogger } from \"@milaboratories/ts-helpers\";\nimport { sleep } from \"@milaboratories/ts-helpers\";\n\nexport type ProcessOptions = {\n cmd: string;\n args: string[];\n opts: SpawnOptions;\n};\n\nexport function processRun(logger: MiLogger, opts: ProcessOptions): ChildProcess {\n logger.info(`Running:\ncmd: ${JSON.stringify([opts.cmd, ...opts.args])}\nwd: ${opts.opts.cwd}`);\n\n logger.info(\" spawning child process\");\n return spawn(opts.cmd, opts.args, opts.opts);\n}\n\n// eslint-disable-next-line @typescript-eslint/require-await\nexport async function isProcessAlive(pid: number) {\n try {\n process.kill(pid, 0);\n\n // Check we look at 'platforma' to not kill wrong process.\n const processName = getProcessName(pid);\n if (process.platform === \"win32\") {\n return processName === \"platforma.exe\"; // process name does not contain path to the file.\n }\n\n // Linux and Mac OS X behave differently (so can different Linux distributions).\n // Process name can contain original path to the binary file or just its name.\n return processName.endsWith(\"/platforma\") || processName === \"platforma\";\n } catch {\n return false;\n }\n}\n\nfunction getProcessName(pid: number): string {\n try {\n if (process.platform === \"win32\") {\n // Windows: use tasklist command\n const output = execSync(`tasklist /FI \"PID eq ${pid}\" /FO CSV /NH`, { encoding: \"utf8\" });\n const lines = output.trim().split(\"\\n\");\n if (lines.length > 0 && lines[0].includes(\",\")) {\n const parts = lines[0].split(\",\");\n if (parts.length >= 1) {\n // Remove quotes and get the executable name\n const exeName = parts[0].replace(/^\"|\"$/g, \"\").trim();\n return exeName;\n }\n }\n } else {\n // Unix-like systems: use ps command\n const output = execSync(`ps -p ${pid} -o comm=`, { encoding: \"utf8\" });\n const processName = output.trim();\n return processName;\n }\n } catch {\n // If we can't get the process name, return empty string\n return \"\";\n }\n return \"\";\n}\n\nexport function processStop(pid: number, force: boolean = false) {\n return process.kill(pid, force ? \"SIGKILL\" : \"SIGINT\");\n}\n\nexport async function processWaitStopped(pid: number, maxMs: number) {\n const sleepMs = 100;\n let total = 0;\n while (await isProcessAlive(pid)) {\n await sleep(sleepMs);\n total += sleepMs;\n if (total > maxMs) {\n throw new Error(`The process did not stopped after ${maxMs} ms.`);\n }\n }\n}\n"],"
|
|
1
|
+
{"version":3,"file":"process.js","names":[],"sources":["../../src/local/process.ts"],"sourcesContent":["import type { SpawnOptions, ChildProcess } from \"node:child_process\";\nimport { spawn } from \"node:child_process\";\nimport { execSync } from \"node:child_process\";\nimport type { MiLogger } from \"@milaboratories/ts-helpers\";\nimport { sleep } from \"@milaboratories/ts-helpers\";\n\nexport type ProcessOptions = {\n cmd: string;\n args: string[];\n opts: SpawnOptions;\n};\n\nexport function processRun(logger: MiLogger, opts: ProcessOptions): ChildProcess {\n logger.info(`Running:\ncmd: ${JSON.stringify([opts.cmd, ...opts.args])}\nwd: ${opts.opts.cwd}`);\n\n logger.info(\" spawning child process\");\n return spawn(opts.cmd, opts.args, opts.opts);\n}\n\n// eslint-disable-next-line @typescript-eslint/require-await\nexport async function isProcessAlive(pid: number) {\n try {\n process.kill(pid, 0);\n\n // Check we look at 'platforma' to not kill wrong process.\n const processName = getProcessName(pid);\n if (process.platform === \"win32\") {\n return processName === \"platforma.exe\"; // process name does not contain path to the file.\n }\n\n // Linux and Mac OS X behave differently (so can different Linux distributions).\n // Process name can contain original path to the binary file or just its name.\n return processName.endsWith(\"/platforma\") || processName === \"platforma\";\n } catch {\n return false;\n }\n}\n\nfunction getProcessName(pid: number): string {\n try {\n if (process.platform === \"win32\") {\n // Windows: use tasklist command\n const output = execSync(`tasklist /FI \"PID eq ${pid}\" /FO CSV /NH`, { encoding: \"utf8\" });\n const lines = output.trim().split(\"\\n\");\n if (lines.length > 0 && lines[0].includes(\",\")) {\n const parts = lines[0].split(\",\");\n if (parts.length >= 1) {\n // Remove quotes and get the executable name\n const exeName = parts[0].replace(/^\"|\"$/g, \"\").trim();\n return exeName;\n }\n }\n } else {\n // Unix-like systems: use ps command\n const output = execSync(`ps -p ${pid} -o comm=`, { encoding: \"utf8\" });\n const processName = output.trim();\n return processName;\n }\n } catch {\n // If we can't get the process name, return empty string\n return \"\";\n }\n return \"\";\n}\n\nexport function processStop(pid: number, force: boolean = false) {\n return process.kill(pid, force ? \"SIGKILL\" : \"SIGINT\");\n}\n\nexport async function processWaitStopped(pid: number, maxMs: number) {\n const sleepMs = 100;\n let total = 0;\n while (await isProcessAlive(pid)) {\n await sleep(sleepMs);\n total += sleepMs;\n if (total > maxMs) {\n throw new Error(`The process did not stopped after ${maxMs} ms.`);\n }\n }\n}\n"],"mappings":";;;;AAYA,SAAgB,WAAW,QAAkB,MAAoC;AAC/E,QAAO,KAAK;OACP,KAAK,UAAU,CAAC,KAAK,KAAK,GAAG,KAAK,KAAK,CAAC,CAAC;MAC1C,KAAK,KAAK,MAAM;AAEpB,QAAO,KAAK,2BAA2B;AACvC,QAAO,MAAM,KAAK,KAAK,KAAK,MAAM,KAAK,KAAK;;AAI9C,eAAsB,eAAe,KAAa;AAChD,KAAI;AACF,UAAQ,KAAK,KAAK,EAAE;EAGpB,MAAM,cAAc,eAAe,IAAI;AACvC,MAAI,QAAQ,aAAa,QACvB,QAAO,gBAAgB;AAKzB,SAAO,YAAY,SAAS,aAAa,IAAI,gBAAgB;SACvD;AACN,SAAO;;;AAIX,SAAS,eAAe,KAAqB;AAC3C,KAAI;AACF,MAAI,QAAQ,aAAa,SAAS;GAGhC,MAAM,QADS,SAAS,wBAAwB,IAAI,gBAAgB,EAAE,UAAU,QAAQ,CAAC,CACpE,MAAM,CAAC,MAAM,KAAK;AACvC,OAAI,MAAM,SAAS,KAAK,MAAM,GAAG,SAAS,IAAI,EAAE;IAC9C,MAAM,QAAQ,MAAM,GAAG,MAAM,IAAI;AACjC,QAAI,MAAM,UAAU,EAGlB,QADgB,MAAM,GAAG,QAAQ,UAAU,GAAG,CAAC,MAAM;;QAQzD,QAFe,SAAS,SAAS,IAAI,YAAY,EAAE,UAAU,QAAQ,CAAC,CAC3C,MAAM;SAG7B;AAEN,SAAO;;AAET,QAAO;;AAGT,SAAgB,YAAY,KAAa,QAAiB,OAAO;AAC/D,QAAO,QAAQ,KAAK,KAAK,QAAQ,YAAY,SAAS;;AAGxD,eAAsB,mBAAmB,KAAa,OAAe;CACnE,MAAM,UAAU;CAChB,IAAI,QAAQ;AACZ,QAAO,MAAM,eAAe,IAAI,EAAE;AAChC,QAAM,MAAM,QAAQ;AACpB,WAAS;AACT,MAAI,QAAQ,MACV,OAAM,IAAI,MAAM,qCAAqC,MAAM,MAAM"}
|
package/dist/local/trace.cjs
CHANGED
|
@@ -1,27 +1,24 @@
|
|
|
1
|
-
'use strict';
|
|
2
1
|
|
|
2
|
+
//#region src/local/trace.ts
|
|
3
3
|
function newTrace() {
|
|
4
|
-
|
|
4
|
+
return {};
|
|
5
5
|
}
|
|
6
6
|
function trace(t, k, v) {
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
t[k] = v;
|
|
8
|
+
return v;
|
|
9
9
|
}
|
|
10
10
|
/** Creates a trace and runs a function with it. The function can record all its
|
|
11
|
-
|
|
11
|
+
* logs or traces using `trace` fn. */
|
|
12
12
|
async function withTrace(logger, fn) {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
throw e;
|
|
21
|
-
}
|
|
13
|
+
const t = newTrace();
|
|
14
|
+
try {
|
|
15
|
+
return await fn((k, v) => trace(t, k, v), t);
|
|
16
|
+
} catch (e) {
|
|
17
|
+
logger.error(`error ${e} while doing traced operation, state: ${JSON.stringify(t)}`);
|
|
18
|
+
throw e;
|
|
19
|
+
}
|
|
22
20
|
}
|
|
23
21
|
|
|
24
|
-
|
|
25
|
-
exports.trace = trace;
|
|
22
|
+
//#endregion
|
|
26
23
|
exports.withTrace = withTrace;
|
|
27
|
-
//# sourceMappingURL=trace.cjs.map
|
|
24
|
+
//# sourceMappingURL=trace.cjs.map
|
package/dist/local/trace.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"trace.cjs","sources":["../../src/local/trace.ts"],"sourcesContent":["import type { MiLogger } from \"@milaboratories/ts-helpers\";\n\n/** Records all inputs and outputs of one's choice, so if the error happened\n * one can check how it was by just printing this structure. */\nexport type Trace = Record<string, any>;\n\nexport function newTrace(): Trace {\n return {};\n}\n\nexport function trace(t: Trace, k: string, v: any) {\n t[k] = v;\n return v;\n}\n\n/** Creates a trace and runs a function with it. The function can record all its\n * logs or traces using `trace` fn. */\nexport async function withTrace<T>(\n logger: MiLogger,\n fn: (trace: (k: string, v: any) => typeof v, t: Trace) => Promise<T>,\n): Promise<T> {\n const t = newTrace();\n try {\n const result = await fn((k: string, v: any) => trace(t, k, v), t);\n return result;\n } catch (e: any) {\n logger.error(`error ${e} while doing traced operation, state: ${JSON.stringify(t)}`);\n throw e;\n }\n}\n"],"
|
|
1
|
+
{"version":3,"file":"trace.cjs","names":[],"sources":["../../src/local/trace.ts"],"sourcesContent":["import type { MiLogger } from \"@milaboratories/ts-helpers\";\n\n/** Records all inputs and outputs of one's choice, so if the error happened\n * one can check how it was by just printing this structure. */\nexport type Trace = Record<string, any>;\n\nexport function newTrace(): Trace {\n return {};\n}\n\nexport function trace(t: Trace, k: string, v: any) {\n t[k] = v;\n return v;\n}\n\n/** Creates a trace and runs a function with it. The function can record all its\n * logs or traces using `trace` fn. */\nexport async function withTrace<T>(\n logger: MiLogger,\n fn: (trace: (k: string, v: any) => typeof v, t: Trace) => Promise<T>,\n): Promise<T> {\n const t = newTrace();\n try {\n const result = await fn((k: string, v: any) => trace(t, k, v), t);\n return result;\n } catch (e: any) {\n logger.error(`error ${e} while doing traced operation, state: ${JSON.stringify(t)}`);\n throw e;\n }\n}\n"],"mappings":";;AAMA,SAAgB,WAAkB;AAChC,QAAO,EAAE;;AAGX,SAAgB,MAAM,GAAU,GAAW,GAAQ;AACjD,GAAE,KAAK;AACP,QAAO;;;;AAKT,eAAsB,UACpB,QACA,IACY;CACZ,MAAM,IAAI,UAAU;AACpB,KAAI;AAEF,SADe,MAAM,IAAI,GAAW,MAAW,MAAM,GAAG,GAAG,EAAE,EAAE,EAAE;UAE1D,GAAQ;AACf,SAAO,MAAM,SAAS,EAAE,wCAAwC,KAAK,UAAU,EAAE,GAAG;AACpF,QAAM"}
|
package/dist/local/trace.d.ts
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { MiLogger } from "@milaboratories/ts-helpers";
|
|
2
|
+
|
|
3
|
+
//#region src/local/trace.d.ts
|
|
2
4
|
/** Records all inputs and outputs of one's choice, so if the error happened
|
|
3
5
|
* one can check how it was by just printing this structure. */
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
export
|
|
7
|
-
/** Creates a trace and runs a function with it. The function can record all its
|
|
8
|
-
* logs or traces using `trace` fn. */
|
|
9
|
-
export declare function withTrace<T>(logger: MiLogger, fn: (trace: (k: string, v: any) => typeof v, t: Trace) => Promise<T>): Promise<T>;
|
|
6
|
+
type Trace = Record<string, any>;
|
|
7
|
+
//#endregion
|
|
8
|
+
export { Trace };
|
|
10
9
|
//# sourceMappingURL=trace.d.ts.map
|
package/dist/local/trace.js
CHANGED
|
@@ -1,23 +1,23 @@
|
|
|
1
|
+
//#region src/local/trace.ts
|
|
1
2
|
function newTrace() {
|
|
2
|
-
|
|
3
|
+
return {};
|
|
3
4
|
}
|
|
4
5
|
function trace(t, k, v) {
|
|
5
|
-
|
|
6
|
-
|
|
6
|
+
t[k] = v;
|
|
7
|
+
return v;
|
|
7
8
|
}
|
|
8
9
|
/** Creates a trace and runs a function with it. The function can record all its
|
|
9
|
-
|
|
10
|
+
* logs or traces using `trace` fn. */
|
|
10
11
|
async function withTrace(logger, fn) {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
throw e;
|
|
19
|
-
}
|
|
12
|
+
const t = newTrace();
|
|
13
|
+
try {
|
|
14
|
+
return await fn((k, v) => trace(t, k, v), t);
|
|
15
|
+
} catch (e) {
|
|
16
|
+
logger.error(`error ${e} while doing traced operation, state: ${JSON.stringify(t)}`);
|
|
17
|
+
throw e;
|
|
18
|
+
}
|
|
20
19
|
}
|
|
21
20
|
|
|
22
|
-
|
|
23
|
-
|
|
21
|
+
//#endregion
|
|
22
|
+
export { withTrace };
|
|
23
|
+
//# sourceMappingURL=trace.js.map
|
package/dist/local/trace.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"trace.js","sources":["../../src/local/trace.ts"],"sourcesContent":["import type { MiLogger } from \"@milaboratories/ts-helpers\";\n\n/** Records all inputs and outputs of one's choice, so if the error happened\n * one can check how it was by just printing this structure. */\nexport type Trace = Record<string, any>;\n\nexport function newTrace(): Trace {\n return {};\n}\n\nexport function trace(t: Trace, k: string, v: any) {\n t[k] = v;\n return v;\n}\n\n/** Creates a trace and runs a function with it. The function can record all its\n * logs or traces using `trace` fn. */\nexport async function withTrace<T>(\n logger: MiLogger,\n fn: (trace: (k: string, v: any) => typeof v, t: Trace) => Promise<T>,\n): Promise<T> {\n const t = newTrace();\n try {\n const result = await fn((k: string, v: any) => trace(t, k, v), t);\n return result;\n } catch (e: any) {\n logger.error(`error ${e} while doing traced operation, state: ${JSON.stringify(t)}`);\n throw e;\n }\n}\n"],"
|
|
1
|
+
{"version":3,"file":"trace.js","names":[],"sources":["../../src/local/trace.ts"],"sourcesContent":["import type { MiLogger } from \"@milaboratories/ts-helpers\";\n\n/** Records all inputs and outputs of one's choice, so if the error happened\n * one can check how it was by just printing this structure. */\nexport type Trace = Record<string, any>;\n\nexport function newTrace(): Trace {\n return {};\n}\n\nexport function trace(t: Trace, k: string, v: any) {\n t[k] = v;\n return v;\n}\n\n/** Creates a trace and runs a function with it. The function can record all its\n * logs or traces using `trace` fn. */\nexport async function withTrace<T>(\n logger: MiLogger,\n fn: (trace: (k: string, v: any) => typeof v, t: Trace) => Promise<T>,\n): Promise<T> {\n const t = newTrace();\n try {\n const result = await fn((k: string, v: any) => trace(t, k, v), t);\n return result;\n } catch (e: any) {\n logger.error(`error ${e} while doing traced operation, state: ${JSON.stringify(t)}`);\n throw e;\n }\n}\n"],"mappings":";AAMA,SAAgB,WAAkB;AAChC,QAAO,EAAE;;AAGX,SAAgB,MAAM,GAAU,GAAW,GAAQ;AACjD,GAAE,KAAK;AACP,QAAO;;;;AAKT,eAAsB,UACpB,QACA,IACY;CACZ,MAAM,IAAI,UAAU;AACpB,KAAI;AAEF,SADe,MAAM,IAAI,GAAW,MAAW,MAAM,GAAG,GAAG,EAAE,EAAE,EAAE;UAE1D,GAAQ;AACf,SAAO,MAAM,SAAS,EAAE,wCAAwC,KAAK,UAAU,EAAE,GAAG;AACpF,QAAM"}
|
package/dist/package.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"package.cjs","names":[],"sources":["../package.json"],"sourcesContent":[""],"mappings":""}
|
package/dist/package.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"package.js","names":[],"sources":["../package.json"],"sourcesContent":[""],"mappings":""}
|