Redmage/api/scheduler/scheduler.go

47 lines
700 B
Go
Raw Normal View History

package scheduler
import (
"sync"
"github.com/robfig/cron/v3"
)
type Runner = func(subreddit string)
type Scheduler struct {
scheduler *cron.Cron
mu sync.RWMutex
2024-05-14 18:49:45 +07:00
entries map[string]*Job
run Runner
}
type Job struct {
ID cron.EntryID
Entry cron.Entry
}
func (job *Job) clone() *Job {
return &Job{
ID: job.ID,
Entry: job.Entry,
}
}
func New(runner Runner) *Scheduler {
return &Scheduler{
scheduler: cron.New(),
2024-05-14 18:49:45 +07:00
entries: make(map[string]*Job),
run: runner,
}
}
// Start starts the scheduler in the background.
func (s *Scheduler) Start() {
s.scheduler.Start()
}
// Stop stops the scheduler.
func (s *Scheduler) Stop() {
s.scheduler.Stop()
}