Skip to content

Commit 57e8ae9

Browse files
authored
[Containers] update example using the repo's latest code
Use the code from the example repo
1 parent 4b92859 commit 57e8ae9

File tree

1 file changed

+23
-20
lines changed
  • src/content/docs/containers/examples

1 file changed

+23
-20
lines changed

src/content/docs/containers/examples/cron.mdx

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -54,31 +54,34 @@ Use a cron expression in your Wrangler config to specify the schedule:
5454
Then in your Worker, call your Container from the "scheduled" handler:
5555

5656
```ts
57-
import { Container, getContainer } from "@cloudflare/containers";
57+
import { Container, getContainer } from '@cloudflare/containers';
5858

5959
export class CronContainer extends Container {
60-
sleepAfter = "5m";
60+
sleepAfter = '10s';
61+
62+
override onStart() {
63+
console.log('Starting container');
64+
}
65+
66+
override onStop() {
67+
console.log('Contianer Stopped');
68+
}
6169
}
6270

6371
export default {
64-
async fetch(): Promise<Response> {
65-
return new Response(
66-
"This Worker runs a cron job to execute a container on a schedule.",
67-
);
68-
},
72+
async fetch(): Promise<Response> {
73+
return new Response("This Worker runs a cron job to execute a container on a schedule.");
74+
},
6975

70-
// scheduled is called when a cron trigger fires
71-
async scheduled(
72-
_controller: any,
73-
env: { CRON_CONTAINER: DurableObjectNamespace<CronContainer> },
74-
) {
75-
await getContainer(env.CRON_CONTAINER).startAndWaitForPorts({
76-
startOptions: {
77-
envVars: {
78-
MESSAGE: "Start Time: " + new Date().toISOString(),
79-
},
80-
},
81-
});
82-
},
76+
async scheduled(_controller: any, env: { CRON_CONTAINER: DurableObjectNamespace<CronContainer> }) {
77+
let container = getContainer(env.CRON_CONTAINER);
78+
await container.start({
79+
envVars: {
80+
LOCATION: "The Cloudflare San Francisco Office",
81+
LATITUDE: "37.780259",
82+
LONGITUDE: "-122.390519",
83+
}
84+
})
85+
},
8386
};
8487
```

0 commit comments

Comments
 (0)