Merge pull request #46 from syumai/move-cron-into-cloudflare

move cron into cloudflare
This commit is contained in:
syumai 2023-04-30 11:16:32 +09:00 committed by GitHub
commit ac05c2a9ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 22 deletions

View File

@ -29,6 +29,7 @@
* [x] D1 (alpha)
* [x] Environment variables
* [x] FetchEvent
* [x] Cron Triggers
## Installation

View File

@ -1,4 +1,4 @@
package workers
package cron
import (
"context"
@ -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
}

View File

@ -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.Event) 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)
}