2022-09-20 19:12:48 +08:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2022-09-23 17:21:27 +08:00
|
|
|
"bufio"
|
2022-09-20 19:12:48 +08:00
|
|
|
"fmt"
|
2022-09-23 17:21:27 +08:00
|
|
|
"os"
|
2023-04-11 22:26:28 +08:00
|
|
|
"path"
|
2024-01-22 22:29:19 +08:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2022-09-23 17:21:27 +08:00
|
|
|
"time"
|
2022-09-20 19:12:48 +08:00
|
|
|
|
2022-10-17 16:32:31 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/dto"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/model"
|
2023-08-02 16:47:30 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/buserr"
|
2022-10-17 16:32:31 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/constant"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/global"
|
2022-09-20 19:12:48 +08:00
|
|
|
"github.com/jinzhu/copier"
|
|
|
|
"github.com/pkg/errors"
|
2022-09-27 17:26:01 +08:00
|
|
|
"github.com/robfig/cron/v3"
|
2022-09-20 19:12:48 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type CronjobService struct{}
|
|
|
|
|
|
|
|
type ICronjobService interface {
|
|
|
|
SearchWithPage(search dto.SearchWithPage) (int64, interface{}, error)
|
2022-09-23 17:21:27 +08:00
|
|
|
SearchRecords(search dto.SearchRecord) (int64, interface{}, error)
|
2022-09-20 19:12:48 +08:00
|
|
|
Create(cronjobDto dto.CronjobCreate) error
|
2022-09-29 11:13:05 +08:00
|
|
|
HandleOnce(id uint) error
|
2022-09-29 16:15:59 +08:00
|
|
|
Update(id uint, req dto.CronjobUpdate) error
|
2022-09-27 17:26:01 +08:00
|
|
|
UpdateStatus(id uint, status string) error
|
2023-04-11 18:48:28 +08:00
|
|
|
Delete(req dto.CronjobBatchDelete) error
|
2023-03-28 18:00:06 +08:00
|
|
|
Download(down dto.CronjobDownload) (string, error)
|
2024-01-22 22:29:19 +08:00
|
|
|
StartJob(cronjob *model.Cronjob) (string, error)
|
2023-04-11 18:48:28 +08:00
|
|
|
CleanRecord(req dto.CronjobClean) error
|
2023-08-02 16:47:30 +08:00
|
|
|
|
|
|
|
LoadRecordLog(req dto.OperateByID) (string, error)
|
2022-09-20 19:12:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewICronjobService() ICronjobService {
|
|
|
|
return &CronjobService{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *CronjobService) SearchWithPage(search dto.SearchWithPage) (int64, interface{}, error) {
|
2023-06-29 18:40:12 +08:00
|
|
|
total, cronjobs, err := cronjobRepo.Page(search.Page, search.PageSize, commonRepo.WithLikeName(search.Info), commonRepo.WithOrderRuleBy(search.OrderBy, search.Order))
|
2022-09-20 19:12:48 +08:00
|
|
|
var dtoCronjobs []dto.CronjobInfo
|
|
|
|
for _, cronjob := range cronjobs {
|
|
|
|
var item dto.CronjobInfo
|
|
|
|
if err := copier.Copy(&item, &cronjob); err != nil {
|
|
|
|
return 0, nil, errors.WithMessage(constant.ErrStructTransform, err.Error())
|
|
|
|
}
|
2022-09-29 16:15:59 +08:00
|
|
|
record, _ := cronjobRepo.RecordFirst(cronjob.ID)
|
|
|
|
if record.ID != 0 {
|
2023-05-30 15:30:57 +08:00
|
|
|
item.LastRecordTime = record.StartTime.Format("2006-01-02 15:04:05")
|
2022-09-29 16:15:59 +08:00
|
|
|
} else {
|
2023-05-30 15:30:57 +08:00
|
|
|
item.LastRecordTime = "-"
|
2022-09-29 16:15:59 +08:00
|
|
|
}
|
2022-09-20 19:12:48 +08:00
|
|
|
dtoCronjobs = append(dtoCronjobs, item)
|
|
|
|
}
|
|
|
|
return total, dtoCronjobs, err
|
|
|
|
}
|
|
|
|
|
2022-09-23 17:21:27 +08:00
|
|
|
func (u *CronjobService) SearchRecords(search dto.SearchRecord) (int64, interface{}, error) {
|
|
|
|
total, records, err := cronjobRepo.PageRecords(
|
|
|
|
search.Page,
|
|
|
|
search.PageSize,
|
|
|
|
commonRepo.WithByStatus(search.Status),
|
|
|
|
cronjobRepo.WithByJobID(search.CronjobID),
|
2023-01-09 22:55:10 +08:00
|
|
|
commonRepo.WithByDate(search.StartTime, search.EndTime))
|
2022-09-23 17:21:27 +08:00
|
|
|
var dtoCronjobs []dto.Record
|
|
|
|
for _, record := range records {
|
|
|
|
var item dto.Record
|
|
|
|
if err := copier.Copy(&item, &record); err != nil {
|
|
|
|
return 0, nil, errors.WithMessage(constant.ErrStructTransform, err.Error())
|
|
|
|
}
|
|
|
|
dtoCronjobs = append(dtoCronjobs, item)
|
|
|
|
}
|
|
|
|
return total, dtoCronjobs, err
|
|
|
|
}
|
|
|
|
|
2023-08-02 16:47:30 +08:00
|
|
|
func (u *CronjobService) LoadRecordLog(req dto.OperateByID) (string, error) {
|
|
|
|
record, err := cronjobRepo.GetRecord(commonRepo.WithByID(req.ID))
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if _, err := os.Stat(record.Records); err != nil {
|
|
|
|
return "", buserr.New("ErrHttpReqNotFound")
|
|
|
|
}
|
|
|
|
content, err := os.ReadFile(record.Records)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return string(content), nil
|
|
|
|
}
|
|
|
|
|
2023-04-11 18:48:28 +08:00
|
|
|
func (u *CronjobService) CleanRecord(req dto.CronjobClean) error {
|
2023-04-12 16:26:30 +08:00
|
|
|
cronjob, err := cronjobRepo.Get(commonRepo.WithByID(req.CronjobID))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-01-24 17:41:56 +08:00
|
|
|
if req.CleanData {
|
|
|
|
if hasBackup(cronjob.Type) {
|
2024-01-25 11:20:42 +08:00
|
|
|
accountMap, err := loadClientMap(cronjob.BackupAccounts)
|
2023-04-11 18:48:28 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-01-24 17:41:56 +08:00
|
|
|
cronjob.RetainCopies = 0
|
|
|
|
u.removeExpiredBackup(cronjob, accountMap, model.BackupRecord{})
|
2023-04-11 18:48:28 +08:00
|
|
|
} else {
|
2024-01-24 17:41:56 +08:00
|
|
|
u.removeExpiredLog(cronjob)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
records, _ := backupRepo.ListRecord(backupRepo.WithByCronID(cronjob.ID))
|
|
|
|
for _, records := range records {
|
|
|
|
records.CronjobID = 0
|
|
|
|
_ = backupRepo.UpdateRecord(&records)
|
2023-04-11 18:48:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
delRecords, err := cronjobRepo.ListRecord(cronjobRepo.WithByJobID(int(req.CronjobID)))
|
2023-04-07 12:02:10 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, del := range delRecords {
|
|
|
|
_ = os.RemoveAll(del.Records)
|
|
|
|
}
|
2023-04-11 18:48:28 +08:00
|
|
|
if err := cronjobRepo.DeleteRecord(cronjobRepo.WithByJobID(int(req.CronjobID))); err != nil {
|
2023-04-07 12:02:10 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-28 00:08:21 +08:00
|
|
|
func (u *CronjobService) Download(down dto.CronjobDownload) (string, error) {
|
|
|
|
record, _ := cronjobRepo.GetRecord(commonRepo.WithByID(down.RecordID))
|
2022-09-28 18:11:36 +08:00
|
|
|
if record.ID == 0 {
|
|
|
|
return "", constant.ErrRecordNotFound
|
2022-09-28 00:08:21 +08:00
|
|
|
}
|
|
|
|
backup, _ := backupRepo.Get(commonRepo.WithByID(down.BackupAccountID))
|
2023-08-02 22:36:37 +08:00
|
|
|
if backup.ID == 0 {
|
2022-09-28 18:11:36 +08:00
|
|
|
return "", constant.ErrRecordNotFound
|
2022-09-28 00:08:21 +08:00
|
|
|
}
|
2023-04-11 22:26:28 +08:00
|
|
|
if backup.Type == "LOCAL" || record.FromLocal {
|
2023-03-18 10:03:40 +08:00
|
|
|
if _, err := os.Stat(record.File); err != nil && os.IsNotExist(err) {
|
2023-06-27 18:57:59 +08:00
|
|
|
return "", err
|
2022-09-28 18:11:36 +08:00
|
|
|
}
|
2023-03-18 10:03:40 +08:00
|
|
|
return record.File, nil
|
|
|
|
}
|
|
|
|
tempPath := fmt.Sprintf("%s/download/%s", constant.DataDir, record.File)
|
2023-08-02 22:36:37 +08:00
|
|
|
if _, err := os.Stat(tempPath); err != nil && os.IsNotExist(err) {
|
|
|
|
client, err := NewIBackupService().NewClient(&backup)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
_ = os.MkdirAll(path.Dir(tempPath), os.ModePerm)
|
|
|
|
isOK, err := client.Download(record.File, tempPath)
|
|
|
|
if !isOK || err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2022-09-28 18:11:36 +08:00
|
|
|
}
|
2023-03-18 10:03:40 +08:00
|
|
|
return tempPath, nil
|
2022-09-28 00:08:21 +08:00
|
|
|
}
|
|
|
|
|
2022-09-29 11:13:05 +08:00
|
|
|
func (u *CronjobService) HandleOnce(id uint) error {
|
|
|
|
cronjob, _ := cronjobRepo.Get(commonRepo.WithByID(id))
|
|
|
|
if cronjob.ID == 0 {
|
|
|
|
return constant.ErrRecordNotFound
|
|
|
|
}
|
2023-03-01 18:17:31 +08:00
|
|
|
u.HandleJob(&cronjob)
|
2022-09-29 11:13:05 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-20 19:12:48 +08:00
|
|
|
func (u *CronjobService) Create(cronjobDto dto.CronjobCreate) error {
|
|
|
|
cronjob, _ := cronjobRepo.Get(commonRepo.WithByName(cronjobDto.Name))
|
|
|
|
if cronjob.ID != 0 {
|
|
|
|
return constant.ErrRecordExist
|
|
|
|
}
|
|
|
|
if err := copier.Copy(&cronjob, &cronjobDto); err != nil {
|
|
|
|
return errors.WithMessage(constant.ErrStructTransform, err.Error())
|
|
|
|
}
|
2022-09-23 17:21:27 +08:00
|
|
|
cronjob.Status = constant.StatusEnable
|
|
|
|
|
2022-12-28 18:35:53 +08:00
|
|
|
global.LOG.Infof("create cronjob %s successful, spec: %s", cronjob.Name, cronjob.Spec)
|
2024-01-22 22:29:19 +08:00
|
|
|
spec := cronjob.Spec
|
|
|
|
entryIDs, err := u.StartJob(&cronjob)
|
2023-03-14 10:19:29 +08:00
|
|
|
if err != nil {
|
2022-09-27 17:26:01 +08:00
|
|
|
return err
|
|
|
|
}
|
2024-01-22 22:29:19 +08:00
|
|
|
cronjob.Spec = spec
|
|
|
|
cronjob.EntryIDs = entryIDs
|
2023-03-10 10:54:47 +08:00
|
|
|
if err := cronjobRepo.Create(&cronjob); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-09-27 17:26:01 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-01-22 22:29:19 +08:00
|
|
|
func (u *CronjobService) StartJob(cronjob *model.Cronjob) (string, error) {
|
|
|
|
if len(cronjob.EntryIDs) != 0 {
|
|
|
|
ids := strings.Split(cronjob.EntryIDs, ",")
|
|
|
|
for _, id := range ids {
|
|
|
|
idItem, _ := strconv.Atoi(id)
|
|
|
|
global.Cron.Remove(cron.EntryID(idItem))
|
|
|
|
}
|
2023-03-14 10:19:29 +08:00
|
|
|
}
|
2024-01-22 22:29:19 +08:00
|
|
|
specs := strings.Split(cronjob.Spec, ",")
|
|
|
|
var ids []string
|
|
|
|
for _, spec := range specs {
|
|
|
|
cronjob.Spec = spec
|
|
|
|
entryID, err := u.AddCronJob(cronjob)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
ids = append(ids, fmt.Sprintf("%v", entryID))
|
2022-09-23 18:08:14 +08:00
|
|
|
}
|
2024-01-22 22:29:19 +08:00
|
|
|
return strings.Join(ids, ","), nil
|
2022-09-20 19:12:48 +08:00
|
|
|
}
|
|
|
|
|
2023-04-11 18:48:28 +08:00
|
|
|
func (u *CronjobService) Delete(req dto.CronjobBatchDelete) error {
|
|
|
|
for _, id := range req.IDs {
|
|
|
|
cronjob, _ := cronjobRepo.Get(commonRepo.WithByID(id))
|
|
|
|
if cronjob.ID == 0 {
|
|
|
|
return errors.New("find cronjob in db failed")
|
|
|
|
}
|
2024-01-22 22:29:19 +08:00
|
|
|
ids := strings.Split(cronjob.EntryIDs, ",")
|
|
|
|
for _, id := range ids {
|
|
|
|
idItem, _ := strconv.Atoi(id)
|
|
|
|
global.Cron.Remove(cron.EntryID(idItem))
|
|
|
|
}
|
|
|
|
global.LOG.Infof("stop cronjob entryID: %s", cronjob.EntryIDs)
|
2023-04-11 18:48:28 +08:00
|
|
|
if err := u.CleanRecord(dto.CronjobClean{CronjobID: id, CleanData: req.CleanData}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := cronjobRepo.Delete(commonRepo.WithByID(id)); err != nil {
|
2022-09-29 16:15:59 +08:00
|
|
|
return err
|
2022-09-27 17:26:01 +08:00
|
|
|
}
|
|
|
|
}
|
2023-04-11 18:48:28 +08:00
|
|
|
|
|
|
|
return nil
|
2022-09-20 19:12:48 +08:00
|
|
|
}
|
|
|
|
|
2022-09-29 16:15:59 +08:00
|
|
|
func (u *CronjobService) Update(id uint, req dto.CronjobUpdate) error {
|
2022-09-23 17:21:27 +08:00
|
|
|
var cronjob model.Cronjob
|
|
|
|
if err := copier.Copy(&cronjob, &req); err != nil {
|
|
|
|
return errors.WithMessage(constant.ErrStructTransform, err.Error())
|
|
|
|
}
|
2023-03-10 00:27:41 +08:00
|
|
|
cronModel, err := cronjobRepo.Get(commonRepo.WithByID(id))
|
|
|
|
if err != nil {
|
|
|
|
return constant.ErrRecordNotFound
|
|
|
|
}
|
2023-06-01 15:05:14 +08:00
|
|
|
upMap := make(map[string]interface{})
|
2024-01-22 22:29:19 +08:00
|
|
|
cronjob.EntryIDs = cronModel.EntryIDs
|
2023-03-14 10:19:29 +08:00
|
|
|
cronjob.Type = cronModel.Type
|
2024-01-22 22:29:19 +08:00
|
|
|
spec := cronjob.Spec
|
2023-06-01 15:05:14 +08:00
|
|
|
if cronModel.Status == constant.StatusEnable {
|
2024-01-22 22:29:19 +08:00
|
|
|
newEntryIDs, err := u.StartJob(&cronjob)
|
2023-06-01 15:05:14 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-01-22 22:29:19 +08:00
|
|
|
upMap["entry_ids"] = newEntryIDs
|
2023-06-01 15:05:14 +08:00
|
|
|
} else {
|
2024-01-22 22:29:19 +08:00
|
|
|
ids := strings.Split(cronjob.EntryIDs, ",")
|
|
|
|
for _, id := range ids {
|
|
|
|
idItem, _ := strconv.Atoi(id)
|
|
|
|
global.Cron.Remove(cron.EntryID(idItem))
|
|
|
|
}
|
2022-09-27 17:26:01 +08:00
|
|
|
}
|
2022-09-29 16:15:59 +08:00
|
|
|
|
|
|
|
upMap["name"] = req.Name
|
2024-01-22 22:29:19 +08:00
|
|
|
upMap["spec"] = spec
|
2022-09-29 16:15:59 +08:00
|
|
|
upMap["script"] = req.Script
|
2023-06-28 14:30:11 +08:00
|
|
|
upMap["container_name"] = req.ContainerName
|
2023-09-13 17:32:12 +08:00
|
|
|
upMap["app_id"] = req.AppID
|
2022-09-29 16:15:59 +08:00
|
|
|
upMap["website"] = req.Website
|
|
|
|
upMap["exclusion_rules"] = req.ExclusionRules
|
2024-01-07 22:52:41 +08:00
|
|
|
upMap["db_type"] = req.DBType
|
2022-10-28 18:46:14 +08:00
|
|
|
upMap["db_name"] = req.DBName
|
2022-09-29 16:15:59 +08:00
|
|
|
upMap["url"] = req.URL
|
|
|
|
upMap["source_dir"] = req.SourceDir
|
2024-01-25 11:20:42 +08:00
|
|
|
|
|
|
|
upMap["backup_accounts"] = req.BackupAccounts
|
|
|
|
upMap["default_download"] = req.DefaultDownload
|
2022-10-28 18:46:14 +08:00
|
|
|
upMap["retain_copies"] = req.RetainCopies
|
2022-09-29 16:15:59 +08:00
|
|
|
return cronjobRepo.Update(id, upMap)
|
2022-09-23 17:21:27 +08:00
|
|
|
}
|
|
|
|
|
2022-09-27 17:26:01 +08:00
|
|
|
func (u *CronjobService) UpdateStatus(id uint, status string) error {
|
|
|
|
cronjob, _ := cronjobRepo.Get(commonRepo.WithByID(id))
|
|
|
|
if cronjob.ID == 0 {
|
|
|
|
return errors.WithMessage(constant.ErrRecordNotFound, "record not found")
|
|
|
|
}
|
2023-03-14 10:19:29 +08:00
|
|
|
var (
|
2024-01-22 22:29:19 +08:00
|
|
|
entryIDs string
|
|
|
|
err error
|
2023-03-14 10:19:29 +08:00
|
|
|
)
|
2022-09-27 17:26:01 +08:00
|
|
|
if status == constant.StatusEnable {
|
2024-01-22 22:29:19 +08:00
|
|
|
entryIDs, err = u.StartJob(&cronjob)
|
2023-03-14 10:19:29 +08:00
|
|
|
if err != nil {
|
2022-09-27 17:26:01 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
2024-01-22 22:29:19 +08:00
|
|
|
ids := strings.Split(cronjob.EntryIDs, ",")
|
|
|
|
for _, id := range ids {
|
|
|
|
idItem, _ := strconv.Atoi(id)
|
|
|
|
global.Cron.Remove(cron.EntryID(idItem))
|
|
|
|
}
|
|
|
|
global.LOG.Infof("stop cronjob entryID: %s", cronjob.EntryIDs)
|
2022-09-27 17:26:01 +08:00
|
|
|
}
|
2024-01-22 22:29:19 +08:00
|
|
|
return cronjobRepo.Update(cronjob.ID, map[string]interface{}{"status": status, "entry_ids": entryIDs})
|
2022-09-27 17:26:01 +08:00
|
|
|
}
|
|
|
|
|
2022-09-29 11:13:05 +08:00
|
|
|
func (u *CronjobService) AddCronJob(cronjob *model.Cronjob) (int, error) {
|
2022-09-23 17:21:27 +08:00
|
|
|
addFunc := func() {
|
2022-09-29 11:13:05 +08:00
|
|
|
u.HandleJob(cronjob)
|
2022-09-23 17:21:27 +08:00
|
|
|
}
|
2022-09-27 17:26:01 +08:00
|
|
|
global.LOG.Infof("add %s job %s successful", cronjob.Type, cronjob.Name)
|
2022-09-23 17:21:27 +08:00
|
|
|
entryID, err := global.Cron.AddFunc(cronjob.Spec, addFunc)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2022-12-28 18:35:53 +08:00
|
|
|
global.LOG.Infof("start cronjob entryID: %d", entryID)
|
2022-09-23 17:21:27 +08:00
|
|
|
return int(entryID), nil
|
|
|
|
}
|
|
|
|
|
2022-09-26 18:40:00 +08:00
|
|
|
func mkdirAndWriteFile(cronjob *model.Cronjob, startTime time.Time, msg []byte) (string, error) {
|
2023-01-29 16:38:34 +08:00
|
|
|
dir := fmt.Sprintf("%s/task/%s/%s", constant.DataDir, cronjob.Type, cronjob.Name)
|
2022-09-23 17:21:27 +08:00
|
|
|
if _, err := os.Stat(dir); err != nil && os.IsNotExist(err) {
|
|
|
|
if err = os.MkdirAll(dir, os.ModePerm); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-23 18:08:14 +08:00
|
|
|
path := fmt.Sprintf("%s/%s.log", dir, startTime.Format("20060102150405"))
|
2022-12-28 18:35:53 +08:00
|
|
|
global.LOG.Infof("cronjob %s has generated some logs %s", cronjob.Name, path)
|
2022-09-23 17:21:27 +08:00
|
|
|
file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, 0666)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
write := bufio.NewWriter(file)
|
|
|
|
_, _ = write.WriteString(string(msg))
|
|
|
|
write.Flush()
|
|
|
|
return path, nil
|
|
|
|
}
|