Skip to content

armgit / E2B

Public Imported from GitHub
0

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

Cleanup logs loop

Tomas Valenta3407e7bec470
diff --git a/packages/cli/src/commands/sandbox/logs.ts b/packages/cli/src/commands/sandbox/logs.ts
index 620c51cba..345768adb 100644
--- a/packages/cli/src/commands/sandbox/logs.ts
+++ b/packages/cli/src/commands/sandbox/logs.ts
@@ -7,7 +7,6 @@ import { ensureAPIKey } from 'src/api'
import { asBold, asTimestamp, withUnderline } from 'src/utils/format'
import { listSandboxes } from './list'
import { wait } from 'src/utils/wait'
-import { createDeferredPromise } from 'src/utils/promise'
const getSandboxLogs = e2b.withAPIKey(
e2b.api.path('/sandboxes/{sandboxID}/logs').method('get').create(),
@@ -22,16 +21,14 @@ function getLongID(sandboxID: string, clientID?: string) {
return sandboxID
}
-async function waitForSandboxEnd(apiKey: string, sandboxID: string) {
- const deferredPromise = createDeferredPromise()
+function waitForSandboxEnd(apiKey: string, sandboxID: string) {
+ let isRunning = true
async function monitor() {
const startTime = new Date().getTime()
// eslint-disable-next-line no-constant-condition
while (true) {
- await wait(10000)
-
const currentTime = new Date().getTime()
const elapsedTime = currentTime - startTime // Time elapsed in milliseconds
@@ -43,15 +40,16 @@ async function waitForSandboxEnd(apiKey: string, sandboxID: string) {
const response = await listSandboxes({ apiKey })
const sandbox = response.find(s => getLongID(s.sandboxID, s.clientID) === sandboxID)
if (!sandbox) {
- deferredPromise.resolve()
+ isRunning = false
break
}
+ await wait(5000)
}
}
monitor()
- return deferredPromise.promise
+ return () => isRunning
}
export const logsCommand = new commander.Command('logs')
@@ -61,50 +59,30 @@ export const logsCommand = new commander.Command('logs')
.action(async (sandboxID: string) => {
try {
const apiKey = ensureAPIKey()
- const runningSandboxes = listSandboxes({ apiKey })
-
- const startTime = new Date().getTime()
-
- let isFirstRun = true
- let isRunning = true
-
- waitForSandboxEnd(apiKey, sandboxID).then(() => {
- isRunning = false
- })
+ const getIsRunning = waitForSandboxEnd(apiKey, sandboxID)
let start: number | undefined
- // eslint-disable-next-line no-constant-condition
- while (true) {
- const currentTime = new Date().getTime()
- const elapsedTime = currentTime - startTime // Time elapsed in milliseconds
-
- // Check if 24 hours (in milliseconds) have passed
- if (elapsedTime >= maxRuntime) {
- console.log('\nStopped printing logs — 24 hours have passed')
- break
- }
-
- const logsPromise = listSandboxLogs({ apiKey, sandboxID, start })
- const info = await runningSandboxes
+ let isFirstRun = true
+ let firstLogsPrinted = false
- const sandbox = info ? info.find(s => getLongID(s.sandboxID, s.clientID) === sandboxID) : undefined
- isRunning = !!sandbox
+ console.log(`\nLogs for sandbox ${asBold(sandboxID)}:`)
- if (isFirstRun) {
- printSandboxInfo({
- isRunning: isRunning,
- sandboxID: sandboxID,
- })
- }
+ const isRunningPromise = listSandboxes({ apiKey }).then(r => r.find(s => getLongID(s.sandboxID, s.clientID) === sandboxID)).then(s => !!s)
+ do {
try {
- const logs = await logsPromise
+ const logs = await listSandboxLogs({ apiKey, sandboxID, start })
+
+ if (logs.length !== 0 && firstLogsPrinted === false) {
+ firstLogsPrinted = true
+ process.stdout.write('\n')
+ }
for (const log of logs) {
printLog(log.timestamp, log.line)
}
- console.log('new logs', logs.length)
+ const isRunning = await isRunningPromise
if (!isRunning && logs.length === 0 && isFirstRun) {
console.log(`\nStopped printing logs — sandbox ${withUnderline('not found')}`)
@@ -116,13 +94,12 @@ export const logsCommand = new commander.Command('logs')
break
}
- const lastLog = logs[logs.length - 1]
- start = (lastLog
- ? new Date(lastLog.timestamp).getTime() + 1
- : Date.now())
+ const lastLog = logs.length > 0 ? logs[logs.length - 1] : undefined
+ if (lastLog) {
+ start = new Date(lastLog.timestamp).getTime() + 1
+ }
} catch (e) {
if (e instanceof getSandboxLogs.Error) {
- console.log('is instance of Error', e.getActualType())
const error = e.getActualType()
if (error.status === 401) {
throw new Error(
@@ -143,22 +120,15 @@ export const logsCommand = new commander.Command('logs')
throw e
}
- await wait(500)
+ await wait(400)
isFirstRun = false
- }
+ } while (getIsRunning())
} catch (err: any) {
console.error(err)
process.exit(1)
}
})
-function printSandboxInfo({ sandboxID, isRunning }: {
- sandboxID: string,
- isRunning: boolean,
-}) {
- process.stdout.write(`\nLogs for ${withUnderline(isRunning ? 'running' : 'closed')} sandbox ${asBold(sandboxID)}:\n\n`)
-}
-
function printLog(timestamp: string, line: string) {
const log = JSON.parse(line)