mirror of
https://github.com/syumai/workers.git
synced 2025-03-11 01:39:11 +00:00
Merge pull request #46 from syumai/move-cron-into-cloudflare
move cron into cloudflare
This commit is contained in:
commit
ac05c2a9ae
@ -29,6 +29,7 @@
|
|||||||
* [x] D1 (alpha)
|
* [x] D1 (alpha)
|
||||||
* [x] Environment variables
|
* [x] Environment variables
|
||||||
* [x] FetchEvent
|
* [x] FetchEvent
|
||||||
|
* [x] Cron Triggers
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package workers
|
package cron
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
@ -11,45 +11,43 @@ import (
|
|||||||
"github.com/syumai/workers/internal/runtimecontext"
|
"github.com/syumai/workers/internal/runtimecontext"
|
||||||
)
|
)
|
||||||
|
|
||||||
// CronEvent represents information about the Cron that invoked this worker.
|
// Event represents information about the Cron that invoked this worker.
|
||||||
type CronEvent struct {
|
type Event struct {
|
||||||
// Type string
|
|
||||||
Cron string
|
Cron string
|
||||||
ScheduledTime time.Time
|
ScheduledTime time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
// toGo converts JS Object to CronEvent
|
// toEvent converts JS Object to Go Event struct
|
||||||
func (ce *CronEvent) toGo(obj js.Value) error {
|
func toEvent(obj js.Value) (*Event, error) {
|
||||||
if obj.IsUndefined() {
|
if obj.IsUndefined() {
|
||||||
return errors.New("event is null")
|
return nil, errors.New("event is null")
|
||||||
}
|
}
|
||||||
cronVal := obj.Get("cron").String()
|
cronVal := obj.Get("cron").String()
|
||||||
ce.Cron = cronVal
|
|
||||||
scheduledTimeVal := obj.Get("scheduledTime").Float()
|
scheduledTimeVal := obj.Get("scheduledTime").Float()
|
||||||
ce.ScheduledTime = time.Unix(int64(scheduledTimeVal)/1000, 0).UTC()
|
return &Event{
|
||||||
|
Cron: cronVal,
|
||||||
return nil
|
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
|
// ScheduleTask sets the Task to be executed
|
||||||
func ScheduleTask(task CronFunc) {
|
func ScheduleTask(task Task) {
|
||||||
cronTask = task
|
scheduledTask = task
|
||||||
jsutil.Global.Call("ready")
|
jsutil.Global.Call("ready")
|
||||||
select {}
|
select {}
|
||||||
}
|
}
|
||||||
|
|
||||||
func runScheduler(eventObj js.Value, runtimeCtxObj js.Value) error {
|
func runScheduler(eventObj js.Value, runtimeCtxObj js.Value) error {
|
||||||
ctx := runtimecontext.New(context.Background(), runtimeCtxObj)
|
ctx := runtimecontext.New(context.Background(), runtimeCtxObj)
|
||||||
event := CronEvent{}
|
event, err := toEvent(eventObj)
|
||||||
err := event.toGo(eventObj)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
err = cronTask(ctx, event)
|
err = scheduledTask(ctx, event)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
@ -5,11 +5,11 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/syumai/workers"
|
|
||||||
"github.com/syumai/workers/cloudflare"
|
"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"))
|
fmt.Println(cloudflare.Getenv(ctx, "HELLO"))
|
||||||
|
|
||||||
if event.ScheduledTime.Minute()%2 == 0 {
|
if event.ScheduledTime.Minute()%2 == 0 {
|
||||||
@ -20,5 +20,5 @@ func task(ctx context.Context, event workers.CronEvent) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
workers.ScheduleTask(task)
|
cron.ScheduleTask(task)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user