Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ProcessLauncher.ts

74 lines3.2 KB

1import * as NodeChildProcessSpawner from "@effect/platform-node-shared/NodeChildProcessSpawner";
2import * as Context from "effect/Context";
3import type * as Duration from "effect/Duration";
4import * as Effect from "effect/Effect";
5import * as Layer from "effect/Layer";
6import * as Option from "effect/Option";
7import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
8import * as HostProcess from "@t3tools/shared/HostProcess";
9​
10import * as NonBlockingSpawner from "./NonBlockingSpawner.ts";
11​
12/**
13 * Exposes the active Linux process-launcher helper PID so callers such as port discovery can
14 * recognize direct children spawned through the helper instead of directly from the server.
15 */
16export class ProcessLauncher extends Context.Reference<{
17 readonly currentPid: Effect.Effect<number | undefined>;
18}>("t3/process/ProcessLauncher", {
19 defaultValue: () => ({ currentPid: Effect.undefined }),
20}) {}
21​
22// On Linux, libuv forks the whole server for every spawn and blocks the event loop while the
23// kernel copies its page tables, so commands are launched by a small long-lived helper instead.
24export const make = Effect.fn("ProcessLauncher.make")(function* (responseTimeout?: Duration.Input) {
25 const fallback = Context.make(
26 ChildProcessSpawner.ChildProcessSpawner,
27 yield* ChildProcessSpawner.ChildProcessSpawner,
28 ).pipe(Context.add(ProcessLauncher, { currentPid: Effect.undefined }));
29 if ((yield* HostProcess.Platform) !== "linux") return fallback;
30​
31 const helper = NodeChildProcessSpawner.layerProxyProcess({
32 executable: yield* HostProcess.ExecutablePath,
33 // A single executable cannot run `-e`, so it hosts the helper as a hidden subcommand of bin.ts.
34 args: (yield* HostProcess.IsExecutable) ? ["__process-launcher"] : undefined,
35 env: {
36 ...(yield* HostProcess.Environment),
37 ELECTRON_RUN_AS_NODE: "1",
38 NODE_OPTIONS: undefined,
39 // `node --watch` children would report their imports over the helper's IPC channel.
40 WATCH_REPORT_DEPENDENCIES: undefined,
41 },
42 responseTimeout,
43 });
44 // The trampoline keeps a hung cwd or PATH entry from stalling the helper, and with it every command.
45 // Fresh: the server has already built this layer around the forking spawner.
46 const launched = Layer.fresh(NonBlockingSpawner.layer).pipe(Layer.provideMerge(helper));
47 return yield* Layer.build(launched).pipe(
48 Effect.map((context) => {
49 const proxy = Context.get(context, NodeChildProcessSpawner.ProxyProcess);
50 return Context.make(
51 ChildProcessSpawner.ChildProcessSpawner,
52 Context.get(context, ChildProcessSpawner.ChildProcessSpawner),
53 ).pipe(
54 Context.add(ProcessLauncher, {
55 currentPid: Effect.map(proxy.currentPid, Option.getOrUndefined),
56 }),
57 );
58 }),
59 Effect.catch((error) =>
60 Effect.as(
61 Effect.logWarning("The process launcher did not start; commands fork the server.", error),
62 fallback,
63 ),
64 ),
65 );
66});
67​
68/**
69 * The server's one spawner: memoized, so every build site shares a single launcher. The provided
70 * spawner stays the fallback when the launcher cannot start.
71 */
72export const layerServer = Layer.effectContext(make()).pipe(
73 Layer.provide(NonBlockingSpawner.layer),
74);