DevToolBoxFREE
BlogAdvertise

Cron untuk Serverless: GitHub Actions, Vercel Cron, dan Cloudflare Workers

9 menit bacaoleh DevToolBox

Ekspresi cron adalah bahasa universal untuk menjadwalkan tugas berulang. Setiap platform serverless punya batasan sendiri.

Dasar Ekspresi Cron

Ekspresi cron standar punya 5 field:

┌───────────── minute (0-59)
│ ┌───────────── hour (0-23)
│ │ ┌───────────── day of month (1-31)
│ │ │ ┌───────────── month (1-12)
│ │ │ │ ┌───────────── day of week (0-6, Sun=0)
│ │ │ │ │
* * * * *

Karakter khusus umum:

  • *nilai apapun
  • */5setiap 5 unit
  • 1,3,5nilai spesifik
  • 1-5rentang nilai

Uji ekspresi cron dengan Cron Parser kami →

Platform 1: GitHub Actions

GitHub Actions memakai event schedule.

Konfigurasi

# .github/workflows/scheduled.yml
name: Scheduled Job
on:
  schedule:
    - cron: '30 5 * * 1-5'  # Weekdays at 5:30 AM UTC
    - cron: '0 12 1 * *'    # 1st of every month at noon UTC
jobs:
  run:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - run: echo "Running scheduled task"

Batasan Utama

AspekDetail
Zona waktuHanya UTC
Interval minSetiap 5 menit (*/5 * * * *)
AkurasiBisa tertunda
BranchHanya branch default
NonaktifSetelah 60 hari tidak aktif

Jadwal Umum

# Every 5 minutes
- cron: '*/5 * * * *'

# Every hour at minute 0
- cron: '0 * * * *'

# Daily at midnight UTC
- cron: '0 0 * * *'

# Daily at 9 AM Eastern (UTC-5, no DST)
- cron: '0 14 * * *'

# Weekdays at 8 AM UTC
- cron: '0 8 * * 1-5'

# Every Monday and Thursday at 6 PM UTC
- cron: '0 18 * * 1,4'

# First day of every month at midnight
- cron: '0 0 1 * *'

Tip: GitHub Actions bisa terlambat.

Platform 2: Vercel Cron Jobs

Konfigurasi di vercel.json.

Konfigurasi

// vercel.json
{
  "crons": [
    {
      "path": "/api/daily-report",
      "schedule": "0 8 * * *"
    },
    {
      "path": "/api/cleanup",
      "schedule": "0 0 * * 0"
    }
  ]
}

API Route

// app/api/daily-report/route.ts
import { NextResponse } from 'next/server';

export async function GET() {
  // Your scheduled logic here
  await sendDailyReport();
  return NextResponse.json({ ok: true });
}

// Vercel recommends setting maxDuration for long tasks
export const maxDuration = 60; // seconds

Batasan Utama

PlanInt. MinMax Cron JobsLimit Eksekusi
Hobby (free)Once per day210s
ProOnce per hour4060s (default)
EnterpriseOnce per minute100900s

Important: Vercel pakai UTC. Hobby: 1/hari.

Platform 3: Cloudflare Workers

Konfigurasi di wrangler.toml.

Konfigurasi

# wrangler.toml
name = "my-worker"
main = "src/index.ts"

[triggers]
crons = [
  "*/5 * * * *",    # Every 5 minutes
  "0 12 * * 1-5",   # Weekdays at noon UTC
  "0 0 1 * *"       # First of every month
]

Kode Worker

// src/index.ts
export default {
  async scheduled(
    controller: ScheduledController,
    env: Env,
    ctx: ExecutionContext
  ) {
    // controller.cron contains the cron pattern that triggered
    switch (controller.cron) {
      case '*/5 * * * *':
        await doHealthCheck(env);
        break;
      case '0 12 * * 1-5':
        await sendReport(env);
        break;
    }
  },
};

Batasan Utama

AspekDetail
Zona waktuHanya UTC
Interval min1x per menit
Max cron triggers3 per Worker (gratis)
CPU10ms/30s
AkurasiSangat andal

Perbandingan Platform

FiturGitHub ActionsVercel CronCF Workers
TimezoneUTCUTCUTC
Min interval (free)5 min1/day1 min
Min interval (paid)5 min1/min1 min
ReliabilityMedium (can delay)HighVery high
Max execution time6 hours10s-900s10ms-30s
Config location.github/workflows/vercel.jsonwrangler.toml
Trigger handlerWorkflow YAMLAPI routescheduled() event

Jebakan Zona Waktu

Ketiga platform pakai UTC eksklusif.

Waktu LokalOffset UTCCron (9 pagi lokal)
US Eastern (EST)UTC-50 14 * * *
US Pacific (PST)UTC-80 17 * * *
Central Europe (CET)UTC+10 8 * * *
China (CST)UTC+80 1 * * *
Japan (JST)UTC+90 0 * * *

Waktu musim panas mengubah offset UTC.

10 Ekspresi Cron Siap Pakai

Jadwal paling umum:

# Every 5 minutes
*/5 * * * *

# Every hour at :00
0 * * * *

# Every day at midnight UTC
0 0 * * *

# Every day at noon UTC
0 12 * * *

# Weekdays (Mon-Fri) at 9 AM UTC
0 9 * * 1-5

# Every Monday at 8 AM UTC
0 8 * * 1

# Every 6 hours
0 */6 * * *

# First day of every month at midnight
0 0 1 * *

# Every 15 minutes during business hours (8-17 UTC)
*/15 8-17 * * 1-5

# Last day of month at 11 PM UTC (approximate)
0 23 28-31 * *

Validasi ekspresi cron dengan Cron Parser →

Pertanyaan Umum

Zona waktu apa untuk GitHub Actions?

Selalu UTC.

Interval min Vercel Cron?

Hobby: 1/hari, Pro: 1/jam, Enterprise: 1/menit.

Cloudflare Workers tiap detik?

Tidak. Granularitas min: 1 menit.

Apakah ini membantu?

Stay Updated

Get weekly dev tips and new tool announcements.

No spam. Unsubscribe anytime.

Partner Picks

Sponsor this article

Place your product next to this developer topic with tracked clicks.

Ask about article sponsorship

This site uses cookies for analytics and to display ads. By continuing to browse, you agree. Privacy Policy