diff --git a/core/dao/cron_job_dao.go b/core/dao/cron_job_dao.go index 2fc3bae..774a4fa 100644 --- a/core/dao/cron_job_dao.go +++ b/core/dao/cron_job_dao.go @@ -75,7 +75,7 @@ func (d CronJobDao) Update(config entity.SysCronJob) int64 { return update } -// Remove 删除数据 +// Delete 删除数据 func (d CronJobDao) Delete(list []int64) bool { session := SqlDB.NewSession() session.Begin() @@ -89,7 +89,7 @@ func (d CronJobDao) Delete(list []int64) bool { return true } -// 通过方法别名获取任务详情 +// SelectByFuncAlias 通过方法别名获取任务详情 func (d CronJobDao) SelectByFuncAlias(funcAlias string) *entity.SysCronJob { config := entity.SysCronJob{} session := d.sql(SqlDB.NewSession()) @@ -100,3 +100,15 @@ func (d CronJobDao) SelectByFuncAlias(funcAlias string) *entity.SysCronJob { } return &config } + +// SelectAll 查找所有启用状态的任务 +func (d CronJobDao) SelectAll() ([]entity.SysCronJob, int) { + configs := make([]entity.SysCronJob, 0) + session := d.sql(SqlDB.NewSession()) + err := session.Where("status = ?", 1).Find(&configs) + if err != nil { + common.ErrorLog(err) + return nil, 0 + } + return configs, len(configs) +} diff --git a/core/job/index.go b/core/job/index.go index 33f526e..7070291 100644 --- a/core/job/index.go +++ b/core/job/index.go @@ -1,21 +1,9 @@ package job -import ( - "cutego/core/service" - "cutego/pkg/common" -) - -// 定时任务: 别名与方法的映射 +// AliasFuncMap 定时任务: 别名与方法的映射 var AliasFuncMap = make(map[string]func()) -// 注册任务 -func RegisterFunc(aliasName string, f func()) { - currentJob := service.CronJobService{}.GetInfoByAlias(aliasName) - AliasFuncMap[aliasName] = f - common.InfoLogf("注册定时任务 --- %s ---> Success", currentJob.JobName) -} - -// 注册方法 +// 任务注册 func init() { - RegisterFunc("test1", TestJob) + AliasFuncMap["test1"] = TestJob1 } diff --git a/core/job/test_job.go b/core/job/test_job.go index ac297c0..94a7791 100644 --- a/core/job/test_job.go +++ b/core/job/test_job.go @@ -2,6 +2,6 @@ package job import "fmt" -func TestJob() { - fmt.Println("哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈") +func TestJob1() { + fmt.Println("无参数任务执行中...") } diff --git a/core/service/cron_job_service.go b/core/service/cron_job_service.go index 6876982..0d12766 100644 --- a/core/service/cron_job_service.go +++ b/core/service/cron_job_service.go @@ -25,7 +25,7 @@ func (s CronJobService) GetInfo(id int64) *entity.SysCronJob { return s.cronJobDao.SelectById(id) } -// GetInfo 查询数据 +// GetInfoByAlias 查询数据 func (s CronJobService) GetInfoByAlias(funcAlias string) *entity.SysCronJob { return s.cronJobDao.SelectByFuncAlias(funcAlias) } @@ -39,3 +39,8 @@ func (s CronJobService) Edit(config entity.SysCronJob) int64 { func (s CronJobService) Remove(list []int64) bool { return s.cronJobDao.Delete(list) } + +// FindAll 查找所有 +func (s CronJobService) FindAll() ([]entity.SysCronJob, int) { + return s.cronJobDao.SelectAll() +} diff --git a/main.go b/main.go index 091a52c..dc5679c 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main // init函数执行顺序自上而下, 最后执行main包里面的init函数 import ( _ "cutego/core/dao" + _ "cutego/core/job" "cutego/core/router" "cutego/pkg/common" "cutego/pkg/config" @@ -14,29 +15,8 @@ import ( ) func main() { - StartTest() - StartApp() -} -func StartTest() { - fmt.Println("================ Test Content =================") + //go testChangeJob() - //cronjob.PrintCronNext() - //cronjob.ExecWithCronNext() - - fmt.Println("================ Test Content =================") -} - -func StartApp() { - //switch config.AppEnvConfig.Server.RunMode { - //case gin.DebugMode: - // gin.SetMode(gin.DebugMode) - // break - //case gin.ReleaseMode: - // gin.SetMode(gin.ReleaseMode) - // break - //default: - // gin.SetMode(gin.DebugMode) - //} gin.SetMode(util.IF(config.AppEnvConfig.Server.RunMode == "", "debug", config.AppEnvConfig.Server.RunMode).(string)) r := router.Init() r.Use(logger.LoggerToFile()) @@ -45,3 +25,9 @@ func StartApp() { common.FatalfLog("Start server: %+v", err) } } + +//func testChangeJob() { +// time.Sleep(time.Millisecond * 5000) +// fmt.Println("改变任务调度间隔") +// cronjob.AppendCronFunc("*/5 * * * *", "test1", "1") +//} diff --git a/pkg/cronjob/index.go b/pkg/cronjob/index.go index 984897f..dd00a0a 100644 --- a/pkg/cronjob/index.go +++ b/pkg/cronjob/index.go @@ -1,7 +1,6 @@ package cronjob import ( - "cutego/core/api/v1/request" "cutego/core/job" "cutego/core/service" "cutego/pkg/common" @@ -19,59 +18,59 @@ import ( // 在26分、29分、33分执行一次:0 26,29,33 * * * ? // 每天的0点、13点、18点、21点都执行一次:0 0 0,13,18,21 * * ? -// 定时任务: 别名与调度器的映射 -var AliasCronMap = make(map[string]*cron.Cron) +// aliasCronMap 定时任务: 别名与调度器的映射 +var aliasCronMap = make(map[string]*cron.Cron) // StopCronFunc 停止任务, 不会停止已开始的任务 func StopCronFunc(aliasName string) { common.InfoLogf("停止任务 %s ---> Start", aliasName) - AliasCronMap[aliasName].Stop() + go aliasCronMap[aliasName].Stop() common.InfoLogf("停止任务 %s ---> Finish", aliasName) } // StartCronFunc 开始任务 func StartCronFunc(aliasName string) { common.InfoLogf("唤起任务 %s ---> Start", aliasName) - AliasCronMap[aliasName].Start() + go aliasCronMap[aliasName].Start() common.InfoLogf("唤起任务 %s ---> Finish", aliasName) } // RemoveCronFunc 移除任务 func RemoveCronFunc(aliasName string) { common.InfoLogf("移除任务 %s ---> Start", aliasName) - StopCronFunc(aliasName) - delete(AliasCronMap, aliasName) + go StopCronFunc(aliasName) + delete(aliasCronMap, aliasName) common.InfoLogf("移除任务 %s ---> Finish", aliasName) } // AppendCronFunc 新增任务 func AppendCronFunc(jobCron string, aliasName string, status string) { + if aliasCronMap[aliasName] != nil { + aliasCronMap[aliasName].Stop() + aliasCronMap[aliasName] = nil + } common.InfoLogf("新增任务 %s ---> Start", aliasName) c := cron.New() - c.AddFunc(jobCron, job.AliasFuncMap[aliasName]) - if status == "1" { - c.Start() - common.InfoLogf("调度定时任务 --- %s ---> Success", aliasName) + err := c.AddFunc(jobCron, job.AliasFuncMap[aliasName]) + if err != nil { + panic("任务追加失败, " + err.Error()) + } + if status == "1" { + go func() { + c.Start() + aliasCronMap[aliasName] = c + common.InfoLogf("调度定时任务 --- %s ---> Success", aliasName) + }() } - AliasCronMap[aliasName] = c common.InfoLogf("新增任务 %s ---> Finish", aliasName) } func init() { - if len(job.AliasFuncMap) > 0 { - //go test() - index := 1 - for true { - q := request.CronJobQuery{} - q.PageNum = index - data, _ := service.CronJobService{}.FindPage(q) - if len(data) == 0 { - break - } - for _, datum := range data { - AppendCronFunc(datum.JobCron, datum.FuncAlias, datum.Status) - } - index += 1 + 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) } } }