tasks: Create task_schedule config

Use it, just need a frontend and way to edit it.
This commit is contained in:
IRHM
2024-05-19 21:20:23 +01:00
parent 7868015ce6
commit 127393245d
2 changed files with 17 additions and 3 deletions
+3
View File
@@ -58,6 +58,9 @@ type ServerConfig struct {
RADARR []RadarrSettings `json:",omitempty"`
TWITCH game.IGDB `json:",omitempty"`
// Optional: Schedule for tasks.
TASK_SCHEDULE map[string]int `json:",omitempty"`
// Enable/disable debug logging. Useful for when trying
// to figure out exactly what the server is doing at a point
// of failure.
+14 -3
View File
@@ -74,12 +74,18 @@ func setupTasks(db *gorm.DB) {
// Small helper to add a new job to the scheduler.
// Makes the setupTasks function a little easier to read.
func addTaskToScheduler(name string, dur time.Duration) error {
// Gets schedule from config, or `defaultDur` if not manually configured.
func addTaskToScheduler(name string, defaultDur time.Duration) error {
s := defaultDur
if Config.TASK_SCHEDULE[name] != 0 {
s = time.Duration(Config.TASK_SCHEDULE[name]) * time.Second
}
_, err := taskScheduler.NewJob(
gocron.DurationJob(dur),
gocron.DurationJob(s),
gocron.NewTask(taskFuncs[name]),
gocron.WithName(name),
)
slog.Debug("addTaskToScheduler: Job added.", "job_name", name, "duration_used", s, "duration_default", defaultDur)
return err
}
@@ -107,6 +113,7 @@ func getTask(name string) *gocron.Job {
for _, j := range taskScheduler.Jobs() {
if j.Name() == name {
job = &j
break
}
}
return job
@@ -118,7 +125,7 @@ func rescheduleTask(name string, req TaskRescheduleRequest) error {
if j == nil {
return errors.New("no task found")
}
taskScheduler.Update(
_, err := taskScheduler.Update(
(*j).ID(),
gocron.DurationJob(
time.Duration(req.Seconds)*time.Second,
@@ -128,5 +135,9 @@ func rescheduleTask(name string, req TaskRescheduleRequest) error {
),
gocron.WithName(name),
)
if err != nil {
slog.Error("rescheduleTask: Failed to update job!", "error", err)
return errors.New("failed to update job")
}
return nil
}