add: cron job (#2)

This commit is contained in:
来自村里的小螃蟹 2021-07-08 14:05:14 +08:00 committed by GitHub
parent 914e93086a
commit 85ccd47bb6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 40 additions and 1 deletions

1
go.mod
View File

@ -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

22
initialize/cron.go Normal file
View File

@ -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 {}
}

15
internal/job/test_job.go Normal file
View File

@ -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")
}

View File

@ -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()