You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
You can now use `startAndWaitForPorts({startOptions: {envVars: {FOO:"BAR"}}})` instead of `startAndWaitForPorts(undefined, {}, {envVars: {FOO:"BAR"}})`, although that is still supported.
All lifecycle methods can be implemented as async if needed.
87
88
88
89
-`onStart()`: Called when container starts successfully - override to add custom behavior
@@ -97,16 +98,45 @@ If you don't stop the container here, the activity tracker will be renewed, and
97
98
98
99
-`fetch(request)`: Default handler to forward HTTP requests to the container. Can be overridden.
99
100
-`containerFetch(...)`: Sends an HTTP request to the container. Supports both standard fetch API signatures:
101
+
100
102
-`containerFetch(request, port?)`: Traditional signature with Request object
101
103
-`containerFetch(url, init?, port?)`: Standard fetch-like signature with URL string/object and RequestInit options
102
-
Either port parameter or defaultPort must be specified.
103
-
When you call any of the fetch functions, the activity will be automatically renewed, and if the container will be started if not already running.
104
-
**Do not use 'containerFetch' when trying to send a Request object with a websocket, until [this issue is addressed](https://github.com/cloudflare/workerd/issues/2319).
105
-
You can overcome this limitation by doing:
106
-
`container.fetch(switchPort(request, port))`
104
+
Either port parameter or defaultPort must be specified.
105
+
When you call any of the fetch functions, the activity will be automatically renewed, and if the container will be started if not already running.
106
+
\*\*Do not use 'containerFetch' when trying to send a Request object with a websocket, until [this issue is addressed](https://github.com/cloudflare/workerd/issues/2319).
Starts the container and then waits for specified ports to be ready. Prioritises `ports` passed in to the function, then `requiredPorts` if set, then `defaultPort`.
113
+
114
+
```typescript
115
+
exportinterfaceStartAndWaitForPortsOptions {
116
+
startOptions?: {
117
+
/** Environment variables to pass to the container */
118
+
envVars?:Record<string, string>;
119
+
/** Custom entrypoint to override container default */
120
+
entrypoint?:string[];
121
+
/** Whether to enable internet access for the container */
122
+
enableInternet?:boolean;
123
+
};
124
+
/** Ports to check */
125
+
ports?:number|number[];
126
+
cancellationOptions?: {
127
+
/** Abort signal to cancel start and port checking */
128
+
abort?:AbortSignal;
129
+
/** Max time to wait for container to start, in milliseconds */
130
+
instanceGetTimeoutMS?:number;
131
+
/** Max time to wait for ports to be ready, in milliseconds */
132
+
portReadyTimeoutMS?:number;
133
+
/** Polling interval for checking container has started or ports are ready, in milliseconds */
134
+
waitInterval?:number;
135
+
};
136
+
}
137
+
```
107
138
108
139
-`start()`: Starts the container if it's not running and sets up monitoring, without waiting for any ports to be ready.
109
-
-`startAndWaitForPorts(ports?, maxTries?)`: Starts the container using `start()` and then waits for specified ports to be ready. If no ports are specified, uses `requiredPorts` or `defaultPort`. If no ports can be determined, just starts the container without port checks.
110
140
-`stop(signal = SIGTERM)`: Sends the specified signal to the container.
111
141
-`destroy()`: Forcefully destroys the container.
112
142
-`getState()`: Get the current container state.
@@ -131,7 +161,7 @@ export class MyContainer extends Container {
131
161
132
162
// Set how long the container should stay active without requests
133
163
// Supported formats: "10m" (minutes), "30s" (seconds), "1h" (hours), or a number (seconds)
134
-
sleepAfter ="10m";
164
+
sleepAfter ='10m';
135
165
136
166
// Lifecycle method called when container starts
137
167
override onStart():void {
@@ -153,9 +183,7 @@ export class MyContainer extends Container {
153
183
154
184
// Lifecycle method when the container class considers the activity to be expired
155
185
override onActivityExpired() {
156
-
console.log(
157
-
'Container activity expired'
158
-
);
186
+
console.log('Container activity expired');
159
187
awaitthis.destroy();
160
188
}
161
189
@@ -170,7 +198,6 @@ export class MyContainer extends Container {
170
198
171
199
// Handle incoming requests
172
200
async fetch(request:Request):Promise<Response> {
173
-
174
201
// Default implementation forwards requests to the container
175
202
// This will automatically renew the activity timeout
176
203
returnawaitthis.containerFetch(request);
@@ -205,13 +232,13 @@ export class ConfiguredContainer extends Container {
205
232
defaultPort =9000;
206
233
207
234
// Set the timeout for sleeping the container after inactivity
208
-
sleepAfter ="2h";
235
+
sleepAfter ='2h';
209
236
210
237
// Environment variables to pass to the container
211
238
envVars = {
212
239
NODE_ENV: 'production',
213
240
LOG_LEVEL: 'info',
214
-
APP_PORT: '9000'
241
+
APP_PORT: '9000',
215
242
};
216
243
217
244
// Custom entrypoint to run in the container
@@ -249,18 +276,16 @@ export class MultiPortContainer extends Container {
0 commit comments