History

Watch Git trace hooks with file events

- Switch Git trace2 monitoring from polling to filesystem watch - Parse trace chunks with schema decoding and keep hook progress flowing - Update test expectations for hook output ordering

Julius Marminge committed 8 months ago

Commit 7eb93a11 parent c4748962 files · +35 −46Browse files

Unified · Split

2 files
  1. apps/server/src/git/Layers/GitCore.ts +31 −45
  2. apps/server/src/git/Layers/GitManager.test.ts +4 −1
apps/server/src/git/Layers/GitCore.ts+31−45
@@ -1,7 +1,5 @@
11 import { randomUUID } from "node:crypto";
2−import { readFile, unlink } from "node:fs/promises";
32 import { tmpdir } from "node:os";
4−import { join } from "node:path";
53
64 import {
75 Cache,
@@ -32,7 +30,6 @@ import { ServerConfig } from "../../config.ts"; · 24 unmodified lines
3230
3331 const DEFAULT_TIMEOUT_MS = 30_000;
3432 const DEFAULT_MAX_OUTPUT_BYTES = 1_000_000;
35−const TRACE2_POLL_INTERVAL_MS = 100;
3633 const STATUS_UPSTREAM_REFRESH_INTERVAL = Duration.seconds(15);
3734 const STATUS_UPSTREAM_REFRESH_TIMEOUT = Duration.seconds(5);
3835 const STATUS_UPSTREAM_REFRESH_CACHE_CAPACITY = 2_048;
@@ -300,6 +297,8 @@ function parseTraceRecord( · 261 unmodified lines
300297
301298 const createTrace2Monitor = Effect.fn(function* (
302299 input: Pick<ExecuteGitInput, "operation" | "cwd" | "args">,
300+ fileSystem: FileSystem.FileSystem,
301+ path: Path.Path,
303302 progress: ExecuteGitProgress | undefined,
304303 ): Effect.fn.Return<Trace2Monitor, never, Scope.Scope> {
305304 if (!progress?.onHookStarted && !progress?.onHookFinished) {
@@ -309,11 +308,12 @@ const createTrace2Monitor = Effect.fn(function* ( · 3 unmodified lines
309308 };
310309 }
311310
312− const traceFilePath = join(tmpdir(), `t3code-git-trace2-${process.pid}-${randomUUID()}.json`);
311+ const traceDir = tmpdir();
312+ const traceFileName = `t3code-git-trace2-${process.pid}-${randomUUID()}.json`;
313+ const traceFilePath = path.join(traceDir, traceFileName);
313314 const hookStartByChildKey = new Map<string, { hookName: string; startedAtMs: number }>();
314315 let processedChars = 0;
315316 let lineBuffer = "";
316− let polling = false;
317317
318318 const handleTraceLine = (line: string) =>
319319 Effect.gen(function* () {
@@ -370,40 +370,16 @@ const createTrace2Monitor = Effect.fn(function* ( · 50 unmodified lines
370370 }
371371 });
372372
373− const pollTraceFile = Effect.tryPromise({
374− try: async () => {
375− if (polling) {
376− return "";
377− }
378− polling = true;
379− try {
380− const contents = await readFile(traceFilePath, "utf8").catch((error: unknown) => {
381− const code =
382− typeof error === "object" && error !== null ? (error as { code?: unknown }).code : null;
383− if (code === "ENOENT") {
384− return "";
385− }
386− throw error;
387− });
388− if (contents.length <= processedChars) {
389− return "";
390− }
391− const delta = contents.slice(processedChars);
392− processedChars = contents.length;
393− return delta;
394− } finally {
395− polling = false;
396− }
397− },
398− catch: () => undefined,
399− }).pipe(
373+ const readTraceDelta = fileSystem.readFileString(traceFilePath).pipe(
400374 Effect.catch(() => Effect.succeed("")),
401375 Effect.flatMap((delta) =>
402376 Effect.gen(function* () {
403− if (delta.length === 0) {
377+ if (delta.length <= processedChars) {
404378 return;
405379 }
406− lineBuffer += delta;
380+ const appended = delta.slice(processedChars);
381+ processedChars = delta.length;
382+ lineBuffer += appended;
407383 let newlineIndex = lineBuffer.indexOf("\n");
408384 while (newlineIndex >= 0) {
409385 const line = lineBuffer.slice(0, newlineIndex);
@@ -414,23 +390,28 @@ const createTrace2Monitor = Effect.fn(function* ( · 4 unmodified lines
414390 }),
415391 ),
416392 );
393+ const watchTraceFile = Stream.runForEach(fileSystem.watch(traceDir), (event) => {
394+ const eventPath = event.path;
395+ const isTargetTraceEvent =
396+ eventPath === traceFileName ||
397+ eventPath === traceFilePath ||
398+ path.resolve(traceDir, eventPath) === traceFilePath;
399+ if (!isTargetTraceEvent) {
400+ return Effect.void;
401+ }
402+ return readTraceDelta;
403+ }).pipe(Effect.ignoreCause({ log: true }));
417404
418− const interval = setInterval(() => {
419− void Effect.runPromise(pollTraceFile);
420− }, TRACE2_POLL_INTERVAL_MS);
405+ yield* Effect.forkScoped(watchTraceFile);
421406
422407 yield* Effect.addFinalizer(() =>
423408 Effect.gen(function* () {
424− clearInterval(interval);
425− yield* pollTraceFile;
409+ yield* readTraceDelta;
426410 const finalLine = lineBuffer.trim();
427411 if (finalLine.length > 0) {
428412 yield* handleTraceLine(finalLine);
429413 }
430− yield* Effect.tryPromise({
431− try: () => unlink(traceFilePath),
432− catch: () => undefined,
433− }).pipe(Effect.ignore);
414+ yield* fileSystem.remove(traceFilePath).pipe(Effect.catch(() => Effect.void));
434415 }),
435416 );
436417
@@ -438,7 +419,7 @@ const createTrace2Monitor = Effect.fn(function* ( · 1 unmodified line
438419 env: {
439420 GIT_TRACE2_EVENT: traceFilePath,
440421 },
441− flush: pollTraceFile,
422+ flush: readTraceDelta,
442423 };
443424 });
444425
@@ -520,7 +501,12 @@ export const makeGitCore = (options?: { executeOverride?: GitCoreShape["execute" · 75 unmodified lines
520501 const maxOutputBytes = input.maxOutputBytes ?? DEFAULT_MAX_OUTPUT_BYTES;
521502
522503 const commandEffect = Effect.gen(function* () {
523− const trace2Monitor = yield* createTrace2Monitor(commandInput, input.progress);
504+ const trace2Monitor = yield* createTrace2Monitor(
505+ commandInput,
506+ fileSystem,
507+ path,
508+ input.progress,
509+ );
524510 const child = yield* commandSpawner
525511 .spawn(
526512 ChildProcess.make("git", commandInput.args, {
apps/server/src/git/Layers/GitManager.test.ts+4−1
@@ -1996,9 +1996,12 @@ it.layer(GitManagerTestLayer)("GitManager", (it) => { · 1995 unmodified lines
19961996 }),
19971997 expect.objectContaining({
19981998 kind: "hook_output",
1999− hookName: "pre-commit",
20001999 text: "hook: start",
20012000 }),
2001+ expect.objectContaining({
2002+ kind: "hook_output",
2003+ text: "hook: end",
2004+ }),
20022005 expect.objectContaining({
20032006 kind: "hook_finished",
20042007 hookName: "pre-commit",

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t