docker-s3-backup/internal/schedule.go

32 lines
590 B
Go
Raw Permalink Normal View History

2023-09-14 22:15:37 +00:00
package internal
import (
"context"
"time"
"github.com/go-co-op/gocron"
)
var (
scheduler *gocron.Scheduler
)
func StartSchedule(ctx context.Context) error {
2023-09-19 11:48:08 +00:00
logger.Info("Starting schedule")
2023-09-14 22:15:37 +00:00
cronExpression := GetEnvWithDefault("SCHEDULE_CRON", "0 0 * * *")
scheduler = gocron.NewScheduler(time.Local)
2023-09-19 11:48:08 +00:00
job, err := scheduler.Cron(cronExpression).Do(Backup, ctx)
2023-09-14 22:15:37 +00:00
if err != nil {
return err
}
2023-09-19 11:48:08 +00:00
logger.Debug("Next scheduled backup", "time", job.NextRun())
2023-09-14 22:15:37 +00:00
scheduler.StartBlocking()
return nil
}
func StopSchedule() {
if scheduler != nil {
scheduler.StopBlockingChan()
}
}