2022-07-09 21:51:19 +00:00
|
|
|
package cron
|
|
|
|
|
|
|
|
import (
|
2022-07-10 20:51:11 +00:00
|
|
|
"context"
|
2022-10-05 23:24:11 +00:00
|
|
|
"time"
|
|
|
|
|
2022-07-09 21:51:19 +00:00
|
|
|
"emperror.dev/errors"
|
2022-07-24 19:59:17 +00:00
|
|
|
log2 "github.com/apex/log"
|
2022-07-09 21:51:19 +00:00
|
|
|
"github.com/go-co-op/gocron"
|
2022-10-06 15:58:42 +00:00
|
|
|
|
2022-07-09 21:51:19 +00:00
|
|
|
"github.com/pterodactyl/wings/config"
|
|
|
|
"github.com/pterodactyl/wings/server"
|
|
|
|
"github.com/pterodactyl/wings/system"
|
|
|
|
)
|
|
|
|
|
2022-07-10 18:30:32 +00:00
|
|
|
const ErrCronRunning = errors.Sentinel("cron: job already running")
|
|
|
|
|
2022-07-09 21:51:19 +00:00
|
|
|
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) {
|
2022-07-09 21:51:19 +00:00
|
|
|
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-09 21:51:19 +00:00
|
|
|
|
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)
|
2022-07-24 19:59:17 +00:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2022-07-24 19:59:17 +00:00
|
|
|
_, _ = 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
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2022-07-09 21:51:19 +00:00
|
|
|
return s, nil
|
|
|
|
}
|