fix: prevent the file sync from timing out after the default timeout

This commit is contained in:
James Murdza 2024-10-02 13:44:55 -07:00
parent 9d06808137
commit 7e48faa1b5

View File

@ -26,7 +26,7 @@ import {
} from "./fileoperations"; } from "./fileoperations";
import { LockManager } from "./utils"; import { LockManager } from "./utils";
import { Sandbox, Filesystem, FilesystemEvent, EntryInfo } from "e2b"; import { Sandbox, Filesystem, FilesystemEvent, EntryInfo, WatchHandle } from "e2b";
import { Terminal } from "./Terminal" import { Terminal } from "./Terminal"
@ -194,6 +194,7 @@ io.on("connection", async (socket) => {
const sandboxFiles = await getSandboxFiles(data.sandboxId); const sandboxFiles = await getSandboxFiles(data.sandboxId);
const projectDirectory = path.join(dirName, "projects", data.sandboxId); const projectDirectory = path.join(dirName, "projects", data.sandboxId);
const containerFiles = containers[data.sandboxId].files; const containerFiles = containers[data.sandboxId].files;
const fileWatchers: WatchHandle[] = [];
// Change the owner of the project directory to user // Change the owner of the project directory to user
const fixPermissions = async (projectDirectory: string) => { const fixPermissions = async (projectDirectory: string) => {
@ -243,9 +244,9 @@ io.on("connection", async (socket) => {
} }
// Start filesystem watcher for the project directory // Start filesystem watcher for the project directory
const watchDirectory = async (directory: string) => { const watchDirectory = async (directory: string): Promise<WatchHandle | undefined> => {
try { try {
await containerFiles.watch(directory, async (event: FilesystemEvent) => { return await containerFiles.watch(directory, async (event: FilesystemEvent) => {
try { try {
function removeDirName(path : string, dirName : string) { function removeDirName(path : string, dirName : string) {
@ -337,14 +338,16 @@ io.on("connection", async (socket) => {
} catch (error) { } catch (error) {
console.error(`Error handling ${event.type} event for ${event.name}:`, error); console.error(`Error handling ${event.type} event for ${event.name}:`, error);
} }
}) }, { "timeout": 0 } )
} catch (error) { } catch (error) {
console.error(`Error watching filesystem:`, error); console.error(`Error watching filesystem:`, error);
} }
}; };
// Watch the project directory // Watch the project directory
await watchDirectory(projectDirectory); const handle = await watchDirectory(projectDirectory);
// Keep track of watch handlers to close later
if (handle) fileWatchers.push(handle);
// Watch all subdirectories of the project directory, but not deeper // Watch all subdirectories of the project directory, but not deeper
// This also means directories created after the container is created won't be watched // This also means directories created after the container is created won't be watched
@ -352,7 +355,9 @@ io.on("connection", async (socket) => {
await Promise.all(dirContent.map(async (item : EntryInfo) => { await Promise.all(dirContent.map(async (item : EntryInfo) => {
if (item.type === "dir") { if (item.type === "dir") {
console.log("Watching " + item.path); console.log("Watching " + item.path);
await watchDirectory(item.path); // Keep track of watch handlers to close later
const handle = await watchDirectory(item.path);
if (handle) fileWatchers.push(handle);
} }
})) }))
@ -820,6 +825,11 @@ io.on("connection", async (socket) => {
connections[data.sandboxId]--; connections[data.sandboxId]--;
} }
// Stop watching file changes in the container
Promise.all(fileWatchers.map(async (handle : WatchHandle) => {
await handle.close();
}));
if (data.isOwner && connections[data.sandboxId] <= 0) { if (data.isOwner && connections[data.sandboxId] <= 0) {
socket.broadcast.emit( socket.broadcast.emit(
"disableAccess", "disableAccess",