From 85ccd47bb69b8f000b5f43478e548e9d17032e44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A5=E8=87=AA=E6=9D=91=E9=87=8C=E7=9A=84=E5=B0=8F?= =?UTF-8?q?=E8=9E=83=E8=9F=B9?= Date: Thu, 8 Jul 2021 14:05:14 +0800 Subject: [PATCH] add: cron job (#2) --- go.mod | 1 + initialize/cron.go | 22 ++++++++++++++++++++++ internal/job/test_job.go | 15 +++++++++++++++ main.go | 3 ++- 4 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 initialize/cron.go create mode 100644 internal/job/test_job.go diff --git a/go.mod b/go.mod index 11c2c62..abf0fbf 100644 --- a/go.mod +++ b/go.mod @@ -20,6 +20,7 @@ require ( github.com/onsi/ginkgo v1.16.4 // indirect github.com/onsi/gomega v1.13.0 // indirect github.com/opentracing/opentracing-go v1.2.0 + github.com/robfig/cron/v3 v3.0.0 github.com/smacker/opentracing-gorm v0.0.0-20181207094635-cd4974441042 // indirect github.com/soheilhy/cmux v0.1.5 github.com/spf13/viper v1.7.1 diff --git a/initialize/cron.go b/initialize/cron.go new file mode 100644 index 0000000..8060bc9 --- /dev/null +++ b/initialize/cron.go @@ -0,0 +1,22 @@ +/* + * @Date: 2021-07-08 13:54:38 + * @LastEditors: viletyy + * @LastEditTime: 2021-07-08 14:01:18 + * @FilePath: /potato/initialize/cron.go + */ +package initialize + +import ( + "github.com/robfig/cron/v3" + "github.com/viletyy/potato/internal/job" +) + +func Cron() { + c := cron.New() + + c.AddJob("* * * * *", job.TestJob{}) + + c.Start() + + select {} +} diff --git a/internal/job/test_job.go b/internal/job/test_job.go new file mode 100644 index 0000000..a3398aa --- /dev/null +++ b/internal/job/test_job.go @@ -0,0 +1,15 @@ +/* + * @Date: 2021-07-08 14:01:46 + * @LastEditors: viletyy + * @LastEditTime: 2021-07-08 14:02:36 + * @FilePath: /potato/internal/job/test_job.go + */ +package job + +import "fmt" + +type TestJob struct{} + +func (t TestJob) Run() { + fmt.Println("i'm test job") +} diff --git a/main.go b/main.go index 9323dbd..4f1ff71 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,7 @@ /* * @Date: 2021-03-21 19:54:57 * @LastEditors: viletyy - * @LastEditTime: 2021-06-20 19:45:38 + * @LastEditTime: 2021-07-08 13:59:06 * @FilePath: /potato/main.go */ package main @@ -33,6 +33,7 @@ func main() { global.GO_DB = initialize.Gorm() global.GO_REDIS = initialize.Redis() global.GO_TRACER = initialize.Tracer() + initialize.Cron() defer global.GO_DB.Close() defer global.GO_REDIS.Close()