fix(utils): handle file write failures with rollback
- Add error handling and rollback logic for unit file writes - Prevent partial file writes by removing created files on failure - Update tests to reflect new return type and error handling
This commit is contained in:
@@ -12,14 +12,20 @@ export async function generateUnitFiles(options: TimerOptions): Promise<void> {
|
||||
console.log(`\n===== ${name}.timer =====`);
|
||||
console.log(timerUnit);
|
||||
} else {
|
||||
const { servicePath, timerPath } = await writeUnitFiles(
|
||||
const result = await writeUnitFiles(
|
||||
name,
|
||||
serviceUnit,
|
||||
timerUnit,
|
||||
options,
|
||||
);
|
||||
console.log(`Service unit written to: ${servicePath}`);
|
||||
console.log(`Timer unit written to: ${timerPath}`);
|
||||
|
||||
if (result) {
|
||||
const { servicePath, timerPath } = result;
|
||||
console.log(`Service Unit geschrieben in: ${servicePath}`);
|
||||
console.log(`Timer Unit geschrieben in: ${timerPath}`);
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
|
||||
console.log(`\nℹ️ Hinweis:`);
|
||||
|
||||
|
@@ -24,7 +24,7 @@ Deno.test('writeUnitFiles schreibt .service und .timer korrekt', async () => {
|
||||
serviceContent,
|
||||
timerContent,
|
||||
options as TimerOptions,
|
||||
);
|
||||
) as { servicePath: string; timerPath: string };
|
||||
|
||||
// Überprüfe Pfade
|
||||
assertEquals(servicePath, join(tmp, 'test-backup.service'));
|
||||
|
@@ -1,4 +1,4 @@
|
||||
import { ensureDir } from 'https://deno.land/std@0.224.0/fs/mod.ts';
|
||||
import { ensureDir, exists } from 'https://deno.land/std@0.224.0/fs/mod.ts';
|
||||
import { join } from 'https://deno.land/std@0.224.0/path/mod.ts';
|
||||
import { TimerOptions } from '../types/mod.ts';
|
||||
|
||||
@@ -7,7 +7,7 @@ export async function writeUnitFiles(
|
||||
serviceContent: string,
|
||||
timerContent: string,
|
||||
options: TimerOptions,
|
||||
): Promise<{ servicePath: string; timerPath: string }> {
|
||||
): Promise<{ servicePath: string; timerPath: string } | undefined> {
|
||||
const basePath = resolveUnitTargetPath(options);
|
||||
|
||||
await ensureDir(basePath);
|
||||
@@ -15,8 +15,24 @@ export async function writeUnitFiles(
|
||||
const servicePath = join(basePath, `${name}.service`);
|
||||
const timerPath = join(basePath, `${name}.timer`);
|
||||
|
||||
await Deno.writeTextFile(servicePath, serviceContent);
|
||||
await Deno.writeTextFile(timerPath, timerContent);
|
||||
try {
|
||||
await Deno.writeTextFile(servicePath, serviceContent);
|
||||
await Deno.writeTextFile(timerPath, timerContent);
|
||||
} catch (error) {
|
||||
// Rollback: Remove any files that were written
|
||||
try {
|
||||
if (await exists(servicePath)) {
|
||||
await Deno.remove(servicePath);
|
||||
}
|
||||
if (await exists(timerPath)) {
|
||||
await Deno.remove(timerPath);
|
||||
}
|
||||
} catch (rollbackError) {
|
||||
console.error('Rollback fehlgeschlagen:', rollbackError);
|
||||
}
|
||||
console.error('Fehler beim Schreiben der Units:', error);
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return { servicePath, timerPath };
|
||||
}
|
||||
|
Reference in New Issue
Block a user