Skip to content
Open
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
43 changes: 23 additions & 20 deletions src/content/docs/containers/examples/cron.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -54,31 +54,34 @@ Use a cron expression in your Wrangler config to specify the schedule:
Then in your Worker, call your Container from the "scheduled" handler:

```ts
import { Container, getContainer } from "@cloudflare/containers";
import { Container, getContainer } from '@cloudflare/containers';

export class CronContainer extends Container {
sleepAfter = "5m";
sleepAfter = '10s';

override onStart() {
console.log('Starting container');
}

override onStop() {
console.log('Contianer Stopped');
}
}

export default {
async fetch(): Promise<Response> {
return new Response(
"This Worker runs a cron job to execute a container on a schedule.",
);
},
async fetch(): Promise<Response> {
return new Response("This Worker runs a cron job to execute a container on a schedule.");
},

// scheduled is called when a cron trigger fires
async scheduled(
_controller: any,
env: { CRON_CONTAINER: DurableObjectNamespace<CronContainer> },
) {
await getContainer(env.CRON_CONTAINER).startAndWaitForPorts({
startOptions: {
envVars: {
MESSAGE: "Start Time: " + new Date().toISOString(),
},
},
});
},
async scheduled(_controller: any, env: { CRON_CONTAINER: DurableObjectNamespace<CronContainer> }) {
let container = getContainer(env.CRON_CONTAINER);
await container.start({
envVars: {
LOCATION: "The Cloudflare San Francisco Office",
LATITUDE: "37.780259",
LONGITUDE: "-122.390519",
}
})
},
};
```
Loading