2022-09-20 19:12:48 +08:00
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
2022-09-23 17:21:27 +08:00
|
|
|
"time"
|
|
|
|
|
2022-10-17 16:32:31 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/model"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/constant"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/global"
|
2022-09-23 17:21:27 +08:00
|
|
|
"gorm.io/gorm"
|
2022-09-20 19:12:48 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type CronjobRepo struct{}
|
|
|
|
|
|
|
|
type ICronjobRepo interface {
|
|
|
|
Get(opts ...DBOption) (model.Cronjob, error)
|
2022-09-28 00:08:21 +08:00
|
|
|
GetRecord(opts ...DBOption) (model.JobRecords, error)
|
2022-09-29 16:15:59 +08:00
|
|
|
RecordFirst(id uint) (model.JobRecords, error)
|
2022-09-28 18:11:36 +08:00
|
|
|
ListRecord(opts ...DBOption) ([]model.JobRecords, error)
|
2022-09-27 17:26:01 +08:00
|
|
|
List(opts ...DBOption) ([]model.Cronjob, error)
|
2022-09-20 19:12:48 +08:00
|
|
|
Page(limit, offset int, opts ...DBOption) (int64, []model.Cronjob, error)
|
|
|
|
Create(cronjob *model.Cronjob) error
|
2022-09-23 17:21:27 +08:00
|
|
|
WithByJobID(id int) DBOption
|
2023-04-17 16:06:28 +08:00
|
|
|
WithByBackupID(id uint) DBOption
|
2023-04-07 12:02:10 +08:00
|
|
|
WithByRecordDropID(id int) DBOption
|
2023-09-28 15:40:17 +08:00
|
|
|
WithByRecordFile(file string) DBOption
|
2022-09-23 17:21:27 +08:00
|
|
|
Save(id uint, cronjob model.Cronjob) error
|
2022-09-20 19:12:48 +08:00
|
|
|
Update(id uint, vars map[string]interface{}) error
|
|
|
|
Delete(opts ...DBOption) error
|
2022-09-28 18:11:36 +08:00
|
|
|
DeleteRecord(opts ...DBOption) error
|
2023-03-18 10:03:40 +08:00
|
|
|
StartRecords(cronjobID uint, fromLocal bool, targetPath string) model.JobRecords
|
2022-09-23 17:21:27 +08:00
|
|
|
EndRecords(record model.JobRecords, status, message, records string)
|
2023-03-28 18:00:06 +08:00
|
|
|
PageRecords(page, size int, opts ...DBOption) (int64, []model.JobRecords, error)
|
2022-09-20 19:12:48 +08:00
|
|
|
}
|
|
|
|
|
2022-09-26 18:40:00 +08:00
|
|
|
func NewICronjobRepo() ICronjobRepo {
|
2022-09-20 19:12:48 +08:00
|
|
|
return &CronjobRepo{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *CronjobRepo) Get(opts ...DBOption) (model.Cronjob, error) {
|
|
|
|
var cronjob model.Cronjob
|
|
|
|
db := global.DB
|
|
|
|
for _, opt := range opts {
|
|
|
|
db = opt(db)
|
|
|
|
}
|
|
|
|
err := db.First(&cronjob).Error
|
|
|
|
return cronjob, err
|
|
|
|
}
|
|
|
|
|
2022-09-28 00:08:21 +08:00
|
|
|
func (u *CronjobRepo) GetRecord(opts ...DBOption) (model.JobRecords, error) {
|
|
|
|
var record model.JobRecords
|
|
|
|
db := global.DB
|
|
|
|
for _, opt := range opts {
|
|
|
|
db = opt(db)
|
|
|
|
}
|
|
|
|
err := db.First(&record).Error
|
|
|
|
return record, err
|
|
|
|
}
|
|
|
|
|
2022-09-27 17:26:01 +08:00
|
|
|
func (u *CronjobRepo) List(opts ...DBOption) ([]model.Cronjob, error) {
|
|
|
|
var cronjobs []model.Cronjob
|
|
|
|
db := global.DB.Model(&model.Cronjob{})
|
|
|
|
for _, opt := range opts {
|
|
|
|
db = opt(db)
|
|
|
|
}
|
2022-09-28 18:11:36 +08:00
|
|
|
err := db.Find(&cronjobs).Error
|
|
|
|
return cronjobs, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *CronjobRepo) ListRecord(opts ...DBOption) ([]model.JobRecords, error) {
|
|
|
|
var cronjobs []model.JobRecords
|
|
|
|
db := global.DB.Model(&model.JobRecords{})
|
|
|
|
for _, opt := range opts {
|
|
|
|
db = opt(db)
|
|
|
|
}
|
2022-09-27 17:26:01 +08:00
|
|
|
err := db.Find(&cronjobs).Error
|
|
|
|
return cronjobs, err
|
|
|
|
}
|
|
|
|
|
2022-09-20 19:12:48 +08:00
|
|
|
func (u *CronjobRepo) Page(page, size int, opts ...DBOption) (int64, []model.Cronjob, error) {
|
2022-09-27 17:26:01 +08:00
|
|
|
var cronjobs []model.Cronjob
|
2022-09-20 19:12:48 +08:00
|
|
|
db := global.DB.Model(&model.Cronjob{})
|
|
|
|
for _, opt := range opts {
|
|
|
|
db = opt(db)
|
|
|
|
}
|
|
|
|
count := int64(0)
|
|
|
|
db = db.Count(&count)
|
2023-06-29 18:40:12 +08:00
|
|
|
err := db.Limit(size).Offset(size * (page - 1)).Find(&cronjobs).Error
|
2022-09-27 17:26:01 +08:00
|
|
|
return count, cronjobs, err
|
2022-09-20 19:12:48 +08:00
|
|
|
}
|
|
|
|
|
2022-09-29 16:15:59 +08:00
|
|
|
func (u *CronjobRepo) RecordFirst(id uint) (model.JobRecords, error) {
|
|
|
|
var record model.JobRecords
|
2023-03-10 00:27:41 +08:00
|
|
|
err := global.DB.Where("cronjob_id = ?", id).Order("created_at desc").First(&record).Error
|
2022-09-29 16:15:59 +08:00
|
|
|
return record, err
|
|
|
|
}
|
|
|
|
|
2022-09-23 17:21:27 +08:00
|
|
|
func (u *CronjobRepo) PageRecords(page, size int, opts ...DBOption) (int64, []model.JobRecords, error) {
|
2022-09-27 17:26:01 +08:00
|
|
|
var cronjobs []model.JobRecords
|
2022-09-23 17:21:27 +08:00
|
|
|
db := global.DB.Model(&model.JobRecords{})
|
|
|
|
for _, opt := range opts {
|
|
|
|
db = opt(db)
|
|
|
|
}
|
|
|
|
count := int64(0)
|
|
|
|
db = db.Count(&count)
|
2022-09-28 18:11:36 +08:00
|
|
|
err := db.Order("created_at desc").Limit(size).Offset(size * (page - 1)).Find(&cronjobs).Error
|
2022-09-27 17:26:01 +08:00
|
|
|
return count, cronjobs, err
|
2022-09-23 17:21:27 +08:00
|
|
|
}
|
|
|
|
|
2022-09-20 19:12:48 +08:00
|
|
|
func (u *CronjobRepo) Create(cronjob *model.Cronjob) error {
|
|
|
|
return global.DB.Create(cronjob).Error
|
|
|
|
}
|
|
|
|
|
2022-09-23 17:21:27 +08:00
|
|
|
func (c *CronjobRepo) WithByJobID(id int) DBOption {
|
|
|
|
return func(g *gorm.DB) *gorm.DB {
|
|
|
|
return g.Where("cronjob_id = ?", id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-17 16:06:28 +08:00
|
|
|
func (c *CronjobRepo) WithByBackupID(id uint) DBOption {
|
|
|
|
return func(g *gorm.DB) *gorm.DB {
|
|
|
|
return g.Where("target_dir_id = ?", id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-28 15:40:17 +08:00
|
|
|
func (c *CronjobRepo) WithByRecordFile(file string) DBOption {
|
|
|
|
return func(g *gorm.DB) *gorm.DB {
|
|
|
|
return g.Where("records = ?", file)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-07 12:02:10 +08:00
|
|
|
func (c *CronjobRepo) WithByRecordDropID(id int) DBOption {
|
|
|
|
return func(g *gorm.DB) *gorm.DB {
|
|
|
|
return g.Where("id < ?", id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-18 10:03:40 +08:00
|
|
|
func (u *CronjobRepo) StartRecords(cronjobID uint, fromLocal bool, targetPath string) model.JobRecords {
|
2022-09-23 17:21:27 +08:00
|
|
|
var record model.JobRecords
|
|
|
|
record.StartTime = time.Now()
|
|
|
|
record.CronjobID = cronjobID
|
2023-03-18 10:03:40 +08:00
|
|
|
record.FromLocal = fromLocal
|
2023-02-24 18:50:35 +08:00
|
|
|
record.Status = constant.StatusWaiting
|
2022-09-23 17:21:27 +08:00
|
|
|
if err := global.DB.Create(&record).Error; err != nil {
|
|
|
|
global.LOG.Errorf("create record status failed, err: %v", err)
|
|
|
|
}
|
|
|
|
return record
|
|
|
|
}
|
|
|
|
func (u *CronjobRepo) EndRecords(record model.JobRecords, status, message, records string) {
|
|
|
|
errMap := make(map[string]interface{})
|
|
|
|
errMap["records"] = records
|
|
|
|
errMap["status"] = status
|
2023-02-24 12:26:23 +08:00
|
|
|
errMap["file"] = record.File
|
2022-09-23 17:21:27 +08:00
|
|
|
errMap["message"] = message
|
|
|
|
errMap["interval"] = time.Since(record.StartTime).Milliseconds()
|
|
|
|
if err := global.DB.Model(&model.JobRecords{}).Where("id = ?", record.ID).Updates(errMap).Error; err != nil {
|
|
|
|
global.LOG.Errorf("update record status failed, err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *CronjobRepo) Save(id uint, cronjob model.Cronjob) error {
|
|
|
|
return global.DB.Model(&model.Cronjob{}).Where("id = ?", id).Save(&cronjob).Error
|
|
|
|
}
|
2022-09-20 19:12:48 +08:00
|
|
|
func (u *CronjobRepo) Update(id uint, vars map[string]interface{}) error {
|
|
|
|
return global.DB.Model(&model.Cronjob{}).Where("id = ?", id).Updates(vars).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *CronjobRepo) Delete(opts ...DBOption) error {
|
|
|
|
db := global.DB
|
|
|
|
for _, opt := range opts {
|
|
|
|
db = opt(db)
|
|
|
|
}
|
|
|
|
return db.Delete(&model.Cronjob{}).Error
|
|
|
|
}
|
2022-09-28 18:11:36 +08:00
|
|
|
func (u *CronjobRepo) DeleteRecord(opts ...DBOption) error {
|
|
|
|
db := global.DB
|
|
|
|
for _, opt := range opts {
|
|
|
|
db = opt(db)
|
|
|
|
}
|
|
|
|
return db.Delete(&model.JobRecords{}).Error
|
2022-09-27 17:26:01 +08:00
|
|
|
}
|