2022-09-20 11:12:48 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2022-09-23 09:21:27 +00:00
|
|
|
"bufio"
|
2022-09-26 10:40:00 +00:00
|
|
|
"encoding/json"
|
2022-09-20 11:12:48 +00:00
|
|
|
"fmt"
|
2022-09-23 09:21:27 +00:00
|
|
|
"os"
|
2022-09-26 10:40:00 +00:00
|
|
|
"strings"
|
2022-09-23 09:21:27 +00:00
|
|
|
"time"
|
2022-09-20 11:12:48 +00:00
|
|
|
|
2022-10-17 08:32:31 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/dto"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/model"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/constant"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/global"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/cloud_storage"
|
2022-09-20 11:12:48 +00:00
|
|
|
"github.com/jinzhu/copier"
|
|
|
|
"github.com/pkg/errors"
|
2022-09-27 09:26:01 +00:00
|
|
|
"github.com/robfig/cron/v3"
|
2022-09-20 11:12:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type CronjobService struct{}
|
|
|
|
|
|
|
|
type ICronjobService interface {
|
|
|
|
SearchWithPage(search dto.SearchWithPage) (int64, interface{}, error)
|
2022-09-23 09:21:27 +00:00
|
|
|
SearchRecords(search dto.SearchRecord) (int64, interface{}, error)
|
2022-09-20 11:12:48 +00:00
|
|
|
Create(cronjobDto dto.CronjobCreate) error
|
2022-09-29 03:13:05 +00:00
|
|
|
HandleOnce(id uint) error
|
2022-09-29 08:15:59 +00:00
|
|
|
Update(id uint, req dto.CronjobUpdate) error
|
2022-09-27 09:26:01 +00:00
|
|
|
UpdateStatus(id uint, status string) error
|
2022-09-20 11:12:48 +00:00
|
|
|
Delete(ids []uint) error
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewICronjobService() ICronjobService {
|
|
|
|
return &CronjobService{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *CronjobService) SearchWithPage(search dto.SearchWithPage) (int64, interface{}, error) {
|
|
|
|
total, cronjobs, err := cronjobRepo.Page(search.Page, search.PageSize, commonRepo.WithLikeName(search.Info))
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
if item.Type == "website" || item.Type == "database" || item.Type == "directory" {
|
|
|
|
backup, _ := backupRepo.Get(commonRepo.WithByID(uint(item.TargetDirID)))
|
|
|
|
if len(backup.Type) != 0 {
|
|
|
|
item.TargetDir = backup.Type
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
item.TargetDir = "-"
|
|
|
|
}
|
2022-09-29 08:15:59 +00:00
|
|
|
record, _ := cronjobRepo.RecordFirst(cronjob.ID)
|
|
|
|
if record.ID != 0 {
|
|
|
|
item.LastRecrodTime = record.StartTime.Format("2006-01-02 15:04:05")
|
|
|
|
} else {
|
|
|
|
item.LastRecrodTime = "-"
|
|
|
|
}
|
2022-09-20 11:12:48 +00:00
|
|
|
dtoCronjobs = append(dtoCronjobs, item)
|
|
|
|
}
|
|
|
|
return total, dtoCronjobs, err
|
|
|
|
}
|
|
|
|
|
2022-09-23 09:21:27 +00: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 14:55:10 +00:00
|
|
|
commonRepo.WithByDate(search.StartTime, search.EndTime))
|
2022-09-23 09:21:27 +00: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
|
|
|
|
}
|
|
|
|
|
2022-09-27 16:08:21 +00:00
|
|
|
func (u *CronjobService) Download(down dto.CronjobDownload) (string, error) {
|
|
|
|
record, _ := cronjobRepo.GetRecord(commonRepo.WithByID(down.RecordID))
|
2022-09-28 10:11:36 +00:00
|
|
|
if record.ID == 0 {
|
|
|
|
return "", constant.ErrRecordNotFound
|
2022-09-27 16:08:21 +00:00
|
|
|
}
|
|
|
|
cronjob, _ := cronjobRepo.Get(commonRepo.WithByID(record.CronjobID))
|
2022-09-28 10:11:36 +00:00
|
|
|
if cronjob.ID == 0 {
|
|
|
|
return "", constant.ErrRecordNotFound
|
2022-09-27 16:08:21 +00:00
|
|
|
}
|
|
|
|
backup, _ := backupRepo.Get(commonRepo.WithByID(down.BackupAccountID))
|
2022-09-28 10:11:36 +00:00
|
|
|
if cronjob.ID == 0 {
|
|
|
|
return "", constant.ErrRecordNotFound
|
2022-09-27 16:08:21 +00:00
|
|
|
}
|
2022-12-28 10:35:53 +00:00
|
|
|
|
|
|
|
global.LOG.Infof("start to download records %s from %s", cronjob.Type, backup.Type)
|
2022-09-27 16:08:21 +00:00
|
|
|
varMap := make(map[string]interface{})
|
|
|
|
if err := json.Unmarshal([]byte(backup.Vars), &varMap); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
varMap["type"] = backup.Type
|
|
|
|
if backup.Type != "LOCAL" {
|
|
|
|
varMap["bucket"] = backup.Bucket
|
|
|
|
switch backup.Type {
|
|
|
|
case constant.Sftp:
|
2022-12-28 10:35:53 +00:00
|
|
|
varMap["username"] = backup.AccessKey
|
2022-09-27 16:08:21 +00:00
|
|
|
varMap["password"] = backup.Credential
|
|
|
|
case constant.OSS, constant.S3, constant.MinIo:
|
2022-12-28 10:35:53 +00:00
|
|
|
varMap["accessKey"] = backup.AccessKey
|
2022-09-27 16:08:21 +00:00
|
|
|
varMap["secretKey"] = backup.Credential
|
|
|
|
}
|
|
|
|
backClient, err := cloud_storage.NewCloudStorageClient(varMap)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("new cloud storage client failed, err: %v", err)
|
|
|
|
}
|
2022-12-28 10:35:53 +00:00
|
|
|
global.LOG.Info("new backup client successful")
|
2022-09-28 10:11:36 +00:00
|
|
|
commonDir := fmt.Sprintf("%s/%s/", cronjob.Type, cronjob.Name)
|
|
|
|
name := fmt.Sprintf("%s%s.tar.gz", commonDir, record.StartTime.Format("20060102150405"))
|
2022-09-27 16:08:21 +00:00
|
|
|
if cronjob.Type == "database" {
|
2022-09-28 10:11:36 +00:00
|
|
|
name = fmt.Sprintf("%s%s.gz", commonDir, record.StartTime.Format("20060102150405"))
|
2022-09-27 16:08:21 +00:00
|
|
|
}
|
2023-01-29 08:38:34 +00:00
|
|
|
tempPath := fmt.Sprintf("%s/download/%s", constant.DataDir, commonDir)
|
2022-09-28 10:11:36 +00:00
|
|
|
if _, err := os.Stat(tempPath); err != nil && os.IsNotExist(err) {
|
|
|
|
if err = os.MkdirAll(tempPath, os.ModePerm); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
}
|
2022-12-28 10:35:53 +00:00
|
|
|
|
|
|
|
global.LOG.Infof("download records %s from %s to %s", name, commonDir, tempPath)
|
2022-09-28 10:11:36 +00:00
|
|
|
targetPath := tempPath + strings.ReplaceAll(name, commonDir, "")
|
|
|
|
if _, err = os.Stat(targetPath); err != nil && os.IsNotExist(err) {
|
|
|
|
isOK, err := backClient.Download(name, targetPath)
|
|
|
|
if !isOK {
|
|
|
|
return "", fmt.Errorf("cloud storage download failed, err: %v", err)
|
|
|
|
}
|
2022-09-27 16:08:21 +00:00
|
|
|
}
|
2022-09-28 10:11:36 +00:00
|
|
|
return targetPath, nil
|
2022-09-27 16:08:21 +00:00
|
|
|
}
|
|
|
|
if _, ok := varMap["dir"]; !ok {
|
|
|
|
return "", errors.New("load local backup dir failed")
|
|
|
|
}
|
2022-12-28 10:35:53 +00:00
|
|
|
global.LOG.Infof("record is save in local dir %s", varMap["dir"])
|
2022-11-30 07:47:11 +00:00
|
|
|
|
|
|
|
switch cronjob.Type {
|
|
|
|
case "website":
|
|
|
|
return fmt.Sprintf("%v/website/%s/website_%s_%s.tar.gz", varMap["dir"], cronjob.Website, cronjob.Website, record.StartTime.Format("20060102150405")), nil
|
|
|
|
case "database":
|
2022-12-21 04:21:27 +00:00
|
|
|
mysqlInfo, err := appInstallRepo.LoadBaseInfo("mysql", "")
|
2022-11-30 07:47:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("load mysqlInfo failed, err: %v", err)
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%v/database/mysql/%s/%s/db_%s_%s.sql.gz", varMap["dir"], mysqlInfo.Name, cronjob.DBName, cronjob.DBName, record.StartTime.Format("20060102150405")), nil
|
|
|
|
case "directory":
|
|
|
|
return fmt.Sprintf("%v/%s/%s/%s.tar.gz", varMap["dir"], cronjob.Type, cronjob.Name, record.StartTime.Format("20060102150405")), nil
|
|
|
|
default:
|
|
|
|
return "", fmt.Errorf("not support type %s", cronjob.Type)
|
2022-09-28 10:11:36 +00:00
|
|
|
}
|
2022-09-27 16:08:21 +00:00
|
|
|
}
|
|
|
|
|
2022-09-29 03:13:05 +00:00
|
|
|
func (u *CronjobService) HandleOnce(id uint) error {
|
|
|
|
cronjob, _ := cronjobRepo.Get(commonRepo.WithByID(id))
|
|
|
|
if cronjob.ID == 0 {
|
|
|
|
return constant.ErrRecordNotFound
|
|
|
|
}
|
|
|
|
u.HandleJob(&cronjob)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-20 11:12:48 +00: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 09:21:27 +00:00
|
|
|
cronjob.Status = constant.StatusEnable
|
|
|
|
cronjob.Spec = loadSpec(cronjob)
|
|
|
|
|
2022-09-20 11:12:48 +00:00
|
|
|
if err := cronjobRepo.Create(&cronjob); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-12-28 10:35:53 +00:00
|
|
|
global.LOG.Infof("create cronjob %s successful, spec: %s", cronjob.Name, cronjob.Spec)
|
2022-09-27 09:26:01 +00:00
|
|
|
if err := u.StartJob(&cronjob); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *CronjobService) StartJob(cronjob *model.Cronjob) error {
|
|
|
|
global.Cron.Remove(cron.EntryID(cronjob.EntryID))
|
2022-12-28 10:35:53 +00:00
|
|
|
global.LOG.Infof("stop cronjob entryID: %d", cronjob.EntryID)
|
2022-09-29 03:13:05 +00:00
|
|
|
entryID, err := u.AddCronJob(cronjob)
|
2022-09-23 10:08:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_ = cronjobRepo.Update(cronjob.ID, map[string]interface{}{"entry_id": entryID})
|
2022-09-20 11:12:48 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *CronjobService) Delete(ids []uint) error {
|
|
|
|
if len(ids) == 1 {
|
2022-09-29 08:15:59 +00:00
|
|
|
if err := u.HandleDelete(ids[0]); err != nil {
|
|
|
|
return err
|
2022-09-27 09:26:01 +00:00
|
|
|
}
|
2022-09-20 11:12:48 +00:00
|
|
|
return cronjobRepo.Delete(commonRepo.WithByID(ids[0]))
|
|
|
|
}
|
2022-09-27 09:26:01 +00:00
|
|
|
cronjobs, err := cronjobRepo.List(commonRepo.WithIdsIn(ids))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for i := range cronjobs {
|
2022-09-29 08:15:59 +00:00
|
|
|
_ = u.HandleDelete(ids[i])
|
2022-09-27 09:26:01 +00:00
|
|
|
}
|
2022-09-20 11:12:48 +00:00
|
|
|
return cronjobRepo.Delete(commonRepo.WithIdsIn(ids))
|
|
|
|
}
|
|
|
|
|
2022-09-29 08:15:59 +00:00
|
|
|
func (u *CronjobService) Update(id uint, req dto.CronjobUpdate) error {
|
2022-09-23 09:21:27 +00:00
|
|
|
var cronjob model.Cronjob
|
|
|
|
if err := copier.Copy(&cronjob, &req); err != nil {
|
|
|
|
return errors.WithMessage(constant.ErrStructTransform, err.Error())
|
|
|
|
}
|
2022-09-29 08:15:59 +00:00
|
|
|
cronjob.Spec = loadSpec(cronjob)
|
2022-09-27 09:26:01 +00:00
|
|
|
if err := u.StartJob(&cronjob); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-09-29 08:15:59 +00:00
|
|
|
|
|
|
|
upMap := make(map[string]interface{})
|
|
|
|
upMap["name"] = req.Name
|
|
|
|
upMap["script"] = req.Script
|
|
|
|
upMap["spec_type"] = req.SpecType
|
|
|
|
upMap["week"] = req.Week
|
|
|
|
upMap["day"] = req.Day
|
|
|
|
upMap["hour"] = req.Hour
|
|
|
|
upMap["minute"] = req.Minute
|
|
|
|
upMap["website"] = req.Website
|
|
|
|
upMap["exclusion_rules"] = req.ExclusionRules
|
2022-10-28 10:46:14 +00:00
|
|
|
upMap["db_name"] = req.DBName
|
2022-09-29 08:15:59 +00:00
|
|
|
upMap["url"] = req.URL
|
|
|
|
upMap["source_dir"] = req.SourceDir
|
2022-10-28 10:46:14 +00:00
|
|
|
upMap["keep_local"] = req.KeepLocal
|
2022-09-29 08:15:59 +00:00
|
|
|
upMap["target_dir_id"] = req.TargetDirID
|
2022-10-28 10:46:14 +00:00
|
|
|
upMap["retain_copies"] = req.RetainCopies
|
2022-09-29 08:15:59 +00:00
|
|
|
return cronjobRepo.Update(id, upMap)
|
2022-09-23 09:21:27 +00:00
|
|
|
}
|
|
|
|
|
2022-09-27 09:26:01 +00: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")
|
|
|
|
}
|
|
|
|
if status == constant.StatusEnable {
|
|
|
|
if err := u.StartJob(&cronjob); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
global.Cron.Remove(cron.EntryID(cronjob.EntryID))
|
2022-12-28 10:35:53 +00:00
|
|
|
global.LOG.Infof("stop cronjob entryID: %d", cronjob.EntryID)
|
2022-09-27 09:26:01 +00:00
|
|
|
}
|
|
|
|
return cronjobRepo.Update(cronjob.ID, map[string]interface{}{"status": status})
|
|
|
|
}
|
|
|
|
|
2022-09-29 03:13:05 +00:00
|
|
|
func (u *CronjobService) AddCronJob(cronjob *model.Cronjob) (int, error) {
|
2022-09-23 09:21:27 +00:00
|
|
|
addFunc := func() {
|
2022-09-29 03:13:05 +00:00
|
|
|
u.HandleJob(cronjob)
|
2022-09-23 09:21:27 +00:00
|
|
|
}
|
2022-09-27 09:26:01 +00:00
|
|
|
global.LOG.Infof("add %s job %s successful", cronjob.Type, cronjob.Name)
|
2022-09-23 09:21:27 +00:00
|
|
|
entryID, err := global.Cron.AddFunc(cronjob.Spec, addFunc)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2022-12-28 10:35:53 +00:00
|
|
|
global.LOG.Infof("start cronjob entryID: %d", entryID)
|
2022-09-23 09:21:27 +00:00
|
|
|
return int(entryID), nil
|
|
|
|
}
|
|
|
|
|
2022-09-26 10:40:00 +00:00
|
|
|
func mkdirAndWriteFile(cronjob *model.Cronjob, startTime time.Time, msg []byte) (string, error) {
|
2023-01-29 08:38:34 +00:00
|
|
|
dir := fmt.Sprintf("%s/task/%s/%s", constant.DataDir, cronjob.Type, cronjob.Name)
|
2022-09-23 09:21:27 +00: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 10:08:14 +00:00
|
|
|
path := fmt.Sprintf("%s/%s.log", dir, startTime.Format("20060102150405"))
|
2022-12-28 10:35:53 +00:00
|
|
|
global.LOG.Infof("cronjob %s has generated some logs %s", cronjob.Name, path)
|
2022-09-23 09:21:27 +00: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
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadSpec(cronjob model.Cronjob) string {
|
|
|
|
switch cronjob.SpecType {
|
|
|
|
case "perMonth":
|
|
|
|
return fmt.Sprintf("%v %v %v * *", cronjob.Minute, cronjob.Hour, cronjob.Day)
|
|
|
|
case "perWeek":
|
|
|
|
return fmt.Sprintf("%v %v * * %v", cronjob.Minute, cronjob.Hour, cronjob.Week)
|
|
|
|
case "perNDay":
|
|
|
|
return fmt.Sprintf("%v %v */%v * *", cronjob.Minute, cronjob.Hour, cronjob.Day)
|
2022-12-14 11:34:42 +00:00
|
|
|
case "perDay":
|
|
|
|
return fmt.Sprintf("%v %v * * *", cronjob.Minute, cronjob.Hour)
|
2022-09-23 09:21:27 +00:00
|
|
|
case "perNHour":
|
|
|
|
return fmt.Sprintf("%v */%v * * *", cronjob.Minute, cronjob.Hour)
|
|
|
|
case "perHour":
|
|
|
|
return fmt.Sprintf("%v * * * *", cronjob.Minute)
|
|
|
|
case "perNMinute":
|
|
|
|
return fmt.Sprintf("@every %vm", cronjob.Minute)
|
|
|
|
default:
|
|
|
|
return ""
|
|
|
|
}
|
2022-09-20 11:12:48 +00:00
|
|
|
}
|