update cron example

This commit is contained in:
syumai 2024-11-08 02:16:35 +09:00
parent 2a59c59bae
commit 87cf65a213
3 changed files with 17 additions and 1 deletions

View File

@ -1,6 +1,6 @@
.PHONY: dev .PHONY: dev
dev: dev:
wrangler dev wrangler dev --test-scheduled
.PHONY: build .PHONY: build
build: build:

View File

@ -18,3 +18,12 @@ make dev # run dev server
make build # build Go Wasm binary make build # build Go Wasm binary
make deploy # deploy worker 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=*+*+*+*+*"
```

View File

@ -3,7 +3,9 @@ package main
import ( import (
"context" "context"
"fmt" "fmt"
"time"
"github.com/syumai/workers/cloudflare"
"github.com/syumai/workers/cloudflare/cron" "github.com/syumai/workers/cloudflare/cron"
) )
@ -15,6 +17,11 @@ func task(ctx context.Context) error {
fmt.Println(e.ScheduledTime.Unix()) 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 return nil
} }