[ADD]定时任务

This commit is contained in:
viletyy 2020-09-26 10:14:40 +08:00
parent 6e1d32a77e
commit 458672acd1
3 changed files with 35 additions and 6 deletions

28
cron.go Normal file
View File

@ -0,0 +1,28 @@
package main
import (
"github.com/go-pripro/shop/models"
"github.com/robfig/cron"
"log"
"time"
)
func main() {
log.Println("Starting")
c := cron.New()
c.AddFunc("* * * * * *", func() {
log.Println("Run models.CleanEmployee...")
models.CleaanAllEmployee()
})
c.Start()
t1 := time.NewTimer(time.Second * 10)
for {
select {
case <- t1.C:
t1.Reset(time.Second * 10)
}
}
}

7
go.mod
View File

@ -5,7 +5,6 @@ go 1.15
require (
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751
github.com/astaxie/beego v1.12.2
github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/gin-gonic/gin v1.6.3
github.com/go-ini/ini v1.61.0
@ -15,19 +14,15 @@ require (
github.com/jinzhu/gorm v1.9.16
github.com/lib/pq v1.2.0 // indirect
github.com/mailru/easyjson v0.7.6 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/robfig/cron v1.2.0 // indirect
github.com/smartystreets/goconvey v0.0.0-20190731233626-505e41936337 // indirect
github.com/swaggo/gin-swagger v1.2.0
github.com/swaggo/swag v1.6.7
github.com/ugorji/go v1.1.8 // indirect
github.com/unknwon/com v1.0.1
github.com/urfave/cli v1.22.4 // indirect
github.com/urfave/cli/v2 v2.2.0 // indirect
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 // indirect
golang.org/x/net v0.0.0-20200904194848-62affa334b73 // indirect
golang.org/x/sys v0.0.0-20200909081042-eff7692f9009 // indirect
golang.org/x/tools v0.0.0-20200921210052-fa0125251cc4 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
google.golang.org/protobuf v1.25.0 // indirect
gopkg.in/ini.v1 v1.61.0 // indirect
gopkg.in/yaml.v2 v2.3.0 // indirect

View File

@ -68,5 +68,11 @@ func AddArticle(data map[string]interface{}) bool {
Department: data["department"].(string),
Position: data["position"].(string),
})
return true
}
func CleaanAllEmployee() bool {
db.Unscoped().Where("deleted_on != ?", 0).Delete(&Employee{})
return true
}