Bugfix: fix race conditions

This commit is contained in:
2026-08-10 11:07:39 +01:00
parent 44c5c19050
commit 9eae95e391
14 changed files with 326 additions and 175 deletions
+11 -29
View File
@@ -16,11 +16,19 @@ exports.default = watchFiles;
const fs_1 = __importDefault(require("fs"));
const delay_1 = __importDefault(require("../../utils/delay"));
const sync_1 = __importDefault(require("../../utils/sync"));
const sync_scheduler_1 = __importDefault(require("../../utils/sync-scheduler"));
function watchFiles(_a) {
return __awaiter(this, arguments, void 0, function* ({ files, options, }) {
let timeout;
const UPDATE_TIMEOUT = 1000;
try {
const INTERVAL = (options === null || options === void 0 ? void 0 : options.interval) ? options.interval : UPDATE_TIMEOUT;
const scheduler = new sync_scheduler_1.default((filePath, firstRun) => (0, sync_1.default)({
options,
dirPath: filePath,
dirs: files,
isFiles: true,
firstRun,
}), INTERVAL);
for (let i = 0; i < files.length; i++) {
const file = files[i];
const filePath = typeof file == "string" ? file : (file === null || file === void 0 ? void 0 : file.path) ? file.path : null;
@@ -61,23 +69,7 @@ function watchFiles(_a) {
fs_1.default.watchFile(filePath, {
interval: interval || 200,
}, (curr, prev) => {
if (global.SYNCING)
return;
const INTERVAL = (options === null || options === void 0 ? void 0 : options.interval)
? options.interval
: UPDATE_TIMEOUT;
clearTimeout(timeout);
timeout = setTimeout(() => {
global.SYNCING = true;
(0, sync_1.default)({
options,
dirPath: filePath,
dirs: files,
isFiles: true,
}).finally(() => {
process.exit(global.SYNC_SUCCESS_EXIT_CODE);
});
}, INTERVAL);
scheduler.schedule(filePath);
});
}
}
@@ -85,17 +77,7 @@ function watchFiles(_a) {
const lastUpdatedFilePath = typeof lastUpdatedFile == "string"
? lastUpdatedFile
: lastUpdatedFile.path;
global.SYNCING = true;
yield (0, sync_1.default)({
dirPath: lastUpdatedFilePath,
dirs: files,
options,
isFiles: true,
firstRun: true,
});
setTimeout(() => {
global.SYNCING = false;
}, UPDATE_TIMEOUT);
scheduler.enqueue(lastUpdatedFilePath, true);
}
catch (error) {
console.log("ERROR:", error.message);
+4 -27
View File
@@ -14,20 +14,19 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = watchFolders;
const fs_1 = __importDefault(require("fs"));
const delay_1 = __importDefault(require("../../utils/delay"));
const sync_1 = __importDefault(require("../../utils/sync"));
const sync_scheduler_1 = __importDefault(require("../../utils/sync-scheduler"));
function watchFolders(_a) {
return __awaiter(this, arguments, void 0, function* ({ folders, options, }) {
let timeout;
const UPDATE_TIMEOUT = 1000;
try {
const dirs = folders;
console.log("global.SYNCING", global.SYNCING);
console.log(`Now handling ${dirs.length} Directories`);
/**
* # Watch Directories
*/
const INTERVAL = (options === null || options === void 0 ? void 0 : options.interval) ? options.interval : UPDATE_TIMEOUT;
const scheduler = new sync_scheduler_1.default((dirPath, firstRun) => (0, sync_1.default)({ dirPath, dirs, options, firstRun }), INTERVAL);
for (let i = 0; i < dirs.length; i++) {
const dir = dirs[i];
if (!dir) {
@@ -35,7 +34,6 @@ function watchFolders(_a) {
continue;
}
const dirPath = typeof dir == "string" ? dir : dir.path;
console.log("global.SYNCING", global.SYNCING);
if ((typeof dir == "string" && !fs_1.default.existsSync(dirPath)) ||
(typeof dir == "object" &&
dir.path &&
@@ -64,21 +62,9 @@ function watchFolders(_a) {
}
}
if (typeof dir == "string") {
yield (0, delay_1.default)();
fs_1.default.watch(dirPath, { recursive: true }, (evt, fileName) => {
console.log("Folder Changed", evt, fileName);
if (global.SYNCING) {
console.log("Existing Sync found. Returning ...");
return;
}
clearTimeout(timeout);
timeout = setTimeout(() => {
console.log("Folder Syncing in progress ...");
global.SYNCING = true;
(0, sync_1.default)({ dirPath, dirs, options }).finally(() => {
process.exit(global.SYNC_SUCCESS_EXIT_CODE);
});
}, INTERVAL);
scheduler.schedule(dirPath);
});
}
}
@@ -89,16 +75,7 @@ function watchFolders(_a) {
const lastUpdatedDirPath = typeof lastUpdatedDir == "string"
? lastUpdatedDir
: lastUpdatedDir.path;
global.SYNCING = true;
yield (0, sync_1.default)({
dirPath: lastUpdatedDirPath,
dirs,
options,
firstRun: true,
});
setTimeout(() => {
global.SYNCING = false;
}, UPDATE_TIMEOUT);
scheduler.enqueue(lastUpdatedDirPath, true);
}
catch (error) {
console.log("ERROR:", error.message);