From 251f91a08e990a2b6680617301b9416c3ee872e3 Mon Sep 17 00:00:00 2001 From: DaneEveritt Date: Sun, 24 Jul 2022 15:59:17 -0400 Subject: [PATCH] Fix crons to actually run correctly using the configuration values --- config/config.go | 4 ++-- internal/cron/activity_cron.go | 4 ++-- internal/cron/cron.go | 13 ++++++++++--- internal/cron/sftp_cron.go | 6 +++--- 4 files changed, 17 insertions(+), 10 deletions(-) diff --git a/config/config.go b/config/config.go index 5ab0f36..4efb7b9 100644 --- a/config/config.go +++ b/config/config.go @@ -167,10 +167,10 @@ type SystemConfiguration struct { // being sent to the Panel. By default this will send activity collected over the last minute. Keep // in mind that only a fixed number of activity log entries, defined by ActivitySendCount, will be sent // in each run. - ActivitySendInterval int64 `default:"60" yaml:"activity_send_interval"` + ActivitySendInterval int `default:"60" yaml:"activity_send_interval"` // ActivitySendCount is the number of activity events to send per batch. - ActivitySendCount int64 `default:"100" yaml:"activity_send_count"` + ActivitySendCount int `default:"100" yaml:"activity_send_count"` // If set to true, file permissions for a server will be checked when the process is // booted. This can cause boot delays if the server has a large amount of files. In most diff --git a/internal/cron/activity_cron.go b/internal/cron/activity_cron.go index c0eb2ef..ce258e2 100644 --- a/internal/cron/activity_cron.go +++ b/internal/cron/activity_cron.go @@ -12,7 +12,7 @@ import ( type activityCron struct { mu *system.AtomicBool manager *server.Manager - max int64 + max int } // Run executes the cronjob and ensures we fetch and send all of the stored activity to the @@ -30,7 +30,7 @@ func (ac *activityCron) Run(ctx context.Context) error { var activity []models.Activity tx := database.Instance().WithContext(ctx). Where("event NOT LIKE ?", "server:sftp.%"). - Limit(int(ac.max)). + Limit(ac.max). Find(&activity) if tx.Error != nil { diff --git a/internal/cron/cron.go b/internal/cron/cron.go index a754e0b..6092582 100644 --- a/internal/cron/cron.go +++ b/internal/cron/cron.go @@ -3,7 +3,7 @@ package cron import ( "context" "emperror.dev/errors" - "github.com/apex/log" + log2 "github.com/apex/log" "github.com/go-co-op/gocron" "github.com/pterodactyl/wings/config" "github.com/pterodactyl/wings/server" @@ -40,7 +40,13 @@ func Scheduler(ctx context.Context, m *server.Manager) (*gocron.Scheduler, error } s := gocron.NewScheduler(l) - _, _ = s.Tag("activity").Every(config.Get().System.ActivitySendInterval).Seconds().Do(func() { + log := log2.WithField("subsystem", "cron") + + interval := time.Duration(config.Get().System.ActivitySendInterval) * time.Second + log.WithField("interval", interval).Info("configuring system crons") + + _, _ = s.Tag("activity").Every(interval).Do(func() { + log.WithField("cron", "activity").Debug("sending internal activity events to Panel") if err := activity.Run(ctx); err != nil { if errors.Is(err, ErrCronRunning) { log.WithField("cron", "activity").Warn("activity process is already running, skipping...") @@ -50,7 +56,8 @@ func Scheduler(ctx context.Context, m *server.Manager) (*gocron.Scheduler, error } }) - _, _ = s.Tag("sftp").Every(config.Get().System.ActivitySendInterval).Seconds().Do(func() { + _, _ = s.Tag("sftp").Every(interval).Do(func() { + log.WithField("cron", "sftp").Debug("sending sftp events to Panel") if err := sftp.Run(ctx); err != nil { if errors.Is(err, ErrCronRunning) { log.WithField("cron", "sftp").Warn("sftp events process already running, skipping...") diff --git a/internal/cron/sftp_cron.go b/internal/cron/sftp_cron.go index f11bd8c..8d24c39 100644 --- a/internal/cron/sftp_cron.go +++ b/internal/cron/sftp_cron.go @@ -14,7 +14,7 @@ import ( type sftpCron struct { mu *system.AtomicBool manager *server.Manager - max int64 + max int } type mapKey struct { @@ -52,7 +52,7 @@ func (sc *sftpCron) Run(ctx context.Context) error { events := &eventMap{ m: map[mapKey]*models.Activity{}, ids: []int{}, - max: int(sc.max), + max: sc.max, } for { @@ -102,7 +102,7 @@ func (sc *sftpCron) fetchRecords(ctx context.Context, offset int) (activity []mo Where("event LIKE ?", "server:sftp.%"). Order("event DESC"). Offset(offset). - Limit(int(sc.max)). + Limit(sc.max). Find(&activity) if tx.Error != nil { err = errors.WithStack(tx.Error)