wings/internal/cron/cron.go

72 lines
2.3 KiB
Go
Raw Permalink Normal View History

package cron
import (
2022-07-10 20:51:11 +00:00
"context"
"emperror.dev/errors"
log2 "github.com/apex/log"
"github.com/go-co-op/gocron"
"github.com/pterodactyl/wings/config"
"github.com/pterodactyl/wings/server"
"github.com/pterodactyl/wings/system"
"time"
)
2022-07-10 18:30:32 +00:00
const ErrCronRunning = errors.Sentinel("cron: job already running")
var o system.AtomicBool
// Scheduler configures the internal cronjob system for Wings and returns the scheduler
// instance to the caller. This should only be called once per application lifecycle, additional
// calls will result in an error being returned.
2022-07-10 20:51:11 +00:00
func Scheduler(ctx context.Context, m *server.Manager) (*gocron.Scheduler, error) {
2022-07-10 18:30:32 +00:00
if !o.SwapIf(true) {
return nil, errors.New("cron: cannot call scheduler more than once in application lifecycle")
}
l, err := time.LoadLocation(config.Get().System.Timezone)
if err != nil {
return nil, errors.Wrap(err, "cron: failed to parse configured system timezone")
}
2022-07-10 20:51:11 +00:00
activity := activityCron{
mu: system.NewAtomicBool(false),
manager: m,
max: config.Get().System.ActivitySendCount,
}
2022-07-24 18:40:06 +00:00
sftp := sftpCron{
mu: system.NewAtomicBool(false),
manager: m,
max: config.Get().System.ActivitySendCount,
}
2022-07-10 20:51:11 +00:00
s := gocron.NewScheduler(l)
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")
2022-07-10 20:51:11 +00:00
if err := activity.Run(ctx); err != nil {
2022-07-10 18:30:32 +00:00
if errors.Is(err, ErrCronRunning) {
2022-07-24 18:40:06 +00:00
log.WithField("cron", "activity").Warn("activity process is already running, skipping...")
} else {
log.WithField("cron", "activity").WithField("error", err).Error("activity process failed to execute")
}
}
})
_, _ = s.Tag("sftp").Every(interval).Do(func() {
log.WithField("cron", "sftp").Debug("sending sftp events to Panel")
2022-07-24 18:40:06 +00:00
if err := sftp.Run(ctx); err != nil {
if errors.Is(err, ErrCronRunning) {
log.WithField("cron", "sftp").Warn("sftp events process already running, skipping...")
2022-07-10 18:30:32 +00:00
} else {
2022-07-24 18:40:06 +00:00
log.WithField("cron", "sftp").WithField("error", err).Error("sftp events process failed to execute")
2022-07-10 18:30:32 +00:00
}
}
})
return s, nil
}