Cron 트리거 및 예약된 워커
Cron 트리거로 클라우드플레어 워커를 일정에 따라 실행하여 엣지에서 반복되는 백그라운드 작업을 자동화합니다.
Cron 트리거 및 예약된 워커은(는) CoddyKit의 무료 Edge Computing with Cloudflare Workers & Deno 강의입니다. 이것은 4개 중 4번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Edge Computing with Cloudflare Workers & Deno 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Edge Computing with Cloudflare Workers & Deno 강의에는 총 4개의 강의가 포함되어 있습니다.
이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.
Why Scheduled Workers?
Beyond responding to HTTP requests, Workers can run on a schedule using Cron Triggers.
This is perfect for recurring jobs such as:
- Cleaning up stale data in KV or D1
- Sending daily report emails
- Refreshing cached API responses
- Aggregating analytics
No always-on server required, the Worker simply wakes up, runs, and goes back to sleep.
The scheduled() Handler
A scheduled Worker exports a scheduled() handler instead of (or alongside) fetch().
Cloudflare invokes it automatically at the times you define in cron expressions.
export default {
async scheduled(event, env, ctx) {
console.log('Triggered at: ' + event.scheduledTime);
}
};Configuring Cron in wrangler.toml
Cron schedules live in your wrangler.toml under the [triggers] section.
You can declare multiple cron expressions, each one fires the same scheduled() handler.
[triggers]
crons = ["0 * * * *", "*/15 * * * *"]Cron Expression Syntax
Cloudflare cron expressions have five fields: minute, hour, day-of-month, month, day-of-week.
0 0 * * *means midnight UTC every day*/5 * * * *means every 5 minutes0 9 * * 1means 09:00 UTC every Monday
All times are in UTC.
Distinguishing Multiple Crons
When you register several cron schedules, use event.cron to tell which one fired.
This lets one Worker handle many jobs.
export default {
async scheduled(event, env, ctx) {
if (event.cron === '0 0 * * *') {
await runDailyCleanup(env);
} else if (event.cron === '*/15 * * * *') {
await refreshCache(env);
}
}
};Using waitUntil for Long Tasks
Just like in fetch(), the ctx.waitUntil() method keeps the Worker alive until a promise resolves.
Use it to ensure background async work completes before the invocation ends.
async scheduled(event, env, ctx) {
ctx.waitUntil(syncRemoteData(env));
}Accessing Bindings in scheduled()
The env argument exposes all your bindings: KV, D1, R2, Queues, secrets.
A scheduled cleanup might delete expired KV keys.
async scheduled(event, env, ctx) {
const list = await env.MY_KV.list({ prefix: 'session:' });
for (const key of list.keys) {
await env.MY_KV.delete(key.name);
}
}Testing Crons Locally
Wrangler lets you test scheduled handlers without waiting for the real schedule.
Run wrangler dev and trigger via the test endpoint, or use the CLI flag.
wrangler dev --test-scheduled
# then visit:
# http://localhost:8787/__scheduled?cron=0+*+*+*+*Combining fetch() and scheduled()
A single Worker can export both handlers.
This is handy when the same logic serves HTTP requests and runs on a timer.
export default {
async fetch(request, env) {
return new Response('Hello from fetch');
},
async scheduled(event, env, ctx) {
console.log('Hello from cron');
}
};Monitoring Scheduled Runs
Cron invocations appear in the Cloudflare dashboard and in wrangler tail logs.
Watch for failures, a thrown error means the job did not complete, and there is no automatic retry for cron triggers.
wrangler tail --format prettyLimits & Best Practices
Keep scheduled jobs efficient:
- Stay within CPU time limits, offload heavy work to Queues
- Make jobs idempotent so reruns are safe
- Avoid sub-minute schedules unless necessary
- Log results for observability
Quick Check
Which property tells you which cron schedule triggered the handler?
Recap
You learned how to run Workers on a schedule:
- Export a
scheduled()handler - Define schedules in
[triggers] crons - Use five-field UTC cron expressions
- Route with
event.cronand keep work alive withctx.waitUntil() - Test with
--test-scheduledand monitor withwrangler tail
Cron Triggers turn Workers into reliable edge-native background job runners.
자주 묻는 질문
“Cron 트리거 및 예약된 워커” 강의는 무료인가요?
네 — “Cron 트리거 및 예약된 워커” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Edge Computing with Cloudflare Workers & Deno 강의 전체를 잠금 해제할 수 있습니다. Edge Computing with Cloudflare Workers & Deno 강의에는 총 4개의 강의가 포함되어 있습니다.
“Cron 트리거 및 예약된 워커”에서 뭘 배우나요?
Cron 트리거로 클라우드플레어 워커를 일정에 따라 실행하여 엣지에서 반복되는 백그라운드 작업을 자동화합니다. 브라우저에서 직접 실행하는 실습 코드로 Edge Computing with Cloudflare Workers & Deno을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.
Edge Computing with Cloudflare Workers & Deno을(를) 시작하는 데 경험이 필요한가요?
사전 경험은 필요하지 않습니다. CoddyKit의 Edge Computing with Cloudflare Workers & Deno은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 4번째 강의입니다.
“Cron 트리거 및 예약된 워커” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 Edge Computing with Cloudflare Workers & Deno 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 Edge Computing with Cloudflare Workers & Deno 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.
이 강의의 모든 강의
- WebSockets 및 실시간 기능
- 대기열 및 비동기 작업
- 서비스 바인딩 및 통합
- Cron 트리거 및 예약된 워커