Redmage/api/scheduler/scheduler.go
Tigor Hutasuhut 2652933361
Some checks failed
/ push (push) Has been cancelled
api: fix scheduler deadlocks
2024-05-14 18:49:45 +07:00

47 lines
700 B
Go

package scheduler
import (
"sync"
"github.com/robfig/cron/v3"
)
type Runner = func(subreddit string)
type Scheduler struct {
scheduler *cron.Cron
mu sync.RWMutex
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(),
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()
}