Skip to content
Open
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
9 changes: 9 additions & 0 deletions src/workerd/api/container.c++
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "container.h"

#include <workerd/api/http.h>
#include <workerd/io/features.h>
#include <workerd/io/io-context.h>

namespace workerd::api {
Expand All @@ -17,6 +18,7 @@ Container::Container(rpc::Container::Client rpcClient, bool running)
running(running) {}

void Container::start(jsg::Lock& js, jsg::Optional<StartupOptions> maybeOptions) {
auto flags = FeatureFlags::get(js);
JSG_REQUIRE(!running, Error, "start() cannot be called on a container that is already running.");

StartupOptions options = kj::mv(maybeOptions).orDefault({});
Expand Down Expand Up @@ -50,6 +52,13 @@ void Container::start(jsg::Lock& js, jsg::Optional<StartupOptions> maybeOptions)
IoContext::current().addTask(req.sendIgnoringResult());

running = true;

if (flags.getWorkerdExperimental()) {
KJ_IF_SOME(hardTimeoutMs, options.hardTimeout) {
JSG_REQUIRE(hardTimeoutMs > 0, TypeError, "Hard timeout must be greater than 0");
req.setHardTimeoutMs(hardTimeoutMs);
}
}
}

jsg::Promise<void> Container::setInactivityTimeout(jsg::Lock& js, int64_t durationMs) {
Expand Down
19 changes: 18 additions & 1 deletion src/workerd/api/container.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,27 @@ class Container: public jsg::Object {
jsg::Optional<kj::Array<kj::String>> entrypoint;
bool enableInternet = false;
jsg::Optional<jsg::Dict<kj::String>> env;
jsg::Optional<int64_t> hardTimeout;

// TODO(containers): Allow intercepting stdin/stdout/stderr by specifying streams here.

JSG_STRUCT(entrypoint, enableInternet, env);
JSG_STRUCT(entrypoint, enableInternet, env, hardTimeout);
JSG_STRUCT_TS_OVERRIDE_DYNAMIC(CompatibilityFlags::Reader flags) {
if (flags.getWorkerdExperimental()) {
JSG_TS_OVERRIDE(ContainerStartupOptions {
entrypoint?: string[];
enableInternet: boolean;
env?: Record<string, string>;
hardTimeout?: number;
});
} else {
JSG_TS_OVERRIDE(ContainerStartupOptions {
entrypoint?: string[];
enableInternet: boolean;
env?: Record<string, string>;
});
}
}
};

bool getRunning() {
Expand Down
6 changes: 6 additions & 0 deletions src/workerd/io/container.capnp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ interface Container @0x9aaceefc06523bca {
# If null, the container will start with the environment variables defined in its image.
# The format is defined as a list of `NAME=VALUE`.
# The container runtime should validate the environment variables input.

hardTimeoutMs @3 :Int64;
# Configures an absolute timeout that starts when the container starts and never resets.
# The container will be forcefully terminated when this timeout expires, regardless of activity.
# Unlike inactivity timeout, this is a hard deadline from container startup.
# If 0 (default), no hard timeout is applied.
}

monitor @2 () -> (exitCode: Int32);
Expand Down
1 change: 1 addition & 0 deletions types/generated-snapshot/experimental/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3840,6 +3840,7 @@ interface ContainerStartupOptions {
entrypoint?: string[];
enableInternet: boolean;
env?: Record<string, string>;
hardTimeout?: number;
}
/**
* The **`FileSystemHandle`** interface of the File System API is an object which represents a file or directory entry.
Expand Down
1 change: 1 addition & 0 deletions types/generated-snapshot/experimental/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3851,6 +3851,7 @@ export interface ContainerStartupOptions {
entrypoint?: string[];
enableInternet: boolean;
env?: Record<string, string>;
hardTimeout?: number;
}
/**
* The **`FileSystemHandle`** interface of the File System API is an object which represents a file or directory entry.
Expand Down
1 change: 1 addition & 0 deletions types/generated-snapshot/latest/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3734,6 +3734,7 @@ interface ContainerStartupOptions {
entrypoint?: string[];
enableInternet: boolean;
env?: Record<string, string>;
hardTimeout?: number | bigint;
}
/**
* The **`MessagePort`** interface of the Channel Messaging API represents one of the two ports of a MessageChannel, allowing messages to be sent from one port and listening out for them arriving at the other.
Expand Down
1 change: 1 addition & 0 deletions types/generated-snapshot/latest/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3745,6 +3745,7 @@ export interface ContainerStartupOptions {
entrypoint?: string[];
enableInternet: boolean;
env?: Record<string, string>;
hardTimeout?: number | bigint;
}
/**
* The **`MessagePort`** interface of the Channel Messaging API represents one of the two ports of a MessageChannel, allowing messages to be sent from one port and listening out for them arriving at the other.
Expand Down