scheduler: fix missing default sheduler timeout causing scheduler to
Some checks failed
/ push (push) Has been cancelled

never runs
This commit is contained in:
Tigor Hutasuhut 2024-05-21 22:27:11 +07:00
parent a98dd2d33e
commit f78661aa6e
5 changed files with 24 additions and 22 deletions

View file

@ -4,15 +4,12 @@ package scheduler
//
// If job does not exist, it will be a no-op.
func (scheduler *Scheduler) Delete(subreddit string) {
scheduler.delete(subreddit, true)
}
func (scheduler *Scheduler) delete(subreddit string, lock bool) {
if lock {
scheduler.mu.Lock()
defer scheduler.mu.Unlock()
scheduler.delete(subreddit)
}
func (scheduler *Scheduler) delete(subreddit string) {
job := scheduler.get(subreddit, false)
if job != nil {
scheduler.scheduler.Remove(job.ID)

View file

@ -11,10 +11,12 @@ import (
//
// If job already exists, it will be replaced.
func (scheduler *Scheduler) Put(subreddit string, schedule string) (job *Job, err error) {
return scheduler.put(subreddit, schedule, true)
scheduler.mu.Lock()
defer scheduler.mu.Unlock()
return scheduler.put(subreddit, schedule)
}
func (scheduler *Scheduler) put(subreddit string, schedule string, lock bool) (job *Job, err error) {
func (scheduler *Scheduler) put(subreddit string, schedule string) (job *Job, err error) {
sched, err := cron.ParseStandard(schedule)
if err != nil {
return nil, errs.
@ -22,18 +24,13 @@ func (scheduler *Scheduler) put(subreddit string, schedule string, lock bool) (j
Code(http.StatusBadRequest)
}
scheduler.delete(subreddit, false)
scheduler.delete(subreddit)
id := scheduler.scheduler.Schedule(sched, cron.FuncJob(func() { scheduler.run(subreddit) }))
e := scheduler.scheduler.Entry(id)
job = &Job{ID: id, Entry: e}
if lock {
scheduler.mu.Lock()
defer scheduler.mu.Unlock()
}
scheduler.entries[subreddit] = job
return job, nil

View file

@ -30,17 +30,15 @@ func (scheduler *Scheduler) Sync(ctx context.Context, db bob.Executor) (err erro
scheduler.scheduler.Remove(entry.ID)
}
var errcoll error
errcoll := make([]error, 0, len(subs))
for _, sub := range subs {
_, err := scheduler.put(sub.Name, sub.Schedule, false)
if err != nil {
errcoll = errors.Join(errcoll, errs.Wrapw(err, "scheduler: rebalance: failed to add job", "subreddit", sub.Name, "schedule", sub.Schedule))
}
_, err := scheduler.put(sub.Name, sub.Schedule)
errcoll = append(errcoll, err)
}
if errcoll != nil {
return errs.Wrapw(errcoll, "scheduler: encountered errors while rebalancing jobs")
if err := errors.Join(errcoll...); err != nil {
return errs.Wrapw(err, "scheduler: encountered errors while rebalancing jobs")
}
return nil

View file

@ -46,5 +46,12 @@ func (api *API) SubredditsCreate(ctx context.Context, params *models.Subreddit)
return subreddit, errs.Wrapw(err, "failed to set schedule status")
}
if params.EnableSchedule == 1 {
_, err = api.scheduler.Put(subreddit.Name, subreddit.Schedule)
if err != nil {
return subreddit, errs.Wrapw(err, "failed to put job to scheduler")
}
}
return subreddit, nil
}

View file

@ -2,6 +2,7 @@ package config
import (
"path"
"time"
"github.com/adrg/xdg"
)
@ -64,4 +65,6 @@ var DefaultConfig = map[string]any{
"runtime.version": Version,
"runtime.environment": "development",
"scheduler.timeout": time.Second * 10,
}