2022-03-01 13:50:13 +08:00
|
|
|
|
package cronjob
|
|
|
|
|
|
|
|
|
|
import (
|
2023-01-18 15:40:27 +08:00
|
|
|
|
"cutego/modules/core/job"
|
|
|
|
|
"cutego/modules/core/service"
|
2023-01-18 17:46:25 +08:00
|
|
|
|
"cutego/pkg/logging"
|
2022-03-01 13:50:13 +08:00
|
|
|
|
"github.com/robfig/cron"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Cron表达式参考
|
|
|
|
|
// 每隔5秒执行一次:*/5 * * * * ?
|
|
|
|
|
// 每隔1分钟执行一次:0 */1 * * * ?
|
|
|
|
|
// 每天23点执行一次:0 0 23 * * ?
|
|
|
|
|
// 每天凌晨1点执行一次:0 0 1 * * ?
|
|
|
|
|
// 每月1号凌晨1点执行一次:0 0 1 1 * ?
|
|
|
|
|
// 每月最后一天23点执行一次:0 0 23 L * ?
|
|
|
|
|
// 每周星期天凌晨1点实行一次:0 0 1 ? * L
|
|
|
|
|
// 在26分、29分、33分执行一次:0 26,29,33 * * * ?
|
|
|
|
|
// 每天的0点、13点、18点、21点都执行一次:0 0 0,13,18,21 * * ?
|
|
|
|
|
|
2023-01-18 14:53:01 +08:00
|
|
|
|
// aliasCronMap 定时任务: 别名与调度器的映射
|
|
|
|
|
var aliasCronMap = make(map[string]*cron.Cron)
|
2022-03-01 13:50:13 +08:00
|
|
|
|
|
2022-03-01 17:43:41 +08:00
|
|
|
|
// StopCronFunc 停止任务, 不会停止已开始的任务
|
2022-03-01 13:50:13 +08:00
|
|
|
|
func StopCronFunc(aliasName string) {
|
2023-01-18 17:46:25 +08:00
|
|
|
|
logging.InfoLogf("停止任务 %s ---> Start", aliasName)
|
2023-01-18 14:53:01 +08:00
|
|
|
|
go aliasCronMap[aliasName].Stop()
|
2023-01-18 17:46:25 +08:00
|
|
|
|
logging.InfoLogf("停止任务 %s ---> Finish", aliasName)
|
2022-03-01 13:50:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-01 17:43:41 +08:00
|
|
|
|
// StartCronFunc 开始任务
|
2022-03-01 13:50:13 +08:00
|
|
|
|
func StartCronFunc(aliasName string) {
|
2023-01-18 17:46:25 +08:00
|
|
|
|
logging.InfoLogf("唤起任务 %s ---> Start", aliasName)
|
2023-01-18 14:53:01 +08:00
|
|
|
|
go aliasCronMap[aliasName].Start()
|
2023-01-18 17:46:25 +08:00
|
|
|
|
logging.InfoLogf("唤起任务 %s ---> Finish", aliasName)
|
2022-03-01 13:50:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-01 17:43:41 +08:00
|
|
|
|
// RemoveCronFunc 移除任务
|
|
|
|
|
func RemoveCronFunc(aliasName string) {
|
2023-01-18 17:46:25 +08:00
|
|
|
|
logging.InfoLogf("移除任务 %s ---> Start", aliasName)
|
2023-01-18 14:53:01 +08:00
|
|
|
|
go StopCronFunc(aliasName)
|
|
|
|
|
delete(aliasCronMap, aliasName)
|
2023-01-18 17:46:25 +08:00
|
|
|
|
logging.InfoLogf("移除任务 %s ---> Finish", aliasName)
|
2022-03-01 17:43:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// AppendCronFunc 新增任务
|
|
|
|
|
func AppendCronFunc(jobCron string, aliasName string, status string) {
|
2023-01-18 14:53:01 +08:00
|
|
|
|
if aliasCronMap[aliasName] != nil {
|
|
|
|
|
aliasCronMap[aliasName].Stop()
|
|
|
|
|
aliasCronMap[aliasName] = nil
|
|
|
|
|
}
|
2023-01-18 17:46:25 +08:00
|
|
|
|
logging.InfoLogf("新增任务 %s ---> Start", aliasName)
|
2022-03-01 17:43:41 +08:00
|
|
|
|
c := cron.New()
|
2023-01-18 14:53:01 +08:00
|
|
|
|
err := c.AddFunc(jobCron, job.AliasFuncMap[aliasName])
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic("任务追加失败, " + err.Error())
|
|
|
|
|
}
|
2022-03-01 17:43:41 +08:00
|
|
|
|
if status == "1" {
|
2023-01-18 14:53:01 +08:00
|
|
|
|
go func() {
|
|
|
|
|
c.Start()
|
|
|
|
|
aliasCronMap[aliasName] = c
|
2023-01-18 17:46:25 +08:00
|
|
|
|
logging.InfoLogf("调度定时任务 --- %s ---> Success", aliasName)
|
2023-01-18 14:53:01 +08:00
|
|
|
|
}()
|
2022-03-01 17:43:41 +08:00
|
|
|
|
}
|
2023-01-18 17:46:25 +08:00
|
|
|
|
logging.InfoLogf("新增任务 %s ---> Finish", aliasName)
|
2022-03-01 17:43:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-01 13:50:13 +08:00
|
|
|
|
func init() {
|
2023-01-18 14:53:01 +08:00
|
|
|
|
jobService := service.CronJobService{}
|
|
|
|
|
jobs, total := jobService.FindAll()
|
|
|
|
|
if len(job.AliasFuncMap) > 0 && total > 0 {
|
|
|
|
|
for _, datum := range jobs {
|
|
|
|
|
AppendCronFunc(datum.JobCron, datum.FuncAlias, datum.Status)
|
2022-03-01 13:50:13 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|