2024-05-13 21:10:48 +07:00
|
|
|
package scheduler
|
|
|
|
|
2024-05-14 09:51:53 +07:00
|
|
|
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
|
2024-05-14 09:51:53 +07:00
|
|
|
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),
|
2024-05-14 09:51:53 +07:00
|
|
|
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()
|
|
|
|
}
|