From fc07f1d66edd4afd6f54646d39064917530dfefd Mon Sep 17 00:00:00 2001 From: syumai Date: Fri, 8 Nov 2024 02:33:41 +0900 Subject: [PATCH] add multiple-handlers example --- _examples/multiple-handlers/.gitignore | 3 ++ _examples/multiple-handlers/Makefile | 12 +++++++ _examples/multiple-handlers/README.md | 29 +++++++++++++++++ _examples/multiple-handlers/go.mod | 7 +++++ _examples/multiple-handlers/go.sum | 0 _examples/multiple-handlers/main.go | 38 +++++++++++++++++++++++ _examples/multiple-handlers/wrangler.toml | 10 ++++++ 7 files changed, 99 insertions(+) create mode 100644 _examples/multiple-handlers/.gitignore create mode 100644 _examples/multiple-handlers/Makefile create mode 100644 _examples/multiple-handlers/README.md create mode 100644 _examples/multiple-handlers/go.mod create mode 100644 _examples/multiple-handlers/go.sum create mode 100644 _examples/multiple-handlers/main.go create mode 100644 _examples/multiple-handlers/wrangler.toml diff --git a/_examples/multiple-handlers/.gitignore b/_examples/multiple-handlers/.gitignore new file mode 100644 index 0000000..aee7b7e --- /dev/null +++ b/_examples/multiple-handlers/.gitignore @@ -0,0 +1,3 @@ +build +node_modules +.wrangler diff --git a/_examples/multiple-handlers/Makefile b/_examples/multiple-handlers/Makefile new file mode 100644 index 0000000..eaa9356 --- /dev/null +++ b/_examples/multiple-handlers/Makefile @@ -0,0 +1,12 @@ +.PHONY: dev +dev: + wrangler dev --test-scheduled + +.PHONY: build +build: + go run ../../cmd/workers-assets-gen + tinygo build -o ./build/app.wasm -target wasm -no-debug ./... + +.PHONY: deploy +deploy: + wrangler deploy diff --git a/_examples/multiple-handlers/README.md b/_examples/multiple-handlers/README.md new file mode 100644 index 0000000..768d335 --- /dev/null +++ b/_examples/multiple-handlers/README.md @@ -0,0 +1,29 @@ +# multiple-handlers + +* This example shows how to use multiple handlers in a single worker. + +## 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 deploy # deploy worker +``` + +#### Testing cron schedule + +* With curl command below, you can test the cron schedule. + - see: https://developers.cloudflare.com/workers/runtime-apis/handlers/scheduled/#background + +``` +curl "http://localhost:8787/__scheduled?cron=*+*+*+*+*" +``` diff --git a/_examples/multiple-handlers/go.mod b/_examples/multiple-handlers/go.mod new file mode 100644 index 0000000..4066289 --- /dev/null +++ b/_examples/multiple-handlers/go.mod @@ -0,0 +1,7 @@ +module github.com/syumai/workers/_examples/multiple-handlers + +go 1.21.3 + +require github.com/syumai/workers v0.0.0 + +replace github.com/syumai/workers => ../../ diff --git a/_examples/multiple-handlers/go.sum b/_examples/multiple-handlers/go.sum new file mode 100644 index 0000000..e69de29 diff --git a/_examples/multiple-handlers/main.go b/_examples/multiple-handlers/main.go new file mode 100644 index 0000000..ff94f6f --- /dev/null +++ b/_examples/multiple-handlers/main.go @@ -0,0 +1,38 @@ +package main + +import ( + "context" + "fmt" + "net/http" + + "github.com/syumai/workers" + "github.com/syumai/workers/cloudflare/cron" +) + +func main() { + handler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { + fmt.Fprint(w, "Hello, world!") + }) + + task := func(ctx context.Context) error { + e, err := cron.NewEvent(ctx) + if err != nil { + return err + } + fmt.Println(e.ScheduledTime.Unix()) + return nil + } + + // set up the worker + workers.ServeNonBlock(handler) + cron.ScheduleTaskNonBlock(task) + + // send a ready signal to the runtime + workers.Ready() + + // block until the handler or task is done + select { + case <-workers.Done(): + case <-cron.Done(): + } +} diff --git a/_examples/multiple-handlers/wrangler.toml b/_examples/multiple-handlers/wrangler.toml new file mode 100644 index 0000000..c0dc4c1 --- /dev/null +++ b/_examples/multiple-handlers/wrangler.toml @@ -0,0 +1,10 @@ +name = "multiple-handlers" +main = "./build/worker.mjs" +compatibility_date = "2023-02-24" +workers_dev = false + +[triggers] +crons = ["* * * * *"] + +[build] +command = "make build"