0Pricing
Edge Computing with Cloudflare Workers & Deno · บทเรียน

ทริกเกอร์ Cron และ Workers ตามกำหนดเวลา

เรียกใช้ Cloudflare Workers ตามกำหนดเวลาด้วยทริกเกอร์ Cron เพื่อทำงานเบื้องหลังที่เกิดซ้ำโดยอัตโนมัติที่เอดจ์

ทริกเกอร์ Cron และ Workers ตามกำหนดเวลา เป็นบทเรียน Edge Computing with Cloudflare Workers & Deno ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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 minutes
  • 0 9 * * 1 means 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 pretty

Limits & 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.cron and keep work alive with ctx.waitUntil()
  • Test with --test-scheduled and monitor with wrangler tail

Cron Triggers turn Workers into reliable edge-native background job runners.

คำถามที่พบบ่อย

บทเรียน “ทริกเกอร์ Cron และ Workers ตามกำหนดเวลา” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “ทริกเกอร์ Cron และ Workers ตามกำหนดเวลา” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Edge Computing with Cloudflare Workers & Deno ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Edge Computing with Cloudflare Workers & Deno มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “ทริกเกอร์ Cron และ Workers ตามกำหนดเวลา”

เรียกใช้ Cloudflare Workers ตามกำหนดเวลาด้วยทริกเกอร์ Cron เพื่อทำงานเบื้องหลังที่เกิดซ้ำโดยอัตโนมัติที่เอดจ์ คุณปฏิบัติ Edge Computing with Cloudflare Workers & Deno ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Edge Computing with Cloudflare Workers & Deno หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Edge Computing with Cloudflare Workers & Deno บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “ทริกเกอร์ Cron และ Workers ตามกำหนดเวลา” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Edge Computing with Cloudflare Workers & Deno นี้ได้ไหม

ได้ บทเรียน Edge Computing with Cloudflare Workers & Deno ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. WebSockets และแบบเรียลไทม์
  2. คิวและงานแบบอะซิงโครนัส
  3. การเชื่อมโยงบริการและการผสานระบบ
  4. ทริกเกอร์ Cron และ Workers ตามกำหนดเวลา
← กลับไปที่ Edge Computing with Cloudflare Workers & Deno