From 87cf65a213f19d3be2b2204b28db4fab164d0920 Mon Sep 17 00:00:00 2001 From: syumai Date: Fri, 8 Nov 2024 02:16:35 +0900 Subject: [PATCH] update cron example --- _examples/cron/Makefile | 2 +- _examples/cron/README.md | 9 +++++++++ _examples/cron/main.go | 7 +++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/_examples/cron/Makefile b/_examples/cron/Makefile index 019492c..eaa9356 100644 --- a/_examples/cron/Makefile +++ b/_examples/cron/Makefile @@ -1,6 +1,6 @@ .PHONY: dev dev: - wrangler dev + wrangler dev --test-scheduled .PHONY: build build: diff --git a/_examples/cron/README.md b/_examples/cron/README.md index 014e4a7..b31da2f 100644 --- a/_examples/cron/README.md +++ b/_examples/cron/README.md @@ -18,3 +18,12 @@ make dev # run dev server make build # build Go Wasm binary make deploy # deploy worker ``` + +#### Testing cron schedule + +* With curl command below, you can test the cron schedule. + - see: https://developers.cloudflare.com/workers/runtime-apis/handlers/scheduled/#background + +``` +curl "http://localhost:8787/__scheduled?cron=*+*+*+*+*" +``` diff --git a/_examples/cron/main.go b/_examples/cron/main.go index 73c3ec9..61f2b08 100644 --- a/_examples/cron/main.go +++ b/_examples/cron/main.go @@ -3,7 +3,9 @@ package main import ( "context" "fmt" + "time" + "github.com/syumai/workers/cloudflare" "github.com/syumai/workers/cloudflare/cron" ) @@ -15,6 +17,11 @@ func task(ctx context.Context) error { fmt.Println(e.ScheduledTime.Unix()) + cloudflare.WaitUntil(func() { + time.Sleep(1 * time.Second) + fmt.Println("Run sub task after returning from main task") + }) + return nil }