Bugfix: fix race conditions
This commit is contained in:
Vendored
+4
-27
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user