Bugfix: fix race conditions
This commit is contained in:
Vendored
+13
@@ -0,0 +1,13 @@
|
||||
type SyncTask = (dirPath: string, firstRun?: boolean) => Promise<void>;
|
||||
export default class SyncScheduler {
|
||||
private readonly task;
|
||||
private readonly debounceMs;
|
||||
private pending;
|
||||
private timers;
|
||||
private flushing;
|
||||
constructor(task: SyncTask, debounceMs: number);
|
||||
schedule(dirPath: string): void;
|
||||
enqueue(dirPath: string, firstRun?: boolean): void;
|
||||
private flush;
|
||||
}
|
||||
export {};
|
||||
Vendored
+68
@@ -0,0 +1,68 @@
|
||||
"use strict";
|
||||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
||||
return new (P || (P = Promise))(function (resolve, reject) {
|
||||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
||||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
||||
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||||
});
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
class SyncScheduler {
|
||||
constructor(task, debounceMs) {
|
||||
this.task = task;
|
||||
this.debounceMs = debounceMs;
|
||||
this.pending = new Map();
|
||||
this.timers = new Map();
|
||||
this.flushing = false;
|
||||
}
|
||||
schedule(dirPath) {
|
||||
const existing = this.timers.get(dirPath);
|
||||
if (existing)
|
||||
clearTimeout(existing);
|
||||
const timer = setTimeout(() => {
|
||||
this.timers.delete(dirPath);
|
||||
this.pending.set(dirPath, false);
|
||||
void this.flush();
|
||||
}, this.debounceMs);
|
||||
this.timers.set(dirPath, timer);
|
||||
}
|
||||
enqueue(dirPath, firstRun) {
|
||||
const existing = this.timers.get(dirPath);
|
||||
if (existing) {
|
||||
clearTimeout(existing);
|
||||
this.timers.delete(dirPath);
|
||||
}
|
||||
this.pending.set(dirPath, !!firstRun);
|
||||
void this.flush();
|
||||
}
|
||||
flush() {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
if (this.flushing)
|
||||
return;
|
||||
this.flushing = true;
|
||||
try {
|
||||
while (this.pending.size > 0) {
|
||||
const batch = [...this.pending.entries()];
|
||||
this.pending.clear();
|
||||
for (const [dirPath, firstRun] of batch) {
|
||||
try {
|
||||
yield this.task(dirPath, firstRun);
|
||||
}
|
||||
catch (error) {
|
||||
console.log("ERROR:", error.message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
finally {
|
||||
this.flushing = false;
|
||||
if (this.pending.size > 0) {
|
||||
void this.flush();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
exports.default = SyncScheduler;
|
||||
Vendored
+69
-3
@@ -15,10 +15,66 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.default = sync;
|
||||
const fs_1 = __importDefault(require("fs"));
|
||||
const path_1 = __importDefault(require("path"));
|
||||
const os_1 = __importDefault(require("os"));
|
||||
const util_1 = __importDefault(require("util"));
|
||||
const crypto_1 = __importDefault(require("crypto"));
|
||||
const child_process_1 = require("child_process");
|
||||
const grab_dir_names_1 = __importDefault(require("./grab-dir-names"));
|
||||
const grab_folders_files_string_paths_1 = require("./grab-folders-files-string-paths");
|
||||
const delay_1 = __importDefault(require("./delay"));
|
||||
const execPromise = util_1.default.promisify(child_process_1.exec);
|
||||
const LOCK_STALE_MS = 5 * 60 * 1000;
|
||||
const LOCK_RETRY_MS = 100;
|
||||
function lockPathFor(dirPath) {
|
||||
const hash = crypto_1.default
|
||||
.createHash("sha1")
|
||||
.update(path_1.default.resolve(dirPath))
|
||||
.digest("hex")
|
||||
.slice(0, 16);
|
||||
return path_1.default.join(os_1.default.tmpdir(), `turbosync-${hash}.lock`);
|
||||
}
|
||||
function acquireLock(lockPath) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
while (true) {
|
||||
try {
|
||||
const fd = fs_1.default.openSync(lockPath, "wx");
|
||||
fs_1.default.writeFileSync(fd, String(process.pid));
|
||||
fs_1.default.closeSync(fd);
|
||||
return;
|
||||
}
|
||||
catch (error) {
|
||||
if (error.code !== "EEXIST")
|
||||
throw error;
|
||||
try {
|
||||
const { mtimeMs } = fs_1.default.statSync(lockPath);
|
||||
if (Date.now() - mtimeMs > LOCK_STALE_MS) {
|
||||
fs_1.default.unlinkSync(lockPath);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
catch (_a) {
|
||||
continue;
|
||||
}
|
||||
yield (0, delay_1.default)(LOCK_RETRY_MS);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
function acquireLocks(lockPaths) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
for (const lockPath of lockPaths) {
|
||||
yield acquireLock(lockPath);
|
||||
}
|
||||
});
|
||||
}
|
||||
function releaseLocks(lockPaths) {
|
||||
for (const lockPath of lockPaths) {
|
||||
try {
|
||||
fs_1.default.unlinkSync(lockPath);
|
||||
}
|
||||
catch (_a) { }
|
||||
}
|
||||
}
|
||||
function sync(_a) {
|
||||
return __awaiter(this, arguments, void 0, function* ({ options, dirs, dirPath, isFiles, firstRun, }) {
|
||||
var _b, _c;
|
||||
@@ -78,9 +134,19 @@ function sync(_a) {
|
||||
}
|
||||
allCommandsArr.push(cmdArray);
|
||||
}
|
||||
yield Promise.all(allCommandsArr.map((cmdArr) => {
|
||||
return execPromise(cmdArr.join(" "));
|
||||
}));
|
||||
const lockPaths = [dirPath, ...dstDirs.map((dr) => (0, grab_folders_files_string_paths_1.fldFileToStr)(dr))]
|
||||
.filter((pth) => Boolean(pth))
|
||||
.map((pth) => lockPathFor(pth))
|
||||
.sort();
|
||||
yield acquireLocks(lockPaths);
|
||||
try {
|
||||
yield Promise.all(allCommandsArr.map((cmdArr) => {
|
||||
return execPromise(cmdArr.join(" "));
|
||||
}));
|
||||
}
|
||||
finally {
|
||||
releaseLocks(lockPaths);
|
||||
}
|
||||
console.log(`${dirPath} Folder Sync Complete. Exiting ...`);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user