Bugfix: fix race conditions
This commit is contained in:
-19
@@ -1,4 +1,3 @@
|
||||
import { spawn } from "child_process";
|
||||
import watchFiles from "./watch/files";
|
||||
import watchFolders from "./watch/folders";
|
||||
import { TurboSyncConfigObject } from "../types";
|
||||
@@ -9,11 +8,7 @@ import fldFileToStrArr, {
|
||||
|
||||
const confFileProvidedJSON = process.argv[process.argv.length - 1];
|
||||
|
||||
global.SYNC_SUCCESS_EXIT_CODE = 32;
|
||||
global.CONFIG_DIR = process.cwd();
|
||||
global.SYNCING = false;
|
||||
|
||||
const REWATCH_TIMEOUT = 1000;
|
||||
|
||||
try {
|
||||
const configFileObject: TurboSyncConfigObject =
|
||||
@@ -70,17 +65,3 @@ try {
|
||||
console.log(error);
|
||||
process.exit();
|
||||
}
|
||||
|
||||
process.on("exit", (code) => {
|
||||
if (code == global.SYNC_SUCCESS_EXIT_CODE) {
|
||||
const args = process.argv;
|
||||
const cmd = args.shift();
|
||||
if (cmd) {
|
||||
spawn(cmd, args, {
|
||||
stdio: "inherit",
|
||||
});
|
||||
}
|
||||
} else {
|
||||
process.exit(0);
|
||||
}
|
||||
});
|
||||
|
||||
+17
-34
@@ -3,15 +3,29 @@ import delay from "../../utils/delay";
|
||||
import { SyncFilesFnParams } from "../../types";
|
||||
|
||||
import sync from "../../utils/sync";
|
||||
import SyncScheduler from "../../utils/sync-scheduler";
|
||||
|
||||
export default async function watchFiles({
|
||||
files,
|
||||
options,
|
||||
}: SyncFilesFnParams) {
|
||||
let timeout: any;
|
||||
const UPDATE_TIMEOUT = 1000;
|
||||
|
||||
try {
|
||||
const INTERVAL = options?.interval ? options.interval : UPDATE_TIMEOUT;
|
||||
|
||||
const scheduler = new SyncScheduler(
|
||||
(filePath, firstRun) =>
|
||||
sync({
|
||||
options,
|
||||
dirPath: filePath,
|
||||
dirs: files,
|
||||
isFiles: true,
|
||||
firstRun,
|
||||
}),
|
||||
INTERVAL
|
||||
);
|
||||
|
||||
for (let i = 0; i < files.length; i++) {
|
||||
const file = files[i];
|
||||
const filePath =
|
||||
@@ -61,26 +75,7 @@ export default async function watchFiles({
|
||||
interval: interval || 200,
|
||||
},
|
||||
(curr, prev) => {
|
||||
if (global.SYNCING) return;
|
||||
|
||||
const INTERVAL = options?.interval
|
||||
? options.interval
|
||||
: UPDATE_TIMEOUT;
|
||||
|
||||
clearTimeout(timeout);
|
||||
|
||||
timeout = setTimeout(() => {
|
||||
global.SYNCING = true;
|
||||
|
||||
sync({
|
||||
options,
|
||||
dirPath: filePath,
|
||||
dirs: files,
|
||||
isFiles: true,
|
||||
}).finally(() => {
|
||||
process.exit(global.SYNC_SUCCESS_EXIT_CODE);
|
||||
});
|
||||
}, INTERVAL);
|
||||
scheduler.schedule(filePath);
|
||||
}
|
||||
);
|
||||
}
|
||||
@@ -92,19 +87,7 @@ export default async function watchFiles({
|
||||
? lastUpdatedFile
|
||||
: lastUpdatedFile.path;
|
||||
|
||||
global.SYNCING = true;
|
||||
|
||||
await sync({
|
||||
dirPath: lastUpdatedFilePath,
|
||||
dirs: files,
|
||||
options,
|
||||
isFiles: true,
|
||||
firstRun: true,
|
||||
});
|
||||
|
||||
setTimeout(() => {
|
||||
global.SYNCING = false;
|
||||
}, UPDATE_TIMEOUT);
|
||||
scheduler.enqueue(lastUpdatedFilePath, true);
|
||||
} catch (error: any) {
|
||||
console.log("ERROR:", error.message);
|
||||
process.exit(0);
|
||||
|
||||
+8
-35
@@ -1,19 +1,17 @@
|
||||
import fs from "fs";
|
||||
import delay from "../../utils/delay";
|
||||
import { SyncFoldersFnParams } from "../../types";
|
||||
import sync from "../../utils/sync";
|
||||
import SyncScheduler from "../../utils/sync-scheduler";
|
||||
|
||||
export default async function watchFolders({
|
||||
folders,
|
||||
options,
|
||||
}: SyncFoldersFnParams) {
|
||||
let timeout: any;
|
||||
const UPDATE_TIMEOUT = 1000;
|
||||
|
||||
try {
|
||||
const dirs = folders;
|
||||
|
||||
console.log("global.SYNCING", global.SYNCING);
|
||||
console.log(`Now handling ${dirs.length} Directories`);
|
||||
|
||||
/**
|
||||
@@ -21,6 +19,11 @@ export default async function watchFolders({
|
||||
*/
|
||||
const INTERVAL = options?.interval ? options.interval : UPDATE_TIMEOUT;
|
||||
|
||||
const scheduler = new SyncScheduler(
|
||||
(dirPath, firstRun) => sync({ dirPath, dirs, options, firstRun }),
|
||||
INTERVAL
|
||||
);
|
||||
|
||||
for (let i = 0; i < dirs.length; i++) {
|
||||
const dir = dirs[i];
|
||||
|
||||
@@ -31,8 +34,6 @@ export default async function watchFolders({
|
||||
|
||||
const dirPath = typeof dir == "string" ? dir : dir.path;
|
||||
|
||||
console.log("global.SYNCING", global.SYNCING);
|
||||
|
||||
if (
|
||||
(typeof dir == "string" && !fs.existsSync(dirPath)) ||
|
||||
(typeof dir == "object" &&
|
||||
@@ -70,27 +71,10 @@ export default async function watchFolders({
|
||||
}
|
||||
|
||||
if (typeof dir == "string") {
|
||||
await delay();
|
||||
|
||||
fs.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;
|
||||
|
||||
sync({ dirPath, dirs, options }).finally(() => {
|
||||
process.exit(global.SYNC_SUCCESS_EXIT_CODE);
|
||||
});
|
||||
}, INTERVAL);
|
||||
scheduler.schedule(dirPath);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -104,18 +88,7 @@ export default async function watchFolders({
|
||||
? lastUpdatedDir
|
||||
: lastUpdatedDir.path;
|
||||
|
||||
global.SYNCING = true;
|
||||
|
||||
await sync({
|
||||
dirPath: lastUpdatedDirPath,
|
||||
dirs,
|
||||
options,
|
||||
firstRun: true,
|
||||
});
|
||||
|
||||
setTimeout(() => {
|
||||
global.SYNCING = false;
|
||||
}, UPDATE_TIMEOUT);
|
||||
scheduler.enqueue(lastUpdatedDirPath, true);
|
||||
} catch (error: any) {
|
||||
console.log("ERROR:", error.message);
|
||||
process.exit(0);
|
||||
|
||||
Reference in New Issue
Block a user