diff --git a/CHANGELOG.md b/CHANGELOG.md index 558b90c..2eab25c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ Versioning and the Keep a Changelog structure. ### Added +- Optional sandbox expiry through `CreateSandboxRequest.Builder.timeoutMinutes()`, + with `expiresAt()` response fields and an optional final-minute SIGTERM warning. - Checkpoint create, list, restore, and delete operations scoped to a running sandbox through `Sandbox.checkpoints()`. - Reusable disk and full snapshot create, get, list, and delete operations, diff --git a/README.md b/README.md index b74929a..c93dd77 100644 --- a/README.md +++ b/README.md @@ -101,6 +101,24 @@ sandbox.setLabels(Map.of("environment", "production")); List filters use AND semantics. `setLabels()` merges the supplied keys into the existing label map. +## Limit sandbox lifetime + +```java +Sandbox sandbox = client.createSandbox( + CreateSandboxRequest.builder("s-1vcpu-1gb") + .rootFileSystem("devbox:1") + .timeoutMinutes(10) + .sigtermBeforeExpiry(true) + .build()); + +System.out.println(sandbox.data().expiresAt()); +``` + +`timeoutMinutes()` accepts 2–1440 whole minutes and starts when the sandbox first +reaches `RUNNING`. Activity and pause/resume do not extend the deadline. +`sigtermBeforeExpiry()` requests a best-effort warning to managed workloads +during the final minute and requires `timeoutMinutes()`. + ## Capture and restore snapshots ```java diff --git a/examples/src/main/java/sh/createos/examples/helloworld/HelloWorld.java b/examples/src/main/java/sh/createos/examples/helloworld/HelloWorld.java index 5c2c8da..4551ef5 100644 --- a/examples/src/main/java/sh/createos/examples/helloworld/HelloWorld.java +++ b/examples/src/main/java/sh/createos/examples/helloworld/HelloWorld.java @@ -15,7 +15,14 @@ public static void main(String[] arguments) { CreateOsClient.builder().apiKey(System.getenv("CREATEOS_API_KEY")).build(); Sandbox sandbox = client.createSandbox( - CreateSandboxRequest.builder("s-4vcpu-4gb").rootFileSystem("devbox:1").build()); + CreateSandboxRequest.builder("s-4vcpu-4gb") + .rootFileSystem("devbox:1") + .timeoutMinutes(10) + .sigtermBeforeExpiry(true) + .build()); + System.out.printf( + "expires_at=%s sigterm_before_expiry=%s%n", + sandbox.data().expiresAt(), sandbox.data().sigtermBeforeExpiry()); try { var response = sandbox.runCommand( diff --git a/src/main/java/sh/createos/CreateOsClient.java b/src/main/java/sh/createos/CreateOsClient.java index 94c67e2..2660e57 100644 --- a/src/main/java/sh/createos/CreateOsClient.java +++ b/src/main/java/sh/createos/CreateOsClient.java @@ -167,7 +167,9 @@ public Sandbox createSandbox(CreateSandboxRequest request) { null, request.autoPauseAfterSeconds(), created.labels(), - created.snapshotId()); + created.snapshotId(), + created.expiresAt(), + created.sigtermBeforeExpiry()); return new Sandbox(transport, data); } diff --git a/src/main/java/sh/createos/Sandbox.java b/src/main/java/sh/createos/Sandbox.java index b2b6081..92218e1 100644 --- a/src/main/java/sh/createos/Sandbox.java +++ b/src/main/java/sh/createos/Sandbox.java @@ -616,7 +616,9 @@ private static SandboxData copyWithStatus(SandboxData source, SandboxStatus stat source.forkedFrom(), source.autoPauseAfterSeconds(), source.labels(), - source.snapshotId()); + source.snapshotId(), + source.expiresAt(), + source.sigtermBeforeExpiry()); } private record DestroyedResponse(String id, SandboxStatus status) {} diff --git a/src/main/java/sh/createos/model/CreateSandboxRequest.java b/src/main/java/sh/createos/model/CreateSandboxRequest.java index 40d3efa..865d916 100644 --- a/src/main/java/sh/createos/model/CreateSandboxRequest.java +++ b/src/main/java/sh/createos/model/CreateSandboxRequest.java @@ -23,7 +23,9 @@ public record CreateSandboxRequest( String region, @JsonProperty("auto_pause_after_seconds") Integer autoPauseAfterSeconds, Map labels, - @JsonProperty("snapshot_id") String snapshotId) { + @JsonProperty("snapshot_id") String snapshotId, + @JsonProperty("timeout_minutes") Integer timeoutMinutes, + @JsonProperty("sigterm_before_expiry") Boolean sigtermBeforeExpiry) { /** Validates required values and defensively copies collection values. */ public CreateSandboxRequest { if ((shape == null || shape.isBlank()) && (snapshotId == null || snapshotId.isBlank())) { @@ -71,6 +73,8 @@ public CreateSandboxRequest( region, autoPauseAfterSeconds, Map.of(), + null, + null, null); } @@ -107,6 +111,8 @@ public CreateSandboxRequest( region, autoPauseAfterSeconds, labels, + null, + null, null); } @@ -138,6 +144,8 @@ public static final class Builder { private Integer autoPauseAfterSeconds; private Map labels = Map.of(); private String snapshotId; + private Integer timeoutMinutes; + private Boolean sigtermBeforeExpiry; private Builder(String shape) { this.shape = shape; @@ -161,6 +169,18 @@ public Builder labels(Map labels) { return this; } + /** Sets the absolute lifetime in whole minutes (2-1440) from first reaching running. */ + public Builder timeoutMinutes(int timeoutMinutes) { + this.timeoutMinutes = timeoutMinutes; + return this; + } + + /** Requests a best-effort SIGTERM warning in the final minute. Requires a timeout. */ + public Builder sigtermBeforeExpiry(boolean sigtermBeforeExpiry) { + this.sigtermBeforeExpiry = sigtermBeforeExpiry; + return this; + } + /** Restores an owned ready reusable snapshot instead of a root filesystem. */ public Builder snapshotId(String snapshotId) { this.snapshotId = snapshotId; @@ -251,7 +271,9 @@ public CreateSandboxRequest build() { region, autoPauseAfterSeconds, labels, - snapshotId); + snapshotId, + timeoutMinutes, + sigtermBeforeExpiry); } } } diff --git a/src/main/java/sh/createos/model/CreateSandboxResponse.java b/src/main/java/sh/createos/model/CreateSandboxResponse.java index 051434b..bda55a8 100644 --- a/src/main/java/sh/createos/model/CreateSandboxResponse.java +++ b/src/main/java/sh/createos/model/CreateSandboxResponse.java @@ -2,6 +2,7 @@ import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonProperty; +import java.time.Instant; import java.util.List; import java.util.Map; @@ -22,7 +23,9 @@ public record CreateSandboxResponse( @JsonProperty("bandwidth_quota_bytes") long bandwidthQuotaBytes, @JsonProperty("ingress_url_template") String ingressUrlTemplate, Map labels, - @JsonProperty("snapshot_id") String snapshotId) { + @JsonProperty("snapshot_id") String snapshotId, + @JsonProperty("expires_at") Instant expiresAt, + @JsonProperty("sigterm_before_expiry") boolean sigtermBeforeExpiry) { /** Normalizes missing labels and keeps the response immutable. */ public CreateSandboxResponse { labels = labels == null ? Map.of() : Map.copyOf(labels); @@ -58,7 +61,9 @@ public CreateSandboxResponse( bandwidthQuotaBytes, ingressUrlTemplate, Map.of(), - null); + null, + null, + false); } /** Creates a response with labels, preserving the pre-snapshots constructor. */ @@ -92,6 +97,8 @@ public CreateSandboxResponse( bandwidthQuotaBytes, ingressUrlTemplate, labels, - null); + null, + null, + false); } } diff --git a/src/main/java/sh/createos/model/SandboxData.java b/src/main/java/sh/createos/model/SandboxData.java index 5e9734a..79adde5 100644 --- a/src/main/java/sh/createos/model/SandboxData.java +++ b/src/main/java/sh/createos/model/SandboxData.java @@ -35,7 +35,9 @@ public record SandboxData( @JsonProperty("forked_from") String forkedFrom, @JsonProperty("auto_pause_after_seconds") Integer autoPauseAfterSeconds, Map labels, - @JsonProperty("snapshot_id") String snapshotId) { + @JsonProperty("snapshot_id") String snapshotId, + @JsonProperty("expires_at") Instant expiresAt, + @JsonProperty("sigterm_before_expiry") boolean sigtermBeforeExpiry) { /** Defensively copies collection values. */ public SandboxData { egressRules = egressRules == null ? List.of() : List.copyOf(egressRules); @@ -99,7 +101,9 @@ public SandboxData( forkedFrom, autoPauseAfterSeconds, Map.of(), - null); + null, + null, + false); } /** Creates a projection with labels, preserving the pre-snapshots constructor. */ @@ -157,6 +161,8 @@ public SandboxData( forkedFrom, autoPauseAfterSeconds, labels, - null); + null, + null, + false); } } diff --git a/src/test/java/sh/createos/CreateOsClientTest.java b/src/test/java/sh/createos/CreateOsClientTest.java index 168ef29..ba2dca9 100644 --- a/src/test/java/sh/createos/CreateOsClientTest.java +++ b/src/test/java/sh/createos/CreateOsClientTest.java @@ -90,7 +90,7 @@ void createsSandboxWithExpectedWireNames() { exchange, 200, """ - {"status":"success","data":{"id":"sb-1","status":"running","labels":{"team":"backend"},"ip":"10.0.0.2","shape":"s-4vcpu-4gb","rootfs":"devbox:1","vcpu":4,"mem_mib":4096,"disk_mib":8192,"spawn_ms":42.5,"egress":[],"ingress_url_template":"https://.example.test"}} + {"status":"success","data":{"id":"sb-1","status":"running","labels":{"team":"backend"},"expires_at":"2026-11-23T12:10:00Z","sigterm_before_expiry":true,"ip":"10.0.0.2","shape":"s-4vcpu-4gb","rootfs":"devbox:1","vcpu":4,"mem_mib":4096,"disk_mib":8192,"spawn_ms":42.5,"egress":[],"ingress_url_template":"https://.example.test"}} """); }); @@ -100,6 +100,8 @@ void createsSandboxWithExpectedWireNames() { CreateSandboxRequest.builder("s-4vcpu-4gb") .rootFileSystem("devbox:1") .labels(Map.of("team", "backend")) + .timeoutMinutes(10) + .sigtermBeforeExpiry(true) .ingressEnabled(true) .build()); @@ -108,7 +110,11 @@ void createsSandboxWithExpectedWireNames() { assertTrue(requestBody.get().contains("\"rootfs\":\"devbox:1\"")); assertTrue(requestBody.get().contains("\"ingress_enabled\":true")); assertTrue(requestBody.get().contains("\"labels\":{\"team\":\"backend\"}")); + assertTrue(requestBody.get().contains("\"timeout_minutes\":10")); + assertTrue(requestBody.get().contains("\"sigterm_before_expiry\":true")); assertEquals(Map.of("team", "backend"), sandbox.labels()); + assertEquals(java.time.Instant.parse("2026-11-23T12:10:00Z"), sandbox.data().expiresAt()); + assertTrue(sandbox.data().sigtermBeforeExpiry()); assertFalse(requestBody.get().contains("disk_mib")); }