Skip to content

armgit / E2B

Public Imported from GitHub
0

Open-source, secure environment with real-world tools for enterprise-grade agents.

Use startAndWait internally; Catch startCmd logging process start error

Tomas Valentacb0db9d22d99
diff --git a/packages/js-sdk/src/runCmd.ts b/packages/js-sdk/src/runCmd.ts
index b559b209d..c96b0ac39 100644
--- a/packages/js-sdk/src/runCmd.ts
+++ b/packages/js-sdk/src/runCmd.ts
@@ -5,10 +5,7 @@ export async function runCmd(command: string, opts?: { apiKey?: string }) {
apiKey: opts?.apiKey || process?.env?.E2B_API_KEY || '', // Sandbox.create will throw an error if the API key is not provided so no need to check here
})
- const proc = await sandbox.process.start({
- cmd: command,
- })
- const out = await proc.wait()
+ const out = await sandbox.process.startAndWait(command)
await sandbox.close()
diff --git a/packages/js-sdk/src/runCode.ts b/packages/js-sdk/src/runCode.ts
index dae6be30a..875feec12 100644
--- a/packages/js-sdk/src/runCode.ts
+++ b/packages/js-sdk/src/runCode.ts
@@ -64,10 +64,7 @@ export async function runCode(
await sandbox.filesystem.write(filepath, code)
- const codeProc = await sandbox.process.start({
- cmd: `${binary} ${filepath}`,
- })
- const out = await codeProc.wait()
+ const out = await sandbox.process.startAndWait(`${binary} ${filepath}`)
await sandbox.close()
diff --git a/packages/js-sdk/src/sandbox/index.ts b/packages/js-sdk/src/sandbox/index.ts
index 30ffe4b18..9a9e2916c 100644
--- a/packages/js-sdk/src/sandbox/index.ts
+++ b/packages/js-sdk/src/sandbox/index.ts
@@ -78,8 +78,6 @@ export class Sandbox extends SandboxConnection {
// We use any here because we cannot properly reference the type of the Sandbox subclass
readonly _actions: Map<string, Action<any, any>> = new Map()
- private _startCmd?: Promise<Process>
-
private readonly onScanPorts?: ScanOpenPortsHandler
protected constructor(opts?: SandboxOpts) {
@@ -405,7 +403,8 @@ export class Sandbox extends SandboxConnection {
startAndWait: async (optsOrCmd: string | ProcessOpts) => {
const opts = typeof optsOrCmd === 'string' ? { cmd: optsOrCmd } : optsOrCmd
const process = await this.process.start(opts)
- return await process.wait()
+ const out = await process.wait()
+ return out
}
}
@@ -706,10 +705,14 @@ export class Sandbox extends SandboxConnection {
}
private async handleStartCmdLogs() {
- this._startCmd = this.process.start({
- cmd: 'sudo journalctl --follow --lines=all -o cat _SYSTEMD_UNIT=start_cmd.service',
- envVars: {},
- cwd: '/',
- })
+ try {
+ await this.process.startAndWait({
+ cmd: 'sudo journalctl --follow --lines=all -o cat _SYSTEMD_UNIT=start_cmd.service',
+ envVars: {},
+ cwd: '/',
+ })
+ } catch (err) {
+ this.logger.warn?.("error waiting for the start command logs")
+ }
}
}
diff --git a/packages/js-sdk/src/templates/dataAnalysis.ts b/packages/js-sdk/src/templates/dataAnalysis.ts
index 1c249f2ca..5e97ba901 100644
--- a/packages/js-sdk/src/templates/dataAnalysis.ts
+++ b/packages/js-sdk/src/templates/dataAnalysis.ts
@@ -53,17 +53,16 @@ export class DataAnalysis extends Sandbox {
const currentEpoch = new Date().getTime()
const codefilePath = `/tmp/main-${currentEpoch}.py`
await this.filesystem.write(codefilePath, code)
- const proc = await this.process.start({
+ const output = await this.process.startAndWait({
cmd: `python ${codefilePath}`,
...opts,
})
- await proc.wait()
await watcher.stop()
return {
- stdout: proc.output.stdout,
- stderr: proc.output.stderr,
+ stdout: output.stdout,
+ stderr: output.stderr,
artifacts: artifacts.map((artifact) => new Artifact<this>(artifact, this)),
}
}
@@ -86,13 +85,10 @@ export class DataAnalysis extends Sandbox {
return
}
- const proc = await this.process.start({
- cmd: `${command} ${packageNames}`,
- })
- await proc.wait()
+ const out = await this.process.startAndWait(`${command} ${packageNames}`)
- if (proc.output.exitCode !== 0) {
- throw new Error(`Failed to install package ${packageNames}: ${proc.output.stderr}`)
+ if (out.exitCode !== 0) {
+ throw new Error(`Failed to install package ${packageNames}: ${out.stderr}`)
}
}
}