update cronjob
This commit is contained in:
@@ -13,6 +13,30 @@ import (
|
||||
func IntToString(n int) string {
|
||||
return strconv.Itoa(n)
|
||||
}
|
||||
|
||||
// StringToInt string转int
|
||||
func StringToInt(s string) int {
|
||||
i, err := strconv.Atoi(s)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return i
|
||||
}
|
||||
|
||||
// StringToInt64 string转int64
|
||||
func StringToInt64(s string) int64 {
|
||||
i, err := strconv.ParseInt(s, 10, 64)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return i
|
||||
}
|
||||
|
||||
// Int64ToString int64转string
|
||||
func Int64ToString(n int64) string {
|
||||
return strconv.FormatInt(n, 10)
|
||||
}
|
||||
|
||||
func mapToBytes(data map[string]interface{}) []byte {
|
||||
bytes, _ := json.Marshal(data)
|
||||
return bytes
|
||||
@@ -6,6 +6,10 @@ func ErrorLog(v ...interface{}) {
|
||||
gotool.Logs.ErrorLog().Println(v)
|
||||
}
|
||||
|
||||
func ErrorLogf(format string, v ...interface{}) {
|
||||
gotool.Logs.ErrorLog().Printf(format+"\n", v)
|
||||
}
|
||||
|
||||
func FatalfLog(format string, v ...interface{}) {
|
||||
gotool.Logs.ErrorLog().Fatalf(format, v)
|
||||
}
|
||||
|
||||
+24
-17
@@ -6,7 +6,6 @@ import (
|
||||
"cutego/core/service"
|
||||
"cutego/pkg/common"
|
||||
"github.com/robfig/cron"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Cron表达式参考
|
||||
@@ -23,20 +22,41 @@ import (
|
||||
// 定时任务: 别名与调度器的映射
|
||||
var AliasCronMap = make(map[string]*cron.Cron)
|
||||
|
||||
// 停止任务, 不会停止已开始的任务
|
||||
// StopCronFunc 停止任务, 不会停止已开始的任务
|
||||
func StopCronFunc(aliasName string) {
|
||||
common.InfoLogf("停止任务 %s ---> Start", aliasName)
|
||||
AliasCronMap[aliasName].Stop()
|
||||
common.InfoLogf("停止任务 %s ---> Finish", aliasName)
|
||||
}
|
||||
|
||||
// 开始任务
|
||||
// StartCronFunc 开始任务
|
||||
func StartCronFunc(aliasName string) {
|
||||
common.InfoLogf("唤起任务 %s ---> Start", aliasName)
|
||||
AliasCronMap[aliasName].Start()
|
||||
common.InfoLogf("唤起任务 %s ---> Finish", aliasName)
|
||||
}
|
||||
|
||||
// RemoveCronFunc 移除任务
|
||||
func RemoveCronFunc(aliasName string) {
|
||||
common.InfoLogf("移除任务 %s ---> Start", aliasName)
|
||||
StopCronFunc(aliasName)
|
||||
delete(AliasCronMap, aliasName)
|
||||
common.InfoLogf("移除任务 %s ---> Finish", aliasName)
|
||||
}
|
||||
|
||||
// AppendCronFunc 新增任务
|
||||
func AppendCronFunc(jobCron string, aliasName string, status string) {
|
||||
common.InfoLogf("新增任务 %s ---> Start", aliasName)
|
||||
c := cron.New()
|
||||
c.AddFunc(jobCron, job.AliasFuncMap[aliasName])
|
||||
if status == "1" {
|
||||
c.Start()
|
||||
common.InfoLogf("调度定时任务 --- %s ---> Success", aliasName)
|
||||
}
|
||||
AliasCronMap[aliasName] = c
|
||||
common.InfoLogf("新增任务 %s ---> Finish", aliasName)
|
||||
}
|
||||
|
||||
func init() {
|
||||
if len(job.AliasFuncMap) > 0 {
|
||||
//go test()
|
||||
@@ -49,22 +69,9 @@ func init() {
|
||||
break
|
||||
}
|
||||
for _, datum := range data {
|
||||
c := cron.New()
|
||||
c.AddFunc(datum.JobCron, job.AliasFuncMap[datum.FuncAlias])
|
||||
c.Start()
|
||||
|
||||
AliasCronMap[datum.FuncAlias] = c
|
||||
common.InfoLogf("调度定时任务 --- %s ---> Success", datum.JobName)
|
||||
AppendCronFunc(datum.JobCron, datum.FuncAlias, datum.Status)
|
||||
}
|
||||
index += 1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 测试通过
|
||||
func test() {
|
||||
time.Sleep(time.Second * 10)
|
||||
StopCronFunc("test1")
|
||||
time.Sleep(time.Second * 10)
|
||||
StartCronFunc("test1")
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
package util
|
||||
|
||||
import "cutego/pkg/page"
|
||||
|
||||
func NewPage(list []interface{}, total int64, pageSize int) page.Page {
|
||||
// 值对象, 不可变
|
||||
return page.Page{
|
||||
List: list,
|
||||
Total: total,
|
||||
Size: pageSize,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"cutego/pkg/common"
|
||||
"encoding/gob"
|
||||
)
|
||||
|
||||
// DeepCopy 深度拷贝对象
|
||||
// @Param src 原对象
|
||||
// @Param dst 目标对象
|
||||
// @Return 目标对象
|
||||
// @Usage util.DeepCopy(item, &response.CronJobPageResponse{})
|
||||
//
|
||||
// @Author tianjun@odboy.cn
|
||||
// @Date 2022-03-01
|
||||
func DeepCopy(src, dst interface{}) interface{} {
|
||||
var buf bytes.Buffer
|
||||
if err := gob.NewEncoder(&buf).Encode(src); err != nil {
|
||||
return err
|
||||
}
|
||||
err := gob.NewDecoder(bytes.NewBuffer(buf.Bytes())).Decode(dst)
|
||||
if err != nil {
|
||||
common.ErrorLogf("src(%v)---> dst(%v), error", src, dst)
|
||||
}
|
||||
return dst
|
||||
}
|
||||
Reference in New Issue
Block a user