Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions agent/app/model/cronjob.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"time"
)

// BackupAccounts ---> SourceAccountIDs
// BackupAccounts ---> DownloadAccountID
type Cronjob struct {
BaseModel

Expand Down Expand Up @@ -40,10 +38,11 @@ type Cronjob struct {
IgnoreErr bool `json:"ignoreErr"`
RetainCopies uint64 `json:"retainCopies"`

Status string `json:"status"`
EntryIDs string `json:"entryIDs"`
Records []JobRecords `json:"records"`
Secret string `json:"secret"`
IsExecuting bool `json:"isExecuting"`
Status string `json:"status"`
EntryIDs string `json:"entryIDs"`
Records []JobRecords `json:"records"`
Secret string `json:"secret"`
}

type JobRecords struct {
Expand Down
17 changes: 15 additions & 2 deletions agent/app/repo/cronjob.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ type ICronjobRepo interface {
Update(id uint, vars map[string]interface{}) error
Delete(opts ...DBOption) error
DeleteRecord(opts ...DBOption) error
StartRecords(cronjobID uint, targetPath, cronjobType string) model.JobRecords
StartRecords(cronjobID uint) model.JobRecords
UpdateRecords(id uint, vars map[string]interface{}) error
EndRecords(record model.JobRecords, status, message, records string)
AddFailedRecord(cronjobID uint)
PageRecords(page, size int, opts ...DBOption) (int64, []model.JobRecords, error)
}

Expand Down Expand Up @@ -143,7 +144,7 @@ func (c *CronjobRepo) WithByRecordDropID(id int) DBOption {
}
}

func (u *CronjobRepo) StartRecords(cronjobID uint, targetPath, cronjobType string) model.JobRecords {
func (u *CronjobRepo) StartRecords(cronjobID uint) model.JobRecords {
var record model.JobRecords
record.StartTime = time.Now()
record.CronjobID = cronjobID
Expand All @@ -152,6 +153,7 @@ func (u *CronjobRepo) StartRecords(cronjobID uint, targetPath, cronjobType strin
if err := global.DB.Create(&record).Error; err != nil {
global.LOG.Errorf("create record status failed, err: %v", err)
}
_ = u.Update(cronjobID, map[string]interface{}{"is_executing": true})
return record
}
func (u *CronjobRepo) EndRecords(record model.JobRecords, status, message, records string) {
Expand All @@ -165,6 +167,17 @@ func (u *CronjobRepo) EndRecords(record model.JobRecords, status, message, recor
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)
}
_ = u.Update(record.CronjobID, map[string]interface{}{"is_executing": false})
}
func (u *CronjobRepo) AddFailedRecord(cronjobID uint) {
var record model.JobRecords
record.StartTime = time.Now()
record.CronjobID = cronjobID
record.Status = constant.StatusFailed
record.Message = "The current cronjob is being executed"
if err := global.DB.Create(&record).Error; err != nil {
global.LOG.Errorf("create record status failed, err: %v", err)
}
}

func (u *CronjobRepo) Save(id uint, cronjob model.Cronjob) error {
Expand Down
7 changes: 6 additions & 1 deletion agent/app/service/cronjob_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ import (
)

func (u *CronjobService) HandleJob(cronjob *model.Cronjob) {
record := cronjobRepo.StartRecords(cronjob.ID, "", cronjob.Type)
cronjobItem, _ := cronjobRepo.Get(repo.WithByID(cronjob.ID))
if cronjobItem.IsExecuting {
cronjobRepo.AddFailedRecord(cronjob.ID)
return
}
record := cronjobRepo.StartRecords(cronjob.ID)
taskItem, err := task.NewTaskWithOps(fmt.Sprintf("cronjob-%s", cronjob.Name), task.TaskHandle, task.TaskScopeCronjob, record.TaskID, cronjob.ID)
if err != nil {
global.LOG.Errorf("new task for exec shell failed, err: %v", err)
Expand Down
5 changes: 3 additions & 2 deletions agent/init/hook/hook.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package hook

import (
"github.com/1Panel-dev/1Panel/agent/app/service"
"github.com/1Panel-dev/1Panel/agent/utils/alert_push"
"os"
"strings"

"github.com/1Panel-dev/1Panel/agent/app/dto"
"github.com/1Panel-dev/1Panel/agent/app/model"
"github.com/1Panel-dev/1Panel/agent/app/repo"
"github.com/1Panel-dev/1Panel/agent/app/service"
"github.com/1Panel-dev/1Panel/agent/constant"
"github.com/1Panel-dev/1Panel/agent/global"
"github.com/1Panel-dev/1Panel/agent/utils/alert_push"
"github.com/1Panel-dev/1Panel/agent/utils/cmd"
"github.com/1Panel-dev/1Panel/agent/utils/xpack"
)
Expand Down Expand Up @@ -68,6 +68,7 @@ func handleSnapStatus() {

func handleCronjobStatus() {
var jobRecords []model.JobRecords
_ = global.DB.Model(&model.Cronjob{}).Where("is_executing = ?", true).Updates(map[string]interface{}{"is_executing": false}).Error
_ = global.DB.Where("status = ?", constant.StatusWaiting).Find(&jobRecords).Error
for _, record := range jobRecords {
err := global.DB.Model(&model.JobRecords{}).Where("status = ?", constant.StatusWaiting).
Expand Down
2 changes: 1 addition & 1 deletion agent/init/migration/migrations/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
)

var AddTable = &gormigrate.Migration{
ID: "20250805-add-table",
ID: "20250828-add-table",
Migrate: func(tx *gorm.DB) error {
return tx.AutoMigrate(
&model.AppDetail{},
Expand Down
Loading