2022-09-20 11:12:48 +00:00
package v1
import (
2022-09-23 09:21:27 +00:00
"time"
2022-10-17 08:32:31 +00:00
"github.com/1Panel-dev/1Panel/backend/app/api/v1/helper"
"github.com/1Panel-dev/1Panel/backend/app/dto"
"github.com/1Panel-dev/1Panel/backend/constant"
2023-05-04 04:00:37 +00:00
"github.com/1Panel-dev/1Panel/backend/utils/common"
2022-09-20 11:12:48 +00:00
"github.com/gin-gonic/gin"
)
2023-01-04 14:31:51 +00:00
// @Tags Cronjob
// @Summary Create cronjob
// @Description 创建计划任务
// @Accept json
// @Param request body dto.CronjobCreate true "request"
// @Success 200
// @Security ApiKeyAuth
// @Router /cronjobs [post]
2023-10-07 07:46:44 +00:00
// @x-panel-log {"bodyKeys":["type","name"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"创建计划任务 [type][name]","formatEN":"create cronjob [type][name]"}
2022-09-20 11:12:48 +00:00
func ( b * BaseApi ) CreateCronjob ( c * gin . Context ) {
var req dto . CronjobCreate
2023-10-27 06:19:26 +00:00
if err := helper . CheckBindAndValidate ( & req , c ) ; err != nil {
2022-09-20 11:12:48 +00:00
return
}
2023-10-27 06:19:26 +00:00
2022-09-20 11:12:48 +00:00
if err := cronjobService . Create ( req ) ; err != nil {
helper . ErrorWithDetail ( c , constant . CodeErrInternalServer , constant . ErrTypeInternalServer , err )
return
}
helper . SuccessWithData ( c , nil )
}
2023-01-04 14:31:51 +00:00
// @Tags Cronjob
2023-01-05 03:57:03 +00:00
// @Summary Page cronjobs
2023-01-04 14:31:51 +00:00
// @Description 获取计划任务分页
// @Accept json
2024-06-11 10:06:34 +00:00
// @Param request body dto.PageCronjob true "request"
2023-01-04 14:31:51 +00:00
// @Success 200 {object} dto.PageResult
// @Security ApiKeyAuth
// @Router /cronjobs/search [post]
2022-09-20 11:12:48 +00:00
func ( b * BaseApi ) SearchCronjob ( c * gin . Context ) {
2024-06-11 10:06:34 +00:00
var req dto . PageCronjob
2023-10-27 06:19:26 +00:00
if err := helper . CheckBindAndValidate ( & req , c ) ; err != nil {
2022-09-20 11:12:48 +00:00
return
}
total , list , err := cronjobService . SearchWithPage ( req )
if err != nil {
helper . ErrorWithDetail ( c , constant . CodeErrInternalServer , constant . ErrTypeInternalServer , err )
return
}
helper . SuccessWithData ( c , dto . PageResult {
Items : list ,
Total : total ,
} )
}
2023-01-04 14:31:51 +00:00
// @Tags Cronjob
2023-01-05 03:57:03 +00:00
// @Summary Page job records
2023-01-04 14:31:51 +00:00
// @Description 获取计划任务记录
// @Accept json
// @Param request body dto.SearchRecord true "request"
// @Success 200 {object} dto.PageResult
// @Security ApiKeyAuth
// @Router /cronjobs/search/records [post]
2022-09-23 09:21:27 +00:00
func ( b * BaseApi ) SearchJobRecords ( c * gin . Context ) {
var req dto . SearchRecord
2023-10-27 06:19:26 +00:00
if err := helper . CheckBindAndValidate ( & req , c ) ; err != nil {
2022-09-23 09:21:27 +00:00
return
}
2023-10-27 06:19:26 +00:00
2024-08-19 10:30:42 +00:00
loc , _ := time . LoadLocation ( common . LoadTimeZoneByCmd ( ) )
2023-05-04 04:00:37 +00:00
req . StartTime = req . StartTime . In ( loc )
req . EndTime = req . EndTime . In ( loc )
2022-09-23 09:21:27 +00:00
total , list , err := cronjobService . SearchRecords ( req )
if err != nil {
helper . ErrorWithDetail ( c , constant . CodeErrInternalServer , constant . ErrTypeInternalServer , err )
return
}
helper . SuccessWithData ( c , dto . PageResult {
Items : list ,
Total : total ,
} )
}
2023-08-02 08:47:30 +00:00
// @Tags Cronjob
// @Summary Load Cronjob record log
// @Description 获取计划任务记录日志
// @Accept json
// @Param request body dto.OperateByID true "request"
// @Success 200
// @Security ApiKeyAuth
2024-01-25 10:18:26 +00:00
// @Router /cronjobs/records/log [post]
2023-08-02 08:47:30 +00:00
func ( b * BaseApi ) LoadRecordLog ( c * gin . Context ) {
var req dto . OperateByID
2023-10-27 06:19:26 +00:00
if err := helper . CheckBindAndValidate ( & req , c ) ; err != nil {
2023-08-02 08:47:30 +00:00
return
}
2024-03-06 02:36:07 +00:00
content := cronjobService . LoadRecordLog ( req )
2023-08-02 08:47:30 +00:00
helper . SuccessWithData ( c , content )
}
2023-04-07 04:02:10 +00:00
// @Tags Cronjob
// @Summary Clean job records
// @Description 清空计划任务记录
// @Accept json
2023-04-11 10:48:28 +00:00
// @Param request body dto.CronjobClean true "request"
2023-04-07 04:02:10 +00:00
// @Success 200
// @Security ApiKeyAuth
// @Router /cronjobs/records/clean [post]
2023-10-07 07:46:44 +00:00
// @x-panel-log {"bodyKeys":["id"],"paramKeys":[],"BeforeFunctions":[{"input_column":"id","input_value":"id","isList":false,"db":"cronjobs","output_column":"name","output_value":"name"}],"formatZH":"清空计划任务记录 [name]","formatEN":"clean cronjob [name] records"}
2023-04-07 04:02:10 +00:00
func ( b * BaseApi ) CleanRecord ( c * gin . Context ) {
2023-04-11 10:48:28 +00:00
var req dto . CronjobClean
2023-10-27 06:19:26 +00:00
if err := helper . CheckBindAndValidate ( & req , c ) ; err != nil {
2023-04-07 04:02:10 +00:00
return
}
2023-04-11 10:48:28 +00:00
if err := cronjobService . CleanRecord ( req ) ; err != nil {
2023-04-07 04:02:10 +00:00
helper . ErrorWithDetail ( c , constant . CodeErrInternalServer , constant . ErrTypeInternalServer , err )
return
}
helper . SuccessWithData ( c , nil )
}
2023-01-04 14:31:51 +00:00
// @Tags Cronjob
// @Summary Delete cronjob
// @Description 删除计划任务
// @Accept json
2023-04-11 10:48:28 +00:00
// @Param request body dto.CronjobBatchDelete true "request"
2023-01-04 14:31:51 +00:00
// @Success 200
// @Security ApiKeyAuth
2023-01-05 03:57:03 +00:00
// @Router /cronjobs/del [post]
2023-10-07 07:46:44 +00:00
// @x-panel-log {"bodyKeys":["ids"],"paramKeys":[],"BeforeFunctions":[{"input_column":"id","input_value":"ids","isList":true,"db":"cronjobs","output_column":"name","output_value":"names"}],"formatZH":"删除计划任务 [names]","formatEN":"delete cronjob [names]"}
2022-09-20 11:12:48 +00:00
func ( b * BaseApi ) DeleteCronjob ( c * gin . Context ) {
2023-04-11 10:48:28 +00:00
var req dto . CronjobBatchDelete
2023-10-27 06:19:26 +00:00
if err := helper . CheckBindAndValidate ( & req , c ) ; err != nil {
2022-09-20 11:12:48 +00:00
return
}
2023-04-11 10:48:28 +00:00
if err := cronjobService . Delete ( req ) ; err != nil {
2022-09-20 11:12:48 +00:00
helper . ErrorWithDetail ( c , constant . CodeErrInternalServer , constant . ErrTypeInternalServer , err )
return
}
helper . SuccessWithData ( c , nil )
}
2023-01-04 14:31:51 +00:00
// @Tags Cronjob
// @Summary Update cronjob
// @Description 更新计划任务
// @Accept json
// @Param request body dto.CronjobUpdate true "request"
// @Success 200
// @Security ApiKeyAuth
2023-01-05 03:57:03 +00:00
// @Router /cronjobs/update [post]
2023-10-07 07:46:44 +00:00
// @x-panel-log {"bodyKeys":["id"],"paramKeys":[],"BeforeFunctions":[{"input_column":"id","input_value":"id","isList":false,"db":"cronjobs","output_column":"name","output_value":"name"}],"formatZH":"更新计划任务 [name]","formatEN":"update cronjob [name]"}
2022-09-20 11:12:48 +00:00
func ( b * BaseApi ) UpdateCronjob ( c * gin . Context ) {
var req dto . CronjobUpdate
2023-10-27 06:19:26 +00:00
if err := helper . CheckBindAndValidate ( & req , c ) ; err != nil {
2022-09-20 11:12:48 +00:00
return
}
2022-12-13 10:54:28 +00:00
if err := cronjobService . Update ( req . ID , req ) ; err != nil {
2022-09-20 11:12:48 +00:00
helper . ErrorWithDetail ( c , constant . CodeErrInternalServer , constant . ErrTypeInternalServer , err )
return
}
helper . SuccessWithData ( c , nil )
}
2022-09-23 09:21:27 +00:00
2023-01-04 14:31:51 +00:00
// @Tags Cronjob
// @Summary Update cronjob status
// @Description 更新计划任务状态
// @Accept json
// @Param request body dto.CronjobUpdateStatus true "request"
// @Success 200
// @Security ApiKeyAuth
2023-01-05 03:57:03 +00:00
// @Router /cronjobs/status [post]
2023-10-07 07:46:44 +00:00
// @x-panel-log {"bodyKeys":["id","status"],"paramKeys":[],"BeforeFunctions":[{"input_column":"id","input_value":"id","isList":false,"db":"cronjobs","output_column":"name","output_value":"name"}],"formatZH":"修改计划任务 [name] 状态为 [status]","formatEN":"change the status of cronjob [name] to [status]."}
2022-09-27 09:26:01 +00:00
func ( b * BaseApi ) UpdateCronjobStatus ( c * gin . Context ) {
var req dto . CronjobUpdateStatus
2023-10-27 06:19:26 +00:00
if err := helper . CheckBindAndValidate ( & req , c ) ; err != nil {
2022-09-27 09:26:01 +00:00
return
}
if err := cronjobService . UpdateStatus ( req . ID , req . Status ) ; err != nil {
helper . ErrorWithDetail ( c , constant . CodeErrInternalServer , constant . ErrTypeInternalServer , err )
return
}
helper . SuccessWithData ( c , nil )
}
2023-01-04 14:31:51 +00:00
// @Tags Cronjob
2023-01-05 03:57:03 +00:00
// @Summary Download cronjob records
2023-01-04 14:31:51 +00:00
// @Description 下载计划任务记录
// @Accept json
// @Param request body dto.CronjobDownload true "request"
// @Success 200
// @Security ApiKeyAuth
2023-01-05 03:57:03 +00:00
// @Router /cronjobs/download [post]
2023-10-07 07:46:44 +00:00
// @x-panel-log {"bodyKeys":["recordID"],"paramKeys":[],"BeforeFunctions":[{"input_column":"id","input_value":"recordID","isList":false,"db":"job_records","output_column":"file","output_value":"file"}],"formatZH":"下载计划任务记录 [file]","formatEN":"download the cronjob record [file]"}
2022-09-27 16:08:21 +00:00
func ( b * BaseApi ) TargetDownload ( c * gin . Context ) {
var req dto . CronjobDownload
2023-10-27 06:19:26 +00:00
if err := helper . CheckBindAndValidate ( & req , c ) ; err != nil {
2022-09-27 16:08:21 +00:00
return
}
2022-09-28 10:11:36 +00:00
filePath , err := cronjobService . Download ( req )
2022-09-27 16:08:21 +00:00
if err != nil {
helper . ErrorWithDetail ( c , constant . CodeErrInternalServer , constant . ErrTypeInternalServer , err )
return
}
2023-08-02 14:36:37 +00:00
c . File ( filePath )
2022-09-27 16:08:21 +00:00
}
2022-09-29 03:13:05 +00:00
2023-01-04 14:31:51 +00:00
// @Tags Cronjob
// @Summary Handle cronjob once
// @Description 手动执行计划任务
// @Accept json
// @Param request body dto.OperateByID true "request"
// @Success 200
// @Security ApiKeyAuth
2023-01-05 03:57:03 +00:00
// @Router /cronjobs/handle [post]
2023-10-07 07:46:44 +00:00
// @x-panel-log {"bodyKeys":["id"],"paramKeys":[],"BeforeFunctions":[{"input_column":"id","input_value":"id","isList":false,"db":"cronjobs","output_column":"name","output_value":"name"}],"formatZH":"手动执行计划任务 [name]","formatEN":"manually execute the cronjob [name]"}
2022-09-29 03:13:05 +00:00
func ( b * BaseApi ) HandleOnce ( c * gin . Context ) {
2022-12-13 10:54:28 +00:00
var req dto . OperateByID
2023-10-27 06:19:26 +00:00
if err := helper . CheckBindAndValidate ( & req , c ) ; err != nil {
2022-12-13 10:54:28 +00:00
return
}
if err := cronjobService . HandleOnce ( req . ID ) ; err != nil {
2022-09-29 03:13:05 +00:00
helper . ErrorWithDetail ( c , constant . CodeErrInternalServer , constant . ErrTypeInternalServer , err )
return
}
helper . SuccessWithData ( c , nil )
}