Open-source, secure environment with real-world tools for enterprise-grade agents.
Python agentaiai-agentai-agentscode-interpretercopilotdevelopmentdevtoolsgptgpt-4javascriptllmnextjsopenaipythonreactsoftwaretypescript
Change close to stop for watch dir
Jakub Novak5c31902cc412
diff --git a/apps/web/src/code/python/basics/fs_watch.py b/apps/web/src/code/python/basics/fs_watch.pyindex 09c267b4f..12a2f0e59 100644--- a/apps/web/src/code/python/basics/fs_watch.py+++ b/apps/web/src/code/python/basics/fs_watch.py@@ -2,19 +2,9 @@ import timefrom e2b import Sandbox-watcher = None+sandbox = Sandbox()--def create_watcher(sandbox): # $HighlightLine- # Start filesystem watcher for the /home directory- watcher = sandbox.filesystem.watch_dir("/home") # $HighlightLine- watcher.add_event_listener(lambda event: print(event)) # $HighlightLine- watcher.start() # $HighlightLine---sandbox = Sandbox(template="base")--create_watcher(sandbox) # $HighlightLine+watcher = sandbox.files.watch("/home") # $HighlightLine# Create files in the /home directory inside the playground# We'll receive notifications for these events through the watcher we created above.@@ -22,7 +12,11 @@ for i in range(10):# `filesystem.write()` will trigger two events:# 1. 'Create' when the file is created# 2. 'Write' when the file is written to- sandbox.filesystem.write(f"/home/file{i}.txt", f"Hello World {i}!")+ sandbox.files.write(f"/home/file{i}.txt", f"Hello World {i}!")time.sleep(1)-sandbox.close()+for event in watcher.get():+ print(f"Event: {event.type} {event.name}") # $HighlightLine++watcher.stop()+sandbox.kill()diff --git a/packages/js-sdk/src/sandbox/filesystem/watchHandle.ts b/packages/js-sdk/src/sandbox/filesystem/watchHandle.tsindex 71f3091b5..5ef32e11f 100644--- a/packages/js-sdk/src/sandbox/filesystem/watchHandle.ts+++ b/packages/js-sdk/src/sandbox/filesystem/watchHandle.ts@@ -54,7 +54,7 @@ export class WatchHandle {/*** Stop watching the directory.*/- async close() {+ async stop() {this.handleStop()}diff --git a/packages/js-sdk/tests/sandbox/files/watch.test.ts b/packages/js-sdk/tests/sandbox/files/watch.test.tsindex d5ed5913c..7486632b0 100644--- a/packages/js-sdk/tests/sandbox/files/watch.test.ts+++ b/packages/js-sdk/tests/sandbox/files/watch.test.ts@@ -1,7 +1,7 @@import { expect } from 'vitest'import { sandboxTest } from '../../setup.js'-import { FilesystemEventType, NotFoundError } from '../../../src'+import { FilesystemEventType, NotFoundError, SandboxError } from '../../../src'sandboxTest('watch directory changes', async ({ sandbox }) => {const dirname = 'test_watch_dir'@@ -14,11 +14,10 @@ sandboxTest('watch directory changes', async ({ sandbox }) => {let trigger: () => void- const eventPromise = new Promise<void>(resolve => {+ const eventPromise = new Promise<void>((resolve) => {trigger = resolve})-const handle = await sandbox.files.watch(dirname, async (event) => {if (event.type === FilesystemEventType.WRITE && event.name === filename) {trigger()@@ -29,11 +28,23 @@ sandboxTest('watch directory changes', async ({ sandbox }) => {await eventPromise- await handle.close()+ await handle.stop()})sandboxTest('watch non-existing directory', async ({ sandbox }) => {const dirname = 'non_existing_watch_dir'- await expect(sandbox.files.watch(dirname, () => { })).rejects.toThrowError(NotFoundError)+ await expect(sandbox.files.watch(dirname, () => {})).rejects.toThrowError(+ NotFoundError+ )+})++sandboxTest('watch file', async ({ sandbox }) => {+ const filename = 'test_watch.txt'+ const content = 'This file will be watched.'+ await sandbox.files.write(filename, content)++ await expect(sandbox.files.watch(filename, () => {})).rejects.toThrowError(+ SandboxError+ )})diff --git a/packages/python-sdk/e2b/sandbox_async/filesystem/watch_handle.py b/packages/python-sdk/e2b/sandbox_async/filesystem/watch_handle.pyindex 6a2400d6c..283b2534b 100644--- a/packages/python-sdk/e2b/sandbox_async/filesystem/watch_handle.py+++ b/packages/python-sdk/e2b/sandbox_async/filesystem/watch_handle.py@@ -26,7 +26,7 @@ class AsyncWatchHandle:self._wait = asyncio.create_task(self._handle_events())- async def close(self):+ async def stop(self):"""Stop watching the directory."""diff --git a/packages/python-sdk/e2b/sandbox_sync/filesystem/watch_handle.py b/packages/python-sdk/e2b/sandbox_sync/filesystem/watch_handle.pyindex fe3286b5e..bc479b581 100644--- a/packages/python-sdk/e2b/sandbox_sync/filesystem/watch_handle.py+++ b/packages/python-sdk/e2b/sandbox_sync/filesystem/watch_handle.py@@ -21,7 +21,7 @@ class WatchHandle:self._watcher_id = watcher_idself._closed = False- def close(self):+ def stop(self):"""Stop watching the directory. After you close the watcher you won't be able to get the events anymore."""diff --git a/packages/python-sdk/tests/async/sandbox_async/files/test_watch.py b/packages/python-sdk/tests/async/sandbox_async/files/test_watch.pyindex 23ba9f3ee..c5f1b557e 100644--- a/packages/python-sdk/tests/async/sandbox_async/files/test_watch.py+++ b/packages/python-sdk/tests/async/sandbox_async/files/test_watch.py@@ -2,7 +2,13 @@ import pytestfrom asyncio import Event-from e2b import NotFoundException, AsyncSandbox, FilesystemEvent, FilesystemEventType+from e2b import (+ NotFoundException,+ AsyncSandbox,+ FilesystemEvent,+ FilesystemEventType,+ SandboxException,+)async def test_watch_directory_changes(async_sandbox: AsyncSandbox):@@ -26,7 +32,7 @@ async def test_watch_directory_changes(async_sandbox: AsyncSandbox):await event_triggered.wait()- await handle.close()+ await handle.stop()async def test_watch_non_existing_directory(async_sandbox: AsyncSandbox):@@ -36,4 +42,9 @@ async def test_watch_non_existing_directory(async_sandbox: AsyncSandbox):await async_sandbox.files.watch(dirname, on_event=lambda e: None)-# TODO: Add test for nonexistent file+async def test_watch_file(async_sandbox: AsyncSandbox):+ filename = "test_watch.txt"+ await async_sandbox.files.write(filename, "This file will be watched.")++ with pytest.raises(SandboxException):+ await async_sandbox.files.watch(filename, on_event=lambda e: None)diff --git a/packages/python-sdk/tests/sync/sandbox_sync/files/test_watch.py b/packages/python-sdk/tests/sync/sandbox_sync/files/test_watch.pyindex 2219ee3d7..72bc684ba 100644--- a/packages/python-sdk/tests/sync/sandbox_sync/files/test_watch.py+++ b/packages/python-sdk/tests/sync/sandbox_sync/files/test_watch.py@@ -1,6 +1,6 @@import pytest-from e2b import NotFoundException, FilesystemEventType, Sandbox+from e2b import NotFoundException, FilesystemEventType, Sandbox, SandboxExceptiondef test_watch_directory_changes(sandbox: Sandbox):@@ -16,18 +16,24 @@ def test_watch_directory_changes(sandbox: Sandbox):sandbox.files.write(f"{dirname}/{filename}", new_content)- for event in handle:+ events = handle.get()+ for event in events:if event.type == FilesystemEventType.WRITE and event.name == filename:break- handle.close()+ handle.stop()-def test_watch_non_existing_directory(sandbox):+def test_watch_non_existing_directory(sandbox: Sandbox):dirname = "non_existing_watch_dir"with pytest.raises(NotFoundException):sandbox.files.watch(dirname)-# TODO: Add test for nonexistent file+def test_watch_file(sandbox: Sandbox):+ filename = "test_watch.txt"+ sandbox.files.write(filename, "This file will be watched.")++ with pytest.raises(SandboxException):+ sandbox.files.watch(filename)