> ## Documentation Index
> Fetch the complete documentation index at: https://novita.ai/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Automatisches Pausieren und Fortsetzen

export const SandboxConfigHint = () => {
  if (typeof document === "undefined") {
    return null;
  } else {
    return <Note>Before running the example code in this document, please ensure you have properly configured environment variables. For details, please refer to <a href="/docs/de/guides/sandbox-your-first-agent-sandbox#configure-environment-variables">Configure Environment Variables</a>.</Note>;
  }
};

Diese Funktionen bauen auf [Sandbox-Persistenz](/docs/de/guides/sandbox-persistence) auf. Automatisches Pausieren behält den Sandbox-Zustand bei, wenn die maximale Lebensdauer abläuft. Automatisches Fortsetzen weckt eine pausierte Sandbox auf, wenn neue Aktivität eintrifft.

<SandboxConfigHint />

## Konfigurieren

Übergeben Sie beim Erstellen einer Sandbox eine `lifecycle`-Konfiguration. Diese steuert das Timeout-Verhalten und ob eine pausierte Sandbox automatisch geweckt werden kann.

<CodeGroup>
  ```js JavaScript & TypeScript icon="js" theme={"system"}
  import { Sandbox } from 'novita-sandbox'

  const sandbox = await Sandbox.create({
    timeoutMs: 10 * 60 * 1000,
    lifecycle: {
      onTimeout: 'pause',
      autoResume: true, // resume when activity arrives
    },
  })
  ```

  ```python Python icon="python" theme={"system"}
  from novita_sandbox.core import Sandbox

  sandbox = Sandbox.create(
      timeout=10 * 60,
      lifecycle={
          "on_timeout": "pause",
          "auto_resume": True,  # resume when activity arrives
      },
  )
  ```
</CodeGroup>

### Lifecycle-Optionen

Die Einstellung `lifecycle` unterstützt diese Felder:

| Einstellung                                | Option    | Bedeutung                                                                                                                            |
| ------------------------------------------ | --------- | ------------------------------------------------------------------------------------------------------------------------------------ |
| `onTimeout` (JS) / `on_timeout` (Python)   | `"kill"`  | Standardverhalten. Die Sandbox wird nach ihrem Timeout entfernt.                                                                     |
| `onTimeout` (JS) / `on_timeout` (Python)   | `"pause"` | Die Sandbox wird nach dem Timeout pausiert, statt gelöscht zu werden.                                                                |
| `autoResume` (JS) / `auto_resume` (Python) | `false`   | Standardverhalten. Pausierte Sandboxes bleiben pausiert, bis sie manuell fortgesetzt werden.                                         |
| `autoResume` (JS) / `auto_resume` (Python) | `true`    | Pausierte Sandboxes starten neu, wenn unterstützte Aktivität eintrifft. Dafür muss das Timeout-Verhalten auf `"pause"` gesetzt sein. |

Wenn automatisches Fortsetzen deaktiviert ist oder weggelassen wird, kann eine pausierte Sandbox weiterhin manuell mit `Sandbox.connect()` fortgesetzt werden.

## Automatisches Pausieren

Standardmäßig wird eine Sandbox beendet, wenn ihr Timeout abläuft. Um stattdessen den Zustand beizubehalten, setzen Sie `onTimeout` in JavaScript oder `on_timeout` in Python, damit bei Timeout pausiert wird.

<CodeGroup>
  ```js JavaScript & TypeScript icon="js" theme={"system"}
  import { Sandbox } from 'novita-sandbox/code-interpreter'

  const sandbox = await Sandbox.create({
    timeoutMs: 300_000,
    lifecycle: {
      onTimeout: 'pause',
      autoResume: false,
    },
  })

  await sandbox.kill()
  ```

  ```python Python icon="python" theme={"system"}
  from novita_sandbox.code_interpreter import Sandbox

  sandbox = Sandbox.create(
      timeout=300,
      lifecycle={
          "on_timeout": "pause",
          "auto_resume": False,
      },
  )

  sandbox.kill()
  ```
</CodeGroup>

## Automatisches Fortsetzen

Automatisches Fortsetzen kann eine pausierte Sandbox aufwecken, wenn unterstützte Aktivität eintrifft. Dies funktioniert nur, wenn die Sandbox so konfiguriert ist, dass sie bei Timeout pausiert.

### Timeout nach automatischem Fortsetzen

Nach einem automatischen Fortsetzen erhält die Sandbox ein Timeout von mindestens fünf Minuten. Wenn das ursprüngliche Timeout länger als fünf Minuten war, wird der längere ursprüngliche Wert wiederverwendet.

Der Timeout-Timer startet, wenn die Sandbox fortgesetzt wird, nicht wenn sie ursprünglich erstellt wurde.

Beispiel mit einem Zwei-Minuten-Timeout:

1. Die Sandbox läuft zwei Minuten lang und wird dann pausiert.
2. Neue Aktivität erreicht die Sandbox, wodurch sie fortgesetzt wird.
3. Die fortgesetzte Sandbox erhält ein Fünf-Minuten-Timeout, da dies das Minimum ist.
4. Wenn nichts den Timer zurücksetzt, wird die Sandbox nach fünf Minuten erneut pausiert.

