F: add cron example

This commit is contained in:
aki-0421 2023-04-30 10:35:04 +09:00
parent 683e5e9a65
commit e068d7a414
No known key found for this signature in database
GPG Key ID: 64A8CF6D437D166A
7 changed files with 77 additions and 0 deletions

1
examples/cron/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
build

12
examples/cron/Makefile Normal file
View File

@ -0,0 +1,12 @@
.PHONY: dev
dev:
wrangler dev
.PHONY: build
build:
go run ../../cmd/workers-assets-gen
tinygo build -o ./build/app.wasm -target wasm ./...
.PHONY: publish
publish:
wrangler publish

20
examples/cron/README.md Normal file
View File

@ -0,0 +1,20 @@
# [Cron](https://developers.cloudflare.com/workers/platform/triggers/cron-triggers/)
* Create a worker to be triggered by Cron.
## Development
### Requirements
This project requires these tools to be installed globally.
* wrangler
* tinygo
### Commands
```
make dev # run dev server
make build # build Go Wasm binary
make publish # publish worker
```

7
examples/cron/go.mod Normal file
View File

@ -0,0 +1,7 @@
module github.com/syumai/workers/examples/scheduled
go 1.18
require github.com/syumai/workers v0.0.0
replace github.com/syumai/workers => ../../

0
examples/cron/go.sum Normal file
View File

24
examples/cron/main.go Normal file
View File

@ -0,0 +1,24 @@
package main
import (
"context"
"errors"
"fmt"
"github.com/syumai/workers"
"github.com/syumai/workers/cloudflare"
)
func task(ctx context.Context, event workers.CronEvent) error {
fmt.Println(cloudflare.Getenv(ctx, "HELLO"))
if event.ScheduledTime.Minute()%2 == 0 {
return errors.New("even numbers cause errors")
}
return nil
}
func main() {
workers.ScheduleTask(task)
}

View File

@ -0,0 +1,13 @@
name = "scheduled"
main = "./build/worker.mjs"
compatibility_date = "2023-02-24"
workers_dev = false
[vars]
HELLO = "hello, world!"
[triggers]
crons = ["* * * * *"]
[build]
command = "make build"