fix: make run log creation atomic
This commit is contained in:
1 parent
4cb61399b1
commit
d2e94959e0
3 files changed
+186
-7
No files matched your search
@@ -1,4 +1,4 @@
|
||||
import { mkdirSync } from "node:fs";
|
||||
import { mkdirSync, rmSync, writeFileSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
import { DatabaseSync, type SQLInputValue } from "node:sqlite";
|
||||
import { buildThreadMethods } from "./database/threads";
|
||||
@@ -38,6 +38,15 @@ function nowIso() {
|
||||
return new Date().toISOString();
|
||||
}
|
||||
|
||||
function assertWritableDirectory(dir: string, label: string): void {
|
||||
const probePath = join(dir, `.agentcompany-${label}-write-probe-${process.pid}-${Date.now()}`);
|
||||
try {
|
||||
writeFileSync(probePath, "", { encoding: "utf8", flag: "wx" });
|
||||
} finally {
|
||||
rmSync(probePath, { force: true });
|
||||
}
|
||||
}
|
||||
|
||||
export interface AppDatabaseCoreModules {
|
||||
threads: ReturnType<typeof buildThreadMethods>;
|
||||
company: ReturnType<typeof buildCompanyMethods>;
|
||||
@@ -101,6 +110,7 @@ export class AppDatabaseBase {
|
||||
this.appVersion = options.appVersion ?? "unknown";
|
||||
mkdirSync(rootDir, { recursive: true });
|
||||
mkdirSync(this.logsDir, { recursive: true });
|
||||
assertWritableDirectory(this.logsDir, "logs");
|
||||
mkdirSync(this.artifactsDir, { recursive: true });
|
||||
mkdirSync(this.backupsDir, { recursive: true });
|
||||
this.vault = new SecretVault(rootDir, { isPackaged: options.isPackaged });
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { randomUUID } from "node:crypto";
|
||||
import { writeFileSync } from "node:fs";
|
||||
import { rmSync, writeFileSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
import type { DatabaseSync, SQLInputValue } from "node:sqlite";
|
||||
import type { ConnectorId, RunRecord, RunUsageStatus } from "@shared/types";
|
||||
@@ -24,6 +24,14 @@ export function buildRunMethods(
|
||||
logsDir: string;
|
||||
},
|
||||
) {
|
||||
function createEmptyRunLog(logPath: string): void {
|
||||
writeFileSync(logPath, "", { encoding: "utf8", flag: "wx" });
|
||||
}
|
||||
|
||||
function cleanupEmptyRunLog(logPath: string): void {
|
||||
rmSync(logPath, { force: true });
|
||||
}
|
||||
|
||||
function insertRunRow(input: {
|
||||
id: string;
|
||||
companyId: string;
|
||||
@@ -85,10 +93,15 @@ export function buildRunMethods(
|
||||
const id = input.id ?? randomUUID();
|
||||
const ts = nowIso();
|
||||
const logPath = join(deps.logsDir, `${id}.log`);
|
||||
transaction(() => {
|
||||
insertRunRow({ ...input, id, logPath, ts });
|
||||
});
|
||||
writeFileSync(logPath, "", "utf8");
|
||||
createEmptyRunLog(logPath);
|
||||
try {
|
||||
transaction(() => {
|
||||
insertRunRow({ ...input, id, logPath, ts });
|
||||
});
|
||||
} catch (error) {
|
||||
cleanupEmptyRunLog(logPath);
|
||||
throw error;
|
||||
}
|
||||
return { id, logPath };
|
||||
},
|
||||
|
||||
@@ -103,6 +116,7 @@ export function buildRunMethods(
|
||||
const id = input.id ?? randomUUID();
|
||||
const ts = nowIso();
|
||||
const logPath = join(deps.logsDir, `${id}.log`);
|
||||
createEmptyRunLog(logPath);
|
||||
try {
|
||||
transaction(() => {
|
||||
insertRunRow({ ...input, id, logPath, ts });
|
||||
@@ -112,12 +126,12 @@ export function buildRunMethods(
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
cleanupEmptyRunLog(logPath);
|
||||
if (error instanceof TaskCheckoutConflict) {
|
||||
return null;
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
writeFileSync(logPath, "", "utf8");
|
||||
return { id, logPath };
|
||||
},
|
||||
|
||||
|
||||
Reference in new issue
Block a user