Beispiel mit einem Ein-Stunden-Timeout:

* Die Sandbox wird mit einem Ein-Stunden-Timeout fortgesetzt, weil das ursprüngliche Timeout länger als das Fünf-Minuten-Minimum ist.

Dieses Verhalten setzt sich über zukünftige Pausierungs- und Fortsetzungszyklen hinweg fort, da die Lifecycle-Einstellungen an die Sandbox gebunden bleiben.

<Note>
  Sie können das Timeout nach dem Fortsetzen aktualisieren, indem Sie `setTimeout()` in JavaScript oder `set_timeout()` in Python verwenden.
</Note>

### Was als Aktivität zählt

Automatisches Fortsetzen kann durch SDK-Aktionen und HTTP-Traffic ausgelöst werden.

Unterstützte Beispiele sind:

* `sandbox.commands.run(...)`
* `sandbox.files.read(...)`
* `sandbox.files.write(...)`
* Aufrufen einer getunnelten Anwendungs-URL
* Senden von Anfragen an einen Dienst, der innerhalb der Sandbox läuft

Wenn eine Sandbox pausiert ist und automatisches Fortsetzen aktiviert ist, setzt die nächste unterstützte Aktion sie automatisch fort. Sie müssen `Sandbox.connect()` nicht zuerst aufrufen.

### SDK-Beispiel: pausieren, dann eine Datei lesen

Dieses Beispiel erstellt eine Sandbox, schreibt eine Datei, pausiert die Sandbox und liest dann die Datei. Der Lesevorgang bewirkt, dass die Sandbox fortgesetzt wird.

<CodeGroup>
  ```js JavaScript & TypeScript icon="js" theme={"system"}
  import { Sandbox } from 'novita-sandbox'

  const sandbox = await Sandbox.create({
    timeoutMs: 10 * 60 * 1000,
    lifecycle: {
      onTimeout: 'pause',
      autoResume: true,
    },
  })

  await sandbox.files.write('/home/user/hello.txt', 'hello from a paused sandbox')
  await sandbox.pause()

  const content = await sandbox.files.read('/home/user/hello.txt')
  console.log(content)
  console.log(`State after read: ${(await sandbox.getInfo()).state}`)
  ```

  ```python Python icon="python" theme={"system"}
  from novita_sandbox.core import Sandbox

  sandbox = Sandbox.create(
      timeout=10 * 60,
      lifecycle={
          "on_timeout": "pause",
          "auto_resume": True,
      },
  )

  sandbox.files.write("/home/user/hello.txt", "hello from a paused sandbox")
  sandbox.pause()

  content = sandbox.files.read("/home/user/hello.txt")
  print(content)
  print(f"State after read: {sandbox.get_info().state}")
  ```
</CodeGroup>

### Beispiel: Webserver mit automatischem Fortsetzen

Automatisches Fortsetzen eignet sich gut für Vorschauumgebungen und Webserver. Nachdem die Sandbox pausiert wurde, kann eine eingehende HTTP-Anfrage an den veröffentlichten Dienst sie aufwecken.

Das folgende Beispiel startet einen einfachen Python-HTTP-Server und gibt eine öffentliche Vorschau-URL aus. Sie können `getHost()` in JavaScript oder `get_host()` in Python verwenden, um den öffentlichen Hostnamen für einen Port abzurufen.

<CodeGroup>
  ```js JavaScript & TypeScript icon="js" theme={"system"}
  import { Sandbox } from 'novita-sandbox'

  const sandbox = await Sandbox.create({
    timeoutMs: 5 * 60 * 1000,
    lifecycle: {
      onTimeout: 'pause',
      autoResume: true,
    },
  })

  await sandbox.commands.run('python3 -m http.server 3000', { background: true })

  const host = sandbox.getHost(3000)
  // Once the sandbox times out and pauses, any request to the preview URL will automatically resume it.
  console.log(`Preview URL: https://${host}`)
  ```

  ```python Python icon="python" theme={"system"}
  from novita_sandbox.core import Sandbox

  sandbox = Sandbox.create(
      timeout=5 * 60,
      lifecycle={
          "on_timeout": "pause",
          "auto_resume": True,
      },
  )

  sandbox.commands.run("python3 -m http.server 3000", background=True)

  host = sandbox.get_host(3000)
  # Once the sandbox times out and pauses, any request to the preview URL will automatically resume it.
  print(f"Preview URL: https://{host}")
  ```
</CodeGroup>

## Bereinigung

Automatisches Fortsetzen bleibt über wiederholte Fortsetzungs- und Pausierungszyklen hinweg aktiviert. Jedes Fortsetzen startet einen neuen Timeout-Zeitraum, wobei mindestens fünf Minuten oder das längere ursprüngliche Timeout verwendet werden.

Nachdem die Sandbox fortgesetzt wurde, sollten Clients die Verbindung zu allen Diensten, die sie verwendet haben, erneut herstellen. Bestehende HTTP-, WebSocket-, Datenbank- und Terminalverbindungen bleiben nicht geöffnet, während die Sandbox pausiert ist.

Sie können `.kill()` aufrufen, um die Sandbox dauerhaft zu löschen. Danach kann sie nicht fortgesetzt werden.
