Files
t3code/apps/server/src/resourceTelemetry/ResourceMonitorBinary.ts
T
+36 de34391427 feat(orchestrator): introduce new orchestrator (#2829)
Co-authored-by: maria-rcks <[email protected]>
Co-authored-by: Bilal Bakr <[email protected]>
Co-authored-by: shivam <[email protected]>
Co-authored-by: Vitalii Yehorov <[email protected]>
Co-authored-by: Jake Leventhal <[email protected]>
Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
Co-authored-by: Alex Southwell <[email protected]>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Nicholas Wasmiller <[email protected]>
Co-authored-by: PB <[email protected]>
Co-authored-by: Exotic <[email protected]>
Co-authored-by: Yash Singh <[email protected]>
Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: Theo Browne <[email protected]>
Co-authored-by: Gabriel De Andrade <[email protected]>
Co-authored-by: Dara Adedeji <[email protected]>
Co-authored-by: scratchyone <[email protected]>
Co-authored-by: Dominic Roy <[email protected]>
Co-authored-by: chukfinley <[email protected]>
Co-authored-by: Primož Ajdišek <[email protected]>
Co-authored-by: benthecarman <[email protected]>
Co-authored-by: NaveDanan <[email protected]>
Co-authored-by: aaditagrawal <[email protected]>
Co-authored-by: Aditya Garud <[email protected]>
Co-authored-by: Nick Anisimov <[email protected]>
Co-authored-by: MacKinley Smith <[email protected]>
Co-authored-by: Yordis Prieto <[email protected]>
Co-authored-by: t3-code[bot] <269035359+t3-code[bot]@users.noreply.github.com>
Co-authored-by: AKolenda <[email protected]>
Co-authored-by: Guillermo Casanova <[email protected]>
Co-authored-by: Otavio Salvador <[email protected]>
Co-authored-by: Shirish Pothi <[email protected]>
Co-authored-by: Ishaan Kothari <[email protected]>
Co-authored-by: Bob Fowler <[email protected]>
Co-authored-by: Anton Bezdenezhnykh <[email protected]>
Co-authored-by: ValeraZSD <[email protected]>
Co-authored-by: Ephraim <[email protected]>
Co-authored-by: Ryan Ilano <[email protected]>
Co-authored-by: Alex <[email protected]>
Co-authored-by: maco <[email protected]>
Co-authored-by: Cursor <[email protected]>
Co-authored-by: Tristan Knight <[email protected]>
Co-authored-by: PR Batch Tester <[email protected]>
Co-authored-by: oliver <[email protected]>
Co-authored-by: kamkm <[email protected]>
Signed-off-by: Yordis Prieto <[email protected]>
2026-10-02 12:22:22 -07:00

227 lines
6.9 KiB
TypeScript

import {
HostProcessArchitecture,
HostProcessEnvironment,
HostProcessPlatform,
} from "@t3tools/shared/hostProcess";
import * as Context from "effect/Context";
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import * as Layer from "effect/Layer";
import * as Option from "effect/Option";
import * as Path from "effect/Path";
import * as Schema from "effect/Schema";
import * as ServerConfig from "../config.ts";
export class ResourceMonitorBinaryUnsupported extends Schema.TaggedError<ResourceMonitorBinaryUnsupported>()(
"ResourceMonitorBinaryUnsupported",
{
platform: Schema.String,
architecture: Schema.String,
},
) {
override get message(): string {
return `Resource monitoring is unsupported on ${this.platform}/${this.architecture}.`;
}
}
export class ResourceMonitorBinaryNotFound extends Schema.TaggedError<ResourceMonitorBinaryNotFound>()(
"ResourceMonitorBinaryNotFound",
{
platform: Schema.String,
architecture: Schema.String,
candidates: Schema.Array(Schema.String),
},
) {
override get message(): string {
return `Resource monitor binary was not found for ${this.platform}/${this.architecture}.`;
}
}
export class ResourceMonitorBinaryNotExecutable extends Schema.TaggedError<ResourceMonitorBinaryNotExecutable>()(
"ResourceMonitorBinaryNotExecutable",
{
path: Schema.String,
mode: Schema.Number,
},
) {
override get message(): string {
return `Resource monitor binary at '${this.path}' is not executable.`;
}
}
export type ResourceMonitorBinaryError =
| ResourceMonitorBinaryUnsupported
| ResourceMonitorBinaryNotFound
| ResourceMonitorBinaryNotExecutable;
export class ResourceMonitorBinary extends Context.Service<
ResourceMonitorBinary,
{
readonly resolve: Effect.Effect<string, ResourceMonitorBinaryError>;
}
>()("t3/resourceTelemetry/ResourceMonitorBinary") {}
function binaryName(platform: NodeJS.Platform): string {
return platform === "win32" ? "t3-resource-monitor.exe" : "t3-resource-monitor";
}
export type ResourceMonitorLinuxLibc = "gnu" | "musl";
function detectResourceMonitorLinuxLibc(): ResourceMonitorLinuxLibc {
try {
const report = process.report?.getReport() as
| {
readonly header?: {
readonly glibcVersionRuntime?: unknown;
};
}
| undefined;
return typeof report?.header?.glibcVersionRuntime === "string" ? "gnu" : "musl";
} catch {
return "musl";
}
}
export const ResourceMonitorHostLinuxLibc = Context.Reference<ResourceMonitorLinuxLibc>(
"t3/resourceTelemetry/ResourceMonitorHostLinuxLibc",
{
defaultValue: detectResourceMonitorLinuxLibc,
},
);
function resourceMonitorPlatformKey(
platform: NodeJS.Platform,
architecture: NodeJS.Architecture,
): string | undefined {
if (
(platform !== "darwin" && platform !== "linux" && platform !== "win32") ||
(architecture !== "arm64" && architecture !== "x64")
) {
return undefined;
}
return `${platform}-${architecture}`;
}
function resourceMonitorRustTarget(
platform: NodeJS.Platform,
architecture: NodeJS.Architecture,
linuxLibc?: ResourceMonitorLinuxLibc,
): string | undefined {
if (platform === "darwin") {
return architecture === "arm64"
? "aarch64-apple-darwin"
: architecture === "x64"
? "x86_64-apple-darwin"
: undefined;
}
if (platform === "linux") {
if (linuxLibc !== "gnu") {
return undefined;
}
return architecture === "arm64"
? "aarch64-unknown-linux-gnu"
: architecture === "x64"
? "x86_64-unknown-linux-gnu"
: undefined;
}
if (platform === "win32") {
return architecture === "arm64"
? "aarch64-pc-windows-msvc"
: architecture === "x64"
? "x86_64-pc-windows-msvc"
: undefined;
}
return undefined;
}
export const make = Effect.fn("resourceTelemetry.resourceMonitorBinary.make")(function* () {
const config = yield* ServerConfig.ServerConfig;
const fileSystem = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const platform = yield* HostProcessPlatform;
const architecture = yield* HostProcessArchitecture;
const environment = yield* HostProcessEnvironment;
const linuxLibc = platform === "linux" ? yield* ResourceMonitorHostLinuxLibc : undefined;
const executableName = binaryName(platform);
const platformKey = resourceMonitorPlatformKey(platform, architecture);
const rustTarget = resourceMonitorRustTarget(platform, architecture, linuxLibc);
const overrideCandidates = [
environment.T3CODE_RESOURCE_MONITOR_PATH,
config.resourceMonitorPath,
].filter((candidate): candidate is string => Boolean(candidate));
const bundledCandidates =
platformKey === undefined || rustTarget === undefined
? []
: [
path.resolve(import.meta.dirname, "resource-monitor", platformKey, executableName),
path.resolve(import.meta.dirname, "resource-monitor", executableName),
path.resolve(import.meta.dirname, "../resource-monitor", executableName),
path.resolve(
import.meta.dirname,
"../../../../native/resource-monitor/target",
rustTarget,
"release",
executableName,
),
path.resolve(
import.meta.dirname,
"../../../native/resource-monitor/target",
rustTarget,
"release",
executableName,
),
path.resolve(
import.meta.dirname,
"../../../../native/resource-monitor/target/release",
executableName,
),
path.resolve(
import.meta.dirname,
"../../../../native/resource-monitor/target/debug",
executableName,
),
];
if (overrideCandidates.length === 0 && bundledCandidates.length === 0) {
return ResourceMonitorBinary.of({
resolve: Effect.fail(
new ResourceMonitorBinaryUnsupported({
platform,
architecture,
}),
),
});
}
const candidates = [...overrideCandidates, ...bundledCandidates];
const resolve: ResourceMonitorBinary["Service"]["resolve"] = Effect.gen(function* () {
for (const candidate of candidates) {
const exists = yield* fileSystem.exists(candidate).pipe(Effect.orElseSucceed(() => false));
if (!exists) continue;
if (platform !== "win32") {
const stat = yield* fileSystem.stat(candidate).pipe(Effect.option);
if (Option.isSome(stat) && (stat.value.mode & 0o111) === 0) {
return yield* new ResourceMonitorBinaryNotExecutable({
path: candidate,
mode: stat.value.mode,
});
}
}
return candidate;
}
return yield* new ResourceMonitorBinaryNotFound({
platform,
architecture,
candidates,
});
});
return ResourceMonitorBinary.of({ resolve });
});
export const layer = Layer.effect(ResourceMonitorBinary, make());