Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add recursive watch #431

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions packages/js-sdk/src/sandbox/filesystem/index.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
import {
Code,
ConnectError,
createPromiseClient,
Transport,
PromiseClient,
ConnectError,
Code,
Transport,
} from '@connectrpc/connect'
import {
ConnectionConfig,
ConnectionOpts,
defaultUsername,
Username,
ConnectionOpts,
} from '../../connectionConfig'
import { handleEnvdApiError } from '../../envd/api'
import { authenticationHeader, handleRpcError } from '../../envd/rpc'
import {
SandboxError
} from '../../errors'
import { handleEnvdApiError } from '../../envd/api'
import { authenticationHeader, handleRpcError } from '../../envd/rpc'

import { EnvdApiClient } from '../../envd/api'
import { Filesystem as FilesystemService } from '../../envd/filesystem/filesystem_connect'
import { FileType as FsFileType, WatchDirResponse } from '../../envd/filesystem/filesystem_pb'

import { WatchHandle, FilesystemEvent } from './watchHandle'
import { FilesystemEvent, FilesystemEventType, WatchHandle } from './watchHandle'

export interface EntryInfo {
name: string
Expand Down Expand Up @@ -233,7 +233,7 @@ export class Filesystem {
async watch(
path: string,
onEvent: (event: FilesystemEvent) => void | Promise<void>,
opts?: FilesystemRequestOpts & { timeout?: number, onExit?: (err?: Error) => void | Promise<void> },
opts?: FilesystemRequestOpts & { timeout?: number, onExit?: (err?: Error) => void | Promise<void>, eventTypes?: Set<FilesystemEventType>},
): Promise<WatchHandle> {
const requestTimeoutMs = opts?.requestTimeoutMs ?? this.connectionConfig.requestTimeoutMs

Expand All @@ -251,6 +251,15 @@ export class Filesystem {
timeoutMs: opts?.timeout ?? this.defaultWatchTimeout,
})

let onEvent_ = onEvent

if (opts?.eventTypes) {
// wrap handler with event type filtering
onEvent_ = (event: FilesystemEvent) => {
if (opts.eventTypes!.has(event.type)) return onEvent(event)
}
}

try {
const startEvent: WatchDirResponse = (await events[Symbol.asyncIterator]().next()).value

Expand All @@ -263,7 +272,7 @@ export class Filesystem {
return new WatchHandle(
() => controller.abort(),
events,
onEvent,
onEvent_,
opts?.onExit,
)
} catch (err) {
Expand Down
15 changes: 13 additions & 2 deletions packages/js-sdk/src/sandbox/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import { createConnectTransport } from '@connectrpc/connect-web'

import { ConnectionOpts, ConnectionConfig, defaultUsername } from '../connectionConfig'
import { ConnectionConfig, ConnectionOpts, defaultUsername } from '../connectionConfig'
import { EnvdApiClient, handleEnvdApiError } from '../envd/api'
import { createRpcLogger } from '../logs'
import { Filesystem } from './filesystem'
import { FilesystemEvent, FilesystemEventType } from './filesystem/watchHandle'
import { Process } from './process'
import { Pty } from './pty'
import { SandboxApi } from './sandboxApi'
import { EnvdApiClient, handleEnvdApiError } from '../envd/api'

export interface SandboxOpts extends ConnectionOpts {
metadata?: Record<string, string>
envs?: Record<string, string>
timeoutMs?: number
onFileCreation?: (event: FilesystemEvent) => void | Promise<void>
}

export class Sandbox extends SandboxApi {
Expand Down Expand Up @@ -45,6 +47,7 @@ export class Sandbox extends SandboxApi {

this.envdApi = new EnvdApiClient({ apiUrl: this.envdApiUrl, logger: opts?.logger })
this.files = new Filesystem(rpcTransport, this.envdApi, this.connectionConfig)

this.commands = new Process(rpcTransport, this.connectionConfig)
this.pty = new Pty(rpcTransport, this.connectionConfig)
}
Expand All @@ -63,13 +66,21 @@ export class Sandbox extends SandboxApi {
: await this.createSandbox(template, sandboxOpts?.timeoutMs ?? this.defaultSandboxTimeoutMs, sandboxOpts)

const sbx = new this({ sandboxId, ...config }) as InstanceType<S>

if (sandboxOpts?.onFileCreation)
await sbx.files.watch('/', sandboxOpts.onFileCreation, { eventTypes: new Set([FilesystemEventType.CREATE])})

return sbx
}

static async connect<S extends typeof Sandbox>(this: S, sandboxId: string, opts?: Omit<SandboxOpts, 'metadata' | 'envs' | 'timeoutMs'>): Promise<InstanceType<S>> {
const config = new ConnectionConfig(opts)

const sbx = new this({ sandboxId, ...config }) as InstanceType<S>

if (opts?.onFileCreation)
await sbx.files.watch('/', opts.onFileCreation, { eventTypes: new Set([FilesystemEventType.CREATE])})

return sbx
}

Expand Down
34 changes: 32 additions & 2 deletions packages/js-sdk/tests/sandbox/connect.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { test, assert } from 'vitest'
import { assert, onTestFinished, test } from 'vitest'

import { Sandbox } from '../../src'
import { FilesystemEvent, FilesystemEventType, Sandbox } from '../../src'
import { isDebug, template } from '../setup.js'

test('connect', async () => {
Expand All @@ -19,3 +19,33 @@ test('connect', async () => {
}
}
})

test.skipIf(isDebug)('connect with file creation handler', async () => {
const sbx = await Sandbox.create(template, { timeoutMs: 10_000 })
const dirPath = '/new-dir2'
onTestFinished(async () => await sbx.files.remove(dirPath))

let trigger: () => void

const eventPromise = new Promise<void>((resolve) => {
trigger = resolve
})

const onFileCreation = async (e: FilesystemEvent) => {
if (e.type === FilesystemEventType.CREATE && e.name === dirPath) {
trigger()
}
}

const isRunning = await sbx.isRunning()
assert.isTrue(isRunning)

const sbxConnection = await Sandbox.create(sbx.sandboxId, { onFileCreation})
const isRunning2 = await sbxConnection.isRunning()
assert.isTrue(isRunning2)

const ok = await sbx.files.makeDir(dirPath)
assert.isTrue(ok)

await eventPromise
})
31 changes: 27 additions & 4 deletions packages/js-sdk/tests/sandbox/create.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { test, assert } from 'vitest'
import { assert, onTestFinished, test } from 'vitest'

import { Sandbox } from '../../src'
import { template, isDebug } from '../setup.js'
import { FilesystemEvent, FilesystemEventType, Sandbox } from '../../src'
import { isDebug, template } from '../setup.js'

test.skipIf(isDebug)('create', async () => {
const sbx = await Sandbox.create(template, { timeoutMs: 5_000 })
Expand All @@ -13,7 +13,7 @@ test.skipIf(isDebug)('create', async () => {
}
})

test.skipIf(isDebug)('metadata', async () => {
test.skipIf(isDebug)('create with metadata', async () => {
const metadata = {
'test-key': 'test-value',
}
Expand All @@ -29,3 +29,26 @@ test.skipIf(isDebug)('metadata', async () => {
await sbx.kill()
}
})

test.skipIf(isDebug)('create with file creation handler', async () => {
const dirPath = '/new-dir'

let trigger: () => void

const eventPromise = new Promise<void>((resolve) => {
trigger = resolve
})

const onFileCreation = async (e: FilesystemEvent) => {
if (e.type === FilesystemEventType.CREATE && e.name === dirPath) {
trigger()
}
}
const sbx = await Sandbox.create(template, { timeoutMs: 5_000, onFileCreation })
onTestFinished(async () => await sbx.files.remove(dirPath))

const ok = await sbx.files.makeDir(dirPath)
assert.isTrue(ok)

await eventPromise
})
3 changes: 2 additions & 1 deletion packages/js-sdk/tests/sandbox/files/list.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { assert } from 'vitest'
import { assert, onTestFinished } from 'vitest'

import { sandboxTest } from '../../setup.js'

sandboxTest('list directory', async ({ sandbox }) => {
const dirName = 'test_directory4'
onTestFinished(async () => await sandbox.files.remove(dirName))

await sandbox.files.makeDir(dirName)

Expand Down
52 changes: 49 additions & 3 deletions packages/js-sdk/tests/sandbox/files/watch.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { expect } from 'vitest'
import { assert, expect, onTestFinished } from 'vitest'

import { sandboxTest } from '../../setup.js'
import { FilesystemEventType, NotFoundError } from '../../../src'
import { sandboxTest } from '../../setup.js'

sandboxTest('watch directory changes', async ({ sandbox }) => {
const dirname = 'test_watch_dir'
Expand All @@ -20,7 +20,7 @@ sandboxTest('watch directory changes', async ({ sandbox }) => {


const handle = await sandbox.files.watch(dirname, async (event) => {
if (event.type === FilesystemEventType.WRITE && event.name === filename) {
if (event.type === FilesystemEventType.WRITE && event.name === `/home/user/${dirname}/${filename}`) {
trigger()
}
})
Expand All @@ -37,3 +37,49 @@ sandboxTest('watch non-existing directory', async ({ sandbox }) => {

await expect(sandbox.files.watch(dirname, () => { })).rejects.toThrowError(NotFoundError)
})

sandboxTest('watch recursive directory changes', async ({ sandbox }) => {
const parentDir = 'a'
const childDir = 'b'
const fileName = 'test_watch.txt'
const filePath = `/${parentDir}/${childDir}/${fileName}`
const content = 'This file will be watched.'

onTestFinished(async () => {
await sandbox.files.remove('/'+parentDir)
})

let trigger1: () => void
let trigger2: () => void
let trigger3: () => void

const eventPromise1 = new Promise<void>((resolve) => {
trigger1 = resolve
})

const eventPromise2 = new Promise<void>((resolve) => {
trigger2 = resolve
})

const eventPromise3 = new Promise<void>((resolve) => {
trigger3 = resolve
})

const handle = await sandbox.files.watch('/', async (event) => {
assert.strictEqual(event.type, FilesystemEventType.CREATE)
// watch parent dir creation
if (event.name === `/${parentDir}`) trigger1()
// watch child dir creation
if (event.name === `/${parentDir}/${childDir}`) trigger2()
// watch nested file creation
if (event.name === filePath) trigger3()
}, { eventTypes: new Set([FilesystemEventType.CREATE])})

await sandbox.files.makeDir('/' + parentDir)
await sandbox.files.makeDir(`/${parentDir}/${childDir}`)
await sandbox.files.write(filePath, content)

await Promise.all([eventPromise1, eventPromise2, eventPromise3])

await handle.close()
})