修改 lock 文件名称规则

This commit is contained in:
whyour 2025-01-16 00:35:47 +08:00
parent f9b9543a6d
commit 4fa5fa2014

View File

@ -1,27 +1,39 @@
import { lock } from 'proper-lockfile'; import { lock } from 'proper-lockfile';
import os from 'os';
import path from 'path';
import { writeFile, open } from 'fs/promises'; import { writeFile, open } from 'fs/promises';
import { fileExist } from '../config/util'; import { fileExist } from '../config/util';
function getUniqueLockPath(filePath: string) {
const sanitizedPath = filePath
.replace(/[<>:"/\\|?*]/g, '_')
.replace(/^_/, '');
return path.join(os.tmpdir(), `${sanitizedPath}.ql_lock`);
}
export async function writeFileWithLock( export async function writeFileWithLock(
path: string, filePath: string,
content: string | Buffer, content: string | Buffer,
options: Parameters<typeof writeFile>[2] = {}, options: Parameters<typeof writeFile>[2] = {},
) { ) {
if (typeof options === 'string') { if (typeof options === 'string') {
options = { encoding: options }; options = { encoding: options };
} }
if (!(await fileExist(path))) { if (!(await fileExist(filePath))) {
const fileHandle = await open(path, 'w'); const fileHandle = await open(filePath, 'w');
fileHandle.close(); fileHandle.close();
} }
const release = await lock(path, { const lockfilePath = getUniqueLockPath(filePath);
const release = await lock(filePath, {
retries: { retries: {
retries: 10, retries: 10,
factor: 2, factor: 2,
minTimeout: 100, minTimeout: 100,
maxTimeout: 3000, maxTimeout: 3000,
}, },
lockfilePath,
}); });
await writeFile(path, content, { encoding: 'utf8', ...options }); await writeFile(filePath, content, { encoding: 'utf8', ...options });
await release(); await release();
} }