Redmage/api/scheduler/put.go

38 lines
946 B
Go
Raw Normal View History

package scheduler
import (
"net/http"
"github.com/robfig/cron/v3"
"github.com/tigorlazuardi/redmage/pkg/errs"
)
// Put adds a job to the scheduler.
//
// If job already exists, it will be replaced.
func (scheduler *Scheduler) Put(subreddit string, schedule string) (job *Job, err error) {
scheduler.mu.Lock()
defer scheduler.mu.Unlock()
return scheduler.put(subreddit, schedule)
2024-05-14 18:49:45 +07:00
}
func (scheduler *Scheduler) put(subreddit string, schedule string) (job *Job, err error) {
sched, err := cron.ParseStandard(schedule)
if err != nil {
return nil, errs.
Wrapw(err, "scheduler: failed to parse schedule", "subreddit", subreddit, "schedule", schedule).
Code(http.StatusBadRequest)
}
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}
2024-05-14 18:49:45 +07:00
scheduler.entries[subreddit] = job
return job, nil
}