update cronjob
This commit is contained in:
+56
-58
@@ -2,14 +2,14 @@ package v1
|
||||
|
||||
import (
|
||||
"cutego/core/api/v1/request"
|
||||
"cutego/core/api/v1/response"
|
||||
"cutego/core/entity"
|
||||
"cutego/core/service"
|
||||
"cutego/pkg/cache"
|
||||
"cutego/pkg/page"
|
||||
"cutego/pkg/common"
|
||||
"cutego/pkg/cronjob"
|
||||
"cutego/pkg/resp"
|
||||
"cutego/pkg/util"
|
||||
"github.com/gin-gonic/gin"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type CronJobApi struct {
|
||||
@@ -24,27 +24,30 @@ func (a CronJobApi) List(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
find, i := a.cronJobService.FindPage(query)
|
||||
resp.OK(c, page.Page{
|
||||
List: find,
|
||||
Total: i,
|
||||
Size: query.PageSize,
|
||||
})
|
||||
|
||||
var result []interface{}
|
||||
for _, item := range find {
|
||||
// 领域对象转换
|
||||
result = append(result, util.DeepCopy(item, &response.CronJobResponse{}))
|
||||
}
|
||||
resp.OK(c, util.NewPage(result, i, query.PageSize))
|
||||
}
|
||||
|
||||
// GetById 根据id获取任务详情
|
||||
func (a CronJobApi) GetById(c *gin.Context) {
|
||||
jobId := c.Param("jobId")
|
||||
info := a.cronJobService.GetInfo(common.StringToInt64(jobId))
|
||||
resp.OK(c, util.DeepCopy(info, &response.CronJobResponse{}))
|
||||
}
|
||||
|
||||
// Edit 修改定时任务
|
||||
func (a CronJobApi) Edit(c *gin.Context) {
|
||||
dictType := entity.SysDictType{}
|
||||
if c.Bind(&dictType) != nil {
|
||||
record := entity.SysCronJob{}
|
||||
if c.Bind(&record) != nil {
|
||||
resp.ParamError(c)
|
||||
return
|
||||
}
|
||||
//检验定时任务是否存在
|
||||
if a.dictTypeService.CheckDictTypeUnique(dictType) {
|
||||
resp.Error(c, "修改字典'"+dictType.DictName+"'失败, 定时任务已存在")
|
||||
return
|
||||
}
|
||||
//修改数据
|
||||
if a.dictTypeService.Edit(dictType) {
|
||||
if a.cronJobService.Edit(record) > 0 {
|
||||
resp.OK(c)
|
||||
} else {
|
||||
resp.Error(c)
|
||||
@@ -53,54 +56,49 @@ func (a CronJobApi) Edit(c *gin.Context) {
|
||||
|
||||
// Add 新增定时任务
|
||||
func (a CronJobApi) Add(c *gin.Context) {
|
||||
dictType := entity.SysDictType{}
|
||||
if c.Bind(&dictType) != nil {
|
||||
dto := entity.SysCronJob{}
|
||||
if c.Bind(&dto) != nil {
|
||||
resp.ParamError(c)
|
||||
return
|
||||
}
|
||||
//检验定时任务是否存在
|
||||
if a.dictTypeService.CheckDictTypeUnique(dictType) {
|
||||
resp.Error(c, "新增字典'"+dictType.DictName+"'失败, 定时任务已存在")
|
||||
record := a.cronJobService.GetInfoByAlias(dto.FuncAlias)
|
||||
if record.JobId > 0 {
|
||||
resp.Error(c, "任务已存在!")
|
||||
} else {
|
||||
a.cronJobService.Save(dto)
|
||||
cronjob.AppendCronFunc(dto.JobCron, dto.FuncAlias, dto.Status)
|
||||
resp.OK(c)
|
||||
}
|
||||
}
|
||||
|
||||
// Remove 删除定时任务
|
||||
func (a CronJobApi) Remove(c *gin.Context) {
|
||||
jobId := common.StringToInt64(c.Param("jobId"))
|
||||
funcAlias := c.Param("funcAlias")
|
||||
|
||||
if a.cronJobService.GetInfo(jobId).Status == "1" {
|
||||
resp.Error(c, "任务运行中, 无法删除!")
|
||||
return
|
||||
}
|
||||
//新增定时任务
|
||||
if a.dictTypeService.Save(dictType) {
|
||||
resp.OK(c)
|
||||
} else {
|
||||
resp.Error(c)
|
||||
}
|
||||
|
||||
cronjob.RemoveCronFunc(funcAlias)
|
||||
a.cronJobService.Remove([]int64{jobId})
|
||||
resp.OK(c)
|
||||
}
|
||||
|
||||
// Remove 批量删除定时任务
|
||||
func (a CronJobApi) Remove(c *gin.Context) {
|
||||
param := c.Param("dictId")
|
||||
split := strings.Split(param, ",")
|
||||
ids := make([]int64, 0)
|
||||
types := make([]string, 0)
|
||||
for _, s := range split {
|
||||
parseInt, _ := strconv.ParseInt(s, 10, 64)
|
||||
ids = append(ids, parseInt)
|
||||
// ChangeStatus 改变任务状态
|
||||
func (a CronJobApi) ChangeStatus(c *gin.Context) {
|
||||
record := entity.SysCronJob{}
|
||||
if c.Bind(&record) != nil {
|
||||
resp.ParamError(c)
|
||||
return
|
||||
}
|
||||
//校验定时任务是否使用
|
||||
for _, id := range ids {
|
||||
dictType := a.dictTypeService.GetById(id)
|
||||
if len(a.dictDataService.FindByDictType(dictType.DictType)) > 0 {
|
||||
resp.Error(c, dictType.DictName+"已分配,不能删除")
|
||||
return
|
||||
if a.cronJobService.Edit(record) > 0 {
|
||||
if record.Status == "1" {
|
||||
cronjob.StartCronFunc(record.FuncAlias)
|
||||
} else {
|
||||
cronjob.StopCronFunc(record.FuncAlias)
|
||||
}
|
||||
types = append(types, dictType.DictType)
|
||||
}
|
||||
//批量删除
|
||||
if a.dictTypeService.Remove(ids) {
|
||||
//从缓存中删除数据
|
||||
cache.RemoveList(types)
|
||||
resp.OK(c)
|
||||
} else {
|
||||
resp.Error(c)
|
||||
}
|
||||
}
|
||||
|
||||
// 改变任务状态
|
||||
func (a CronJobApi) ChangeStatus(context *gin.Context) {
|
||||
|
||||
resp.OK(c)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user