From d3e1df72acaa8120c28e40166bd20974d90ae757 Mon Sep 17 00:00:00 2001 From: syumai Date: Sun, 30 Apr 2023 10:54:24 +0900 Subject: [PATCH 1/4] move cron into cloudflare package --- cron.go => cloudflare/cron/cron.go | 2 +- examples/cron/main.go | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) rename cron.go => cloudflare/cron/cron.go (99%) diff --git a/cron.go b/cloudflare/cron/cron.go similarity index 99% rename from cron.go rename to cloudflare/cron/cron.go index a320d2c..6470195 100644 --- a/cron.go +++ b/cloudflare/cron/cron.go @@ -1,4 +1,4 @@ -package workers +package cron import ( "context" diff --git a/examples/cron/main.go b/examples/cron/main.go index be86cd3..acb5f27 100644 --- a/examples/cron/main.go +++ b/examples/cron/main.go @@ -5,11 +5,11 @@ import ( "errors" "fmt" - "github.com/syumai/workers" "github.com/syumai/workers/cloudflare" + "github.com/syumai/workers/cloudflare/cron" ) -func task(ctx context.Context, event workers.CronEvent) error { +func task(ctx context.Context, event cron.CronEvent) error { fmt.Println(cloudflare.Getenv(ctx, "HELLO")) if event.ScheduledTime.Minute()%2 == 0 { @@ -20,5 +20,5 @@ func task(ctx context.Context, event workers.CronEvent) error { } func main() { - workers.ScheduleTask(task) + cron.ScheduleTask(task) } From 30fff6c5600ad4c57288670652ebf5b5c0da7080 Mon Sep 17 00:00:00 2001 From: syumai Date: Sun, 30 Apr 2023 11:00:00 +0900 Subject: [PATCH 2/4] rename some types in cron package --- cloudflare/cron/cron.go | 34 ++++++++++++++++------------------ examples/cron/main.go | 2 +- 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/cloudflare/cron/cron.go b/cloudflare/cron/cron.go index 6470195..a3cd47b 100644 --- a/cloudflare/cron/cron.go +++ b/cloudflare/cron/cron.go @@ -11,45 +11,43 @@ import ( "github.com/syumai/workers/internal/runtimecontext" ) -// CronEvent represents information about the Cron that invoked this worker. -type CronEvent struct { - // Type string +// Event represents information about the Cron that invoked this worker. +type Event struct { Cron string ScheduledTime time.Time } -// toGo converts JS Object to CronEvent -func (ce *CronEvent) toGo(obj js.Value) error { +// toEvent converts JS Object to Go Event struct +func toEvent(obj js.Value) (*Event, error) { if obj.IsUndefined() { - return errors.New("event is null") + return nil, errors.New("event is null") } cronVal := obj.Get("cron").String() - ce.Cron = cronVal scheduledTimeVal := obj.Get("scheduledTime").Float() - ce.ScheduledTime = time.Unix(int64(scheduledTimeVal)/1000, 0).UTC() - - return nil + return &Event{ + Cron: cronVal, + ScheduledTime: time.Unix(int64(scheduledTimeVal)/1000, 0).UTC(), + }, nil } -type CronFunc func(ctx context.Context, event CronEvent) error +type Task func(ctx context.Context, event *Event) error -var cronTask CronFunc +var scheduledTask Task -// ScheduleTask sets the CronFunc to be executed -func ScheduleTask(task CronFunc) { - cronTask = task +// ScheduleTask sets the Task to be executed +func ScheduleTask(task Task) { + scheduledTask = task jsutil.Global.Call("ready") select {} } func runScheduler(eventObj js.Value, runtimeCtxObj js.Value) error { ctx := runtimecontext.New(context.Background(), runtimeCtxObj) - event := CronEvent{} - err := event.toGo(eventObj) + event, err := toEvent(eventObj) if err != nil { return err } - err = cronTask(ctx, event) + err = scheduledTask(ctx, event) if err != nil { return err } diff --git a/examples/cron/main.go b/examples/cron/main.go index acb5f27..1931619 100644 --- a/examples/cron/main.go +++ b/examples/cron/main.go @@ -9,7 +9,7 @@ import ( "github.com/syumai/workers/cloudflare/cron" ) -func task(ctx context.Context, event cron.CronEvent) error { +func task(ctx context.Context, event cron.Event) error { fmt.Println(cloudflare.Getenv(ctx, "HELLO")) if event.ScheduledTime.Minute()%2 == 0 { From c3ffe351741d2c0fd5d894318d3574b0ba053435 Mon Sep 17 00:00:00 2001 From: syumai Date: Sun, 30 Apr 2023 11:01:41 +0900 Subject: [PATCH 3/4] update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 0fb36f6..967ee37 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,7 @@ * [x] D1 (alpha) * [x] Environment variables * [x] FetchEvent +* [x] Cron Triggers ## Installation From ea2393e691e3f744a4082a3d218a68a891bfa00d Mon Sep 17 00:00:00 2001 From: syumai Date: Sun, 30 Apr 2023 11:04:28 +0900 Subject: [PATCH 4/4] update example --- examples/cron/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/cron/main.go b/examples/cron/main.go index 1931619..0b01bf5 100644 --- a/examples/cron/main.go +++ b/examples/cron/main.go @@ -9,7 +9,7 @@ import ( "github.com/syumai/workers/cloudflare/cron" ) -func task(ctx context.Context, event cron.Event) error { +func task(ctx context.Context, event *cron.Event) error { fmt.Println(cloudflare.Getenv(ctx, "HELLO")) if event.ScheduledTime.Minute()%2 == 0 {