Skip to content

Commit

Permalink
feat: 有任务运行则跳过自动更新
Browse files Browse the repository at this point in the history
  • Loading branch information
devhaozi committed Jan 2, 2025
1 parent d6b016d commit a573a6e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
2 changes: 1 addition & 1 deletion cmd/web/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions internal/job/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,18 @@ type Jobs struct {
cert biz.CertRepo
backup biz.BackupRepo
cache biz.CacheRepo
task biz.TaskRepo
}

func NewJobs(db *gorm.DB, log *slog.Logger, setting biz.SettingRepo, cert biz.CertRepo, backup biz.BackupRepo, cache biz.CacheRepo) *Jobs {
func NewJobs(db *gorm.DB, log *slog.Logger, setting biz.SettingRepo, cert biz.CertRepo, backup biz.BackupRepo, cache biz.CacheRepo, task biz.TaskRepo) *Jobs {
return &Jobs{
db: db,
log: log,
setting: setting,
cert: cert,
backup: backup,
cache: cache,
task: task,
}
}

Expand All @@ -40,7 +42,7 @@ func (r *Jobs) Register(c *cron.Cron) error {
return err
}

if _, err := c.AddJob("0 2 * * *", NewPanelTask(r.db, r.log, r.backup, r.cache, r.setting)); err != nil {
if _, err := c.AddJob("0 2 * * *", NewPanelTask(r.db, r.log, r.backup, r.cache, r.task, r.setting)); err != nil {
return err
}

Expand Down
8 changes: 7 additions & 1 deletion internal/job/panel_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,18 @@ type PanelTask struct {
log *slog.Logger
backupRepo biz.BackupRepo
cacheRepo biz.CacheRepo
taskRepo biz.TaskRepo
settingRepo biz.SettingRepo
}

func NewPanelTask(db *gorm.DB, log *slog.Logger, backup biz.BackupRepo, cache biz.CacheRepo, setting biz.SettingRepo) *PanelTask {
func NewPanelTask(db *gorm.DB, log *slog.Logger, backup biz.BackupRepo, cache biz.CacheRepo, task biz.TaskRepo, setting biz.SettingRepo) *PanelTask {
return &PanelTask{
api: api.NewAPI(app.Version),
db: db,
log: log,
backupRepo: backup,
cacheRepo: cache,
taskRepo: task,
settingRepo: setting,
}
}
Expand Down Expand Up @@ -100,6 +102,10 @@ func (r *PanelTask) updateRewrites() {

// 更新面板
func (r *PanelTask) updatePanel() {
if r.taskRepo.HasRunningTask() {
return
}

// 加 300 秒确保在缓存更新后才更新面板
time.AfterFunc(time.Duration(rand.IntN(300))*time.Second+300*time.Second, func() {
panel, err := r.api.LatestVersion()
Expand Down

0 comments on commit a573a6e

Please sign in to comment.