mirror of
https://github.com/syumai/workers.git
synced 2025-03-10 17:29:11 +00:00
F: add cron example
This commit is contained in:
parent
683e5e9a65
commit
e068d7a414
1
examples/cron/.gitignore
vendored
Normal file
1
examples/cron/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
build
|
12
examples/cron/Makefile
Normal file
12
examples/cron/Makefile
Normal 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
20
examples/cron/README.md
Normal 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
7
examples/cron/go.mod
Normal 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
0
examples/cron/go.sum
Normal file
24
examples/cron/main.go
Normal file
24
examples/cron/main.go
Normal 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)
|
||||
}
|
13
examples/cron/wrangler.toml
Normal file
13
examples/cron/wrangler.toml
Normal 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"
|
Loading…
x
Reference in New Issue
Block a user