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

ResourceMonitorBinary.ts

217 lines6.9 KB

1import * as HostProcess from "@t3tools/shared/HostProcess";
2import * as Context from "effect/Context";
3import * as Effect from "effect/Effect";
4import * as FileSystem from "effect/FileSystem";
5import * as Layer from "effect/Layer";
6import * as Option from "effect/Option";
7import * as Path from "effect/Path";
8import * as Schema from "effect/Schema";
9​
10import * as ServerConfig from "../config.ts";
11​
12export class ResourceMonitorBinaryUnsupported extends Schema.TaggedError<ResourceMonitorBinaryUnsupported>()(
13 "ResourceMonitorBinaryUnsupported",
14 {
15 platform: Schema.String,
16 architecture: Schema.String,
17 },
18) {
19 override get message(): string {
20 return `Resource monitoring is unsupported on ${this.platform}/${this.architecture}.`;
21 }
23​
24export class ResourceMonitorBinaryNotFound extends Schema.TaggedError<ResourceMonitorBinaryNotFound>()(
25 "ResourceMonitorBinaryNotFound",
26 {
27 platform: Schema.String,
28 architecture: Schema.String,
29 candidates: Schema.Array(Schema.String),
30 },
31) {
32 override get message(): string {
33 return `Resource monitor binary was not found for ${this.platform}/${this.architecture}.`;
34 }
36​
37export class ResourceMonitorBinaryNotExecutable extends Schema.TaggedError<ResourceMonitorBinaryNotExecutable>()(
38 "ResourceMonitorBinaryNotExecutable",
39 {
40 path: Schema.String,
41 mode: Schema.Number,
42 },
43) {
44 override get message(): string {
45 return `Resource monitor binary at '${this.path}' is not executable.`;
46 }
48​
49export type ResourceMonitorBinaryError =
50 | ResourceMonitorBinaryUnsupported
51 | ResourceMonitorBinaryNotFound
52 | ResourceMonitorBinaryNotExecutable;
53​
54export class ResourceMonitorBinary extends Context.Service<
55 ResourceMonitorBinary,
56 {
57 readonly resolve: Effect.Effect<string, ResourceMonitorBinaryError>;
58 }
59>()("t3/resourceTelemetry/ResourceMonitorBinary") {}
60​
61function binaryName(platform: NodeJS.Platform): string {
62 return platform === "win32" ? "t3-resource-monitor.exe" : "t3-resource-monitor";
64​
65export type ResourceMonitorLinuxLibc = "gnu" | "musl";
66​
67function detectResourceMonitorLinuxLibc(): ResourceMonitorLinuxLibc {
68 try {
69 const report = process.report?.getReport() as
70 | {
71 readonly header?: {
72 readonly glibcVersionRuntime?: unknown;
73 };
74 }
75 | undefined;
76 return typeof report?.header?.glibcVersionRuntime === "string" ? "gnu" : "musl";
77 } catch {
78 return "musl";
79 }
81​
82export const ResourceMonitorHostLinuxLibc = Context.Reference<ResourceMonitorLinuxLibc>(
83 "t3/resourceTelemetry/ResourceMonitorHostLinuxLibc",
84 {
85 defaultValue: detectResourceMonitorLinuxLibc,
86 },
87);
88​
89function resourceMonitorPlatformKey(
90 platform: NodeJS.Platform,
91 architecture: NodeJS.Architecture,
92): string | undefined {
93 if (
94 (platform !== "darwin" && platform !== "linux" && platform !== "win32") ||
95 (architecture !== "arm64" && architecture !== "x64")
96 ) {
97 return undefined;
98 }
99 return `${platform}-${architecture}`;
101​
102function resourceMonitorRustTarget(
103 platform: NodeJS.Platform,
104 architecture: NodeJS.Architecture,
105 linuxLibc?: ResourceMonitorLinuxLibc,
106): string | undefined {
107 if (platform === "darwin") {
108 return architecture === "arm64"
109 ? "aarch64-apple-darwin"
110 : architecture === "x64"
111 ? "x86_64-apple-darwin"
112 : undefined;
113 }
114 if (platform === "linux") {
115 if (linuxLibc !== "gnu") {
116 return undefined;
117 }
118 return architecture === "arm64"
119 ? "aarch64-unknown-linux-gnu"
120 : architecture === "x64"
121 ? "x86_64-unknown-linux-gnu"
122 : undefined;
123 }
124 if (platform === "win32") {
125 return architecture === "arm64"
126 ? "aarch64-pc-windows-msvc"
127 : architecture === "x64"
128 ? "x86_64-pc-windows-msvc"
129 : undefined;
130 }
131 return undefined;
133​
134export const make = Effect.fn("resourceTelemetry.resourceMonitorBinary.make")(function* () {
135 const config = yield* ServerConfig.ServerConfig;
136 const fileSystem = yield* FileSystem.FileSystem;
137 const path = yield* Path.Path;
138 const platform = yield* HostProcess.Platform;
139 const architecture = yield* HostProcess.Architecture;
140 const environment = yield* HostProcess.Environment;
141 const linuxLibc = platform === "linux" ? yield* ResourceMonitorHostLinuxLibc : undefined;
142 const executableName = binaryName(platform);
143 const platformKey = resourceMonitorPlatformKey(platform, architecture);
144 const rustTarget = resourceMonitorRustTarget(platform, architecture, linuxLibc);
145 const overrideCandidates = [
146 environment.T3CODE_RESOURCE_MONITOR_PATH,
147 config.resourceMonitorPath,
148 ].filter((candidate): candidate is string => Boolean(candidate));
149 const cargoOutputDirectories =
150 rustTarget === undefined
151 ? []
152 : [
153 path.join("target", rustTarget, "release"),
154 path.join("target", "release"),
155 path.join("target", "debug"),
156 ];
157 const sourceCandidates = [
158 path.resolve(import.meta.dirname, "../../../../native/resource-monitor"),
159 path.resolve(import.meta.dirname, "../../../native/resource-monitor"),
160 ].flatMap((crateDirectory) =>
161 cargoOutputDirectories.map((directory) => path.join(crateDirectory, directory, executableName)),
162 );
163 const bundledCandidates =
164 platformKey === undefined || rustTarget === undefined
165 ? []
166 : [
167 path.resolve(import.meta.dirname, "resource-monitor", platformKey, executableName),
168 path.resolve(import.meta.dirname, "resource-monitor", executableName),
169 path.resolve(import.meta.dirname, "../resource-monitor", executableName),
170 ...sourceCandidates,
171 ];
172 if (overrideCandidates.length === 0 && bundledCandidates.length === 0) {
173 return ResourceMonitorBinary.of({
174 resolve: Effect.fail(
175 new ResourceMonitorBinaryUnsupported({
176 platform,
177 architecture,
178 }),
179 ),
180 });
181 }
182​
183 const candidates = [...overrideCandidates, ...bundledCandidates];
184​
185 const resolve: ResourceMonitorBinary["Service"]["resolve"] = Effect.gen(function* () {
186 for (const candidate of candidates) {
187 const exists = yield* fileSystem.exists(candidate).pipe(Effect.orElseSucceed(() => false));
188 if (!exists) continue;
189​
190 const stat = yield* fileSystem.stat(candidate).pipe(Effect.option);
191 if (Option.isSome(stat)) {
192 // Windows skips exec-bit checks, so directories would otherwise pass until spawn.
193 const unusable =
194 stat.value.type === "Directory" ||
195 (platform !== "win32" && (stat.value.mode & 0o111) === 0);
196 if (unusable) {
197 return yield* new ResourceMonitorBinaryNotExecutable({
198 path: candidate,
199 mode: stat.value.mode,
200 });
201 }
202 }
203​
204 return candidate;
205 }
206​
207 return yield* new ResourceMonitorBinaryNotFound({
208 platform,
209 architecture,
210 candidates,
211 });
212 });
213​
214 return ResourceMonitorBinary.of({ resolve });
215});
216​
217export const layer = Layer.effect(ResourceMonitorBinary, make());