Fix crons to actually run correctly using the configuration values

This commit is contained in:
DaneEveritt 2022-07-24 15:59:17 -04:00
parent 4634c93182
commit 251f91a08e
No known key found for this signature in database
GPG Key ID: EEA66103B3D71F53
4 changed files with 17 additions and 10 deletions

View File

@ -167,10 +167,10 @@ type SystemConfiguration struct {
// being sent to the Panel. By default this will send activity collected over the last minute. Keep // 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 mind that only a fixed number of activity log entries, defined by ActivitySendCount, will be sent
// in each run. // 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 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 // 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 // booted. This can cause boot delays if the server has a large amount of files. In most

View File

@ -12,7 +12,7 @@ import (
type activityCron struct { type activityCron struct {
mu *system.AtomicBool mu *system.AtomicBool
manager *server.Manager manager *server.Manager
max int64 max int
} }
// Run executes the cronjob and ensures we fetch and send all of the stored activity to the // 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 var activity []models.Activity
tx := database.Instance().WithContext(ctx). tx := database.Instance().WithContext(ctx).
Where("event NOT LIKE ?", "server:sftp.%"). Where("event NOT LIKE ?", "server:sftp.%").
Limit(int(ac.max)). Limit(ac.max).
Find(&activity) Find(&activity)
if tx.Error != nil { if tx.Error != nil {

View File

@ -3,7 +3,7 @@ package cron
import ( import (
"context" "context"
"emperror.dev/errors" "emperror.dev/errors"
"github.com/apex/log" log2 "github.com/apex/log"
"github.com/go-co-op/gocron" "github.com/go-co-op/gocron"
"github.com/pterodactyl/wings/config" "github.com/pterodactyl/wings/config"
"github.com/pterodactyl/wings/server" "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 := 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 err := activity.Run(ctx); err != nil {
if errors.Is(err, ErrCronRunning) { if errors.Is(err, ErrCronRunning) {
log.WithField("cron", "activity").Warn("activity process is already running, skipping...") 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 err := sftp.Run(ctx); err != nil {
if errors.Is(err, ErrCronRunning) { if errors.Is(err, ErrCronRunning) {
log.WithField("cron", "sftp").Warn("sftp events process already running, skipping...") log.WithField("cron", "sftp").Warn("sftp events process already running, skipping...")

View File

@ -14,7 +14,7 @@ import (
type sftpCron struct { type sftpCron struct {
mu *system.AtomicBool mu *system.AtomicBool
manager *server.Manager manager *server.Manager
max int64 max int
} }
type mapKey struct { type mapKey struct {
@ -52,7 +52,7 @@ func (sc *sftpCron) Run(ctx context.Context) error {
events := &eventMap{ events := &eventMap{
m: map[mapKey]*models.Activity{}, m: map[mapKey]*models.Activity{},
ids: []int{}, ids: []int{},
max: int(sc.max), max: sc.max,
} }
for { for {
@ -102,7 +102,7 @@ func (sc *sftpCron) fetchRecords(ctx context.Context, offset int) (activity []mo
Where("event LIKE ?", "server:sftp.%"). Where("event LIKE ?", "server:sftp.%").
Order("event DESC"). Order("event DESC").
Offset(offset). Offset(offset).
Limit(int(sc.max)). Limit(sc.max).
Find(&activity) Find(&activity)
if tx.Error != nil { if tx.Error != nil {
err = errors.WithStack(tx.Error) err = errors.WithStack(tx.Error)