mirror of
https://github.com/whyour/qinglong.git
synced 2026-02-12 14:05:38 +08:00
- Configure HTTP server timeouts (requestTimeout: 5min, headersTimeout: 2min, keepAliveTimeout: 65s) - Add better error logging in PUT /scripts endpoint - Improve writeFileWithLock error handling with descriptive messages and proper cleanup - Ensure lock is always released even on error Co-authored-by: whyour <22700758+whyour@users.noreply.github.com>
68 lines
1.7 KiB
TypeScript
68 lines
1.7 KiB
TypeScript
import { lock } from 'proper-lockfile';
|
|
import os from 'os';
|
|
import path from 'path';
|
|
import { writeFile, open, chmod } from 'fs/promises';
|
|
import { fileExist } from '../config/util';
|
|
import Logger from '../loaders/logger';
|
|
|
|
function getUniqueLockPath(filePath: string) {
|
|
const sanitizedPath = filePath
|
|
.replace(/[<>:"/\\|?*]/g, '_')
|
|
.replace(/^_/, '');
|
|
return path.join(os.tmpdir(), `${sanitizedPath}.ql_lock`);
|
|
}
|
|
|
|
export async function writeFileWithLock(
|
|
filePath: string,
|
|
content: string,
|
|
options: Parameters<typeof writeFile>[2] = {},
|
|
) {
|
|
if (typeof options === 'string') {
|
|
options = { encoding: options };
|
|
}
|
|
|
|
try {
|
|
if (!(await fileExist(filePath))) {
|
|
const fileHandle = await open(filePath, 'w');
|
|
await fileHandle.close();
|
|
}
|
|
} catch (error) {
|
|
throw new Error(`Failed to create file ${filePath}: ${error}`);
|
|
}
|
|
|
|
const lockfilePath = getUniqueLockPath(filePath);
|
|
let release: (() => Promise<void>) | null = null;
|
|
|
|
try {
|
|
release = await lock(filePath, {
|
|
retries: {
|
|
retries: 10,
|
|
factor: 2,
|
|
minTimeout: 100,
|
|
maxTimeout: 3000,
|
|
},
|
|
lockfilePath,
|
|
});
|
|
} catch (error) {
|
|
throw new Error(`Failed to acquire lock for ${filePath}: ${error}`);
|
|
}
|
|
|
|
try {
|
|
await writeFile(filePath, content, { encoding: 'utf8', ...options });
|
|
if (options?.mode) {
|
|
await chmod(filePath, options.mode);
|
|
}
|
|
} catch (error) {
|
|
throw new Error(`Failed to write to file ${filePath}: ${error}`);
|
|
} finally {
|
|
if (release) {
|
|
try {
|
|
await release();
|
|
} catch (error) {
|
|
// Log but don't throw on release failure
|
|
Logger.error(`Failed to release lock for ${filePath}:`, error);
|
|
}
|
|
}
|
|
}
|
|
}
|