41 lines
1.3 KiB
JavaScript
41 lines
1.3 KiB
JavaScript
"use strict";
|
|
|
|
/*
|
|
* 前回実行が失敗して残った、アップロード未完了のローカル一時ファイル
|
|
* (pg_dumpの分割ファイル・設定アーカイブ)を起動時に削除する。
|
|
* 削除前提でリトライ機構は無いため、放置するとディスクを圧迫し続ける。
|
|
*/
|
|
|
|
const fs = require("fs");
|
|
const path = require("path");
|
|
|
|
function escapeRegExp(s) {
|
|
return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
}
|
|
|
|
function cleanupStaleLocalFiles({ outputDir, hostname, logger }) {
|
|
if (!fs.existsSync(outputDir)) {
|
|
return [];
|
|
}
|
|
|
|
const hostPattern = escapeRegExp(hostname);
|
|
const patterns = [
|
|
new RegExp(`^\\[${hostPattern}\\]postgres-.*\\.dmp.*$`),
|
|
new RegExp(`^\\[${hostPattern}\\]config-.*\\.tar\\.gz$`),
|
|
];
|
|
|
|
const removed = [];
|
|
for (const name of fs.readdirSync(outputDir)) {
|
|
if (!patterns.some((p) => p.test(name))) continue;
|
|
|
|
const filePath = path.join(outputDir, name);
|
|
const size = fs.statSync(filePath).size;
|
|
fs.unlinkSync(filePath);
|
|
logger.warn(`前回実行の残骸ローカルファイルを削除: ${name} (${size}bytes)`);
|
|
removed.push(filePath);
|
|
}
|
|
return removed;
|
|
}
|
|
|
|
module.exports = { cleanupStaleLocalFiles };
|