mirror of
https://github.com/syumai/workers.git
synced 2025-03-10 17:29:11 +00:00
add template for cron jobs creation
This commit is contained in:
parent
6d313765fb
commit
53e3359fb9
3
_templates/cloudflare/cron-go/.gitignore
vendored
Normal file
3
_templates/cloudflare/cron-go/.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
build
|
||||
node_modules
|
||||
.wrangler
|
12
_templates/cloudflare/cron-go/Makefile
Normal file
12
_templates/cloudflare/cron-go/Makefile
Normal file
@ -0,0 +1,12 @@
|
||||
.PHONY: dev
|
||||
dev:
|
||||
wrangler dev
|
||||
|
||||
.PHONY: build
|
||||
build:
|
||||
go run github.com/syumai/workers/cmd/workers-assets-gen@v0.23.1 -mode=go
|
||||
GOOS=js GOARCH=wasm go build -o ./build/app.wasm .
|
||||
|
||||
.PHONY: deploy
|
||||
deploy:
|
||||
wrangler deploy
|
61
_templates/cloudflare/cron-go/README.md
Normal file
61
_templates/cloudflare/cron-go/README.md
Normal file
@ -0,0 +1,61 @@
|
||||
# cron-job-template-go
|
||||
|
||||
- A template for starting a Cloudflare Worker project with a cron job using Go.
|
||||
- This template uses the [workers](https://github.com/syumai/workers) package to schedule and run cron jobs.
|
||||
|
||||
## Notice
|
||||
|
||||
- A free plan Cloudflare Workers only accepts ~1MB sized workers.
|
||||
- Go Wasm binaries easily exceed this limit, so *you'll need to use a paid plan of Cloudflare Workers* (which accepts ~5MB sized workers).
|
||||
- There's also a Tinygo version of this that can be found [here](https://github.com/syumai/workers/tree/main/_templates/cloudflare/cron-tiny-go).
|
||||
|
||||
## Usage
|
||||
|
||||
- `main.go` includes a simple cron job implementation. Feel free to edit this code and implement your own cron job logic.
|
||||
|
||||
## Requirements
|
||||
|
||||
- Node.js
|
||||
- [wrangler](https://developers.cloudflare.com/workers/wrangler/)
|
||||
- Just run `npm install -g wrangler`
|
||||
- Go 1.21.0 or later
|
||||
|
||||
## Getting Started
|
||||
|
||||
* If not already installed, please install the [gonew](https://pkg.go.dev/golang.org/x/tools/cmd/gonew) command.
|
||||
|
||||
```console
|
||||
go install golang.org/x/tools/cmd/gonew@latest
|
||||
```
|
||||
|
||||
* Create a new project using this template.
|
||||
- The second argument passed to `gonew` is the module path of your new app.
|
||||
|
||||
```console
|
||||
gonew github.com/syumai/workers/_templates/cloudflare/cron-go your.module/my-app # e.g. github.com/syumai/my-app
|
||||
cd my-app
|
||||
go mod tidy
|
||||
make dev # start running dev server
|
||||
```
|
||||
|
||||
- To change the worker name, please edit the `name` property in `wrangler.toml`.
|
||||
|
||||
## Development
|
||||
|
||||
### Commands
|
||||
|
||||
```console
|
||||
make dev # run dev server
|
||||
make build # build Go Wasm binary
|
||||
make deploy # deploy worker
|
||||
```
|
||||
|
||||
### Testing the Dev Server
|
||||
|
||||
- To test the cron job, you can simulate the cron event by sending an HTTP request to the dev server.
|
||||
|
||||
```console
|
||||
curl -X POST http://localhost:8787/cron
|
||||
```
|
||||
|
||||
- You should see the scheduled time printed in the console.
|
7
_templates/cloudflare/cron-go/go.mod
Normal file
7
_templates/cloudflare/cron-go/go.mod
Normal file
@ -0,0 +1,7 @@
|
||||
module github.com/syumai/workers/_templates/cloudflare/cron-go
|
||||
|
||||
go 1.21.3
|
||||
|
||||
toolchain go1.22.4
|
||||
|
||||
require github.com/syumai/workers v0.26.1
|
2
_templates/cloudflare/cron-go/go.sum
Normal file
2
_templates/cloudflare/cron-go/go.sum
Normal file
@ -0,0 +1,2 @@
|
||||
github.com/syumai/workers v0.26.1 h1:DvhLZ4PPO/zu5leYRd85TnHELOBTkBbi/2ymkLOieSY=
|
||||
github.com/syumai/workers v0.26.1/go.mod h1:ZnqmdiHNBrbxOLrZ/HJ5jzHy6af9cmiNZk10R9NrIEA=
|
25
_templates/cloudflare/cron-go/main.go
Normal file
25
_templates/cloudflare/cron-go/main.go
Normal file
@ -0,0 +1,25 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/syumai/workers/cloudflare/cron"
|
||||
)
|
||||
|
||||
func task(ctx context.Context) error {
|
||||
e, err := cron.NewEvent(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create cron event: %w", err)
|
||||
}
|
||||
log.Printf("Cron job triggered at: %s", time.Unix(e.ScheduledTime.Unix(), 0).Format(time.RFC3339))
|
||||
log.Println("Executing scheduled task...")
|
||||
return nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
cron.ScheduleTask(task)
|
||||
select {}
|
||||
}
|
10
_templates/cloudflare/cron-go/wrangler.toml
Normal file
10
_templates/cloudflare/cron-go/wrangler.toml
Normal file
@ -0,0 +1,10 @@
|
||||
name = "go-cron"
|
||||
main = "./build/worker.mjs"
|
||||
compatibility_date = "2023-02-24"
|
||||
workers_dev = false
|
||||
|
||||
[triggers]
|
||||
crons = ["* * * * *"]
|
||||
|
||||
[build]
|
||||
command = "make build"
|
3
_templates/cloudflare/cron-tiny-go/.gitignore
vendored
Normal file
3
_templates/cloudflare/cron-tiny-go/.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
build
|
||||
node_modules
|
||||
.wrangler
|
12
_templates/cloudflare/cron-tiny-go/Makefile
Normal file
12
_templates/cloudflare/cron-tiny-go/Makefile
Normal file
@ -0,0 +1,12 @@
|
||||
.PHONY: dev
|
||||
dev:
|
||||
wrangler dev
|
||||
|
||||
.PHONY: build
|
||||
build:
|
||||
go run github.com/syumai/workers/cmd/workers-assets-gen@v0.23.1
|
||||
tinygo build -o ./build/app.wasm -target wasm -no-debug ./...
|
||||
|
||||
.PHONY: deploy
|
||||
deploy:
|
||||
wrangler deploy
|
61
_templates/cloudflare/cron-tiny-go/README.md
Normal file
61
_templates/cloudflare/cron-tiny-go/README.md
Normal file
@ -0,0 +1,61 @@
|
||||
# cron-tiny-go
|
||||
|
||||
- A template for starting a Cloudflare Worker project with a cron job using Go.
|
||||
- This template uses the [workers](https://github.com/syumai/workers) package to schedule and run cron jobs.
|
||||
|
||||
## Notice
|
||||
|
||||
- A free plan Cloudflare Workers only accepts ~1MB sized workers.
|
||||
- Tiny-go Wasm binaries probably won't exceed this limit, so you might not need to use a paid plan of Cloudflare Workers.
|
||||
- There's also a Go version of this that can be found [here](https://github.com/syumai/workers/tree/main/_templates/cloudflare/cron-go).
|
||||
|
||||
## Usage
|
||||
|
||||
- `main.go` includes a simple cron job implementation. Feel free to edit this code and implement your own cron job logic.
|
||||
|
||||
## Requirements
|
||||
|
||||
- Node.js
|
||||
- [wrangler](https://developers.cloudflare.com/workers/wrangler/)
|
||||
- Just run `npm install -g wrangler`
|
||||
- Go 1.21.0 or later
|
||||
|
||||
## Getting Started
|
||||
|
||||
* If not already installed, please install the [gonew](https://pkg.go.dev/golang.org/x/tools/cmd/gonew) command.
|
||||
|
||||
```console
|
||||
go install golang.org/x/tools/cmd/gonew@latest
|
||||
```
|
||||
|
||||
* Create a new project using this template.
|
||||
- The second argument passed to `gonew` is the module path of your new app.
|
||||
|
||||
```console
|
||||
gonew github.com/syumai/workers/_templates/cloudflare/cron-go your.module/my-app # e.g. github.com/syumai/my-app
|
||||
cd my-app
|
||||
go mod tidy
|
||||
make dev # start running dev server
|
||||
```
|
||||
|
||||
- To change the worker name, please edit the `name` property in `wrangler.toml`.
|
||||
|
||||
## Development
|
||||
|
||||
### Commands
|
||||
|
||||
```console
|
||||
make dev # run dev server
|
||||
make build # build Go Wasm binary
|
||||
make deploy # deploy worker
|
||||
```
|
||||
|
||||
### Testing the Dev Server
|
||||
|
||||
- To test the cron job, you can simulate the cron event by sending an HTTP request to the dev server.
|
||||
|
||||
```console
|
||||
curl -X POST http://localhost:8787/cron
|
||||
```
|
||||
|
||||
- You should see the scheduled time printed in the console.
|
7
_templates/cloudflare/cron-tiny-go/go.mod
Normal file
7
_templates/cloudflare/cron-tiny-go/go.mod
Normal file
@ -0,0 +1,7 @@
|
||||
module github.com/syumai/workers/_templates/cloudflare/cron-tiny-go
|
||||
|
||||
go 1.21.3
|
||||
|
||||
toolchain go1.22.4
|
||||
|
||||
require github.com/syumai/workers v0.26.1
|
2
_templates/cloudflare/cron-tiny-go/go.sum
Normal file
2
_templates/cloudflare/cron-tiny-go/go.sum
Normal file
@ -0,0 +1,2 @@
|
||||
github.com/syumai/workers v0.26.1 h1:DvhLZ4PPO/zu5leYRd85TnHELOBTkBbi/2ymkLOieSY=
|
||||
github.com/syumai/workers v0.26.1/go.mod h1:ZnqmdiHNBrbxOLrZ/HJ5jzHy6af9cmiNZk10R9NrIEA=
|
25
_templates/cloudflare/cron-tiny-go/main.go
Normal file
25
_templates/cloudflare/cron-tiny-go/main.go
Normal file
@ -0,0 +1,25 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/syumai/workers/cloudflare/cron"
|
||||
)
|
||||
|
||||
func task(ctx context.Context) error {
|
||||
e, err := cron.NewEvent(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create cron event: %w", err)
|
||||
}
|
||||
log.Printf("Cron job triggered at: %s", time.Unix(e.ScheduledTime.Unix(), 0).Format(time.RFC3339))
|
||||
log.Println("Executing scheduled task...")
|
||||
return nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
cron.ScheduleTask(task)
|
||||
select {}
|
||||
}
|
10
_templates/cloudflare/cron-tiny-go/wrangler.toml
Normal file
10
_templates/cloudflare/cron-tiny-go/wrangler.toml
Normal file
@ -0,0 +1,10 @@
|
||||
name = "tiny-go-cron"
|
||||
main = "./build/worker.mjs"
|
||||
compatibility_date = "2023-02-24"
|
||||
workers_dev = false
|
||||
|
||||
[triggers]
|
||||
crons = ["* * * * *"]
|
||||
|
||||
[build]
|
||||
command = "make build"
|
Loading…
x
Reference in New Issue
Block a user