2024-05-14 09:51:53 +07:00
|
|
|
package scheduler
|
|
|
|
|
|
|
|
// List returns a copy of the list of jobs.
|
|
|
|
func (scheduler *Scheduler) List() map[string]*Job {
|
2024-05-14 18:49:45 +07:00
|
|
|
return scheduler.list(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (scheduler *Scheduler) list(lock bool) map[string]*Job {
|
|
|
|
if lock {
|
|
|
|
scheduler.mu.RLock()
|
|
|
|
defer scheduler.mu.RUnlock()
|
|
|
|
}
|
2024-05-14 09:51:53 +07:00
|
|
|
|
2024-05-14 18:49:45 +07:00
|
|
|
m := make(map[string]*Job, len(scheduler.entries))
|
|
|
|
for k, v := range scheduler.entries {
|
2024-05-14 09:51:53 +07:00
|
|
|
m[k] = v.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get returns the job for the given subreddit.
|
|
|
|
//
|
|
|
|
// Returns nil if the subreddit is not found or active.
|
|
|
|
func (scheduler *Scheduler) Get(subreddit string) *Job {
|
2024-05-14 18:49:45 +07:00
|
|
|
return scheduler.get(subreddit, true)
|
|
|
|
}
|
2024-05-14 09:51:53 +07:00
|
|
|
|
2024-05-14 18:49:45 +07:00
|
|
|
func (scheduler *Scheduler) get(subreddit string, lock bool) *Job {
|
|
|
|
if lock {
|
|
|
|
scheduler.mu.RLock()
|
|
|
|
defer scheduler.mu.RUnlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
schedule := scheduler.entries[subreddit]
|
2024-05-14 09:51:53 +07:00
|
|
|
if schedule != nil {
|
|
|
|
return schedule.clone()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (scheduler *Scheduler) Iter(f func(string, *Job) bool) {
|
2024-05-14 18:49:45 +07:00
|
|
|
scheduler.iter(f, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (scheduler *Scheduler) iter(f func(string, *Job) bool, lock bool) {
|
|
|
|
if lock {
|
|
|
|
scheduler.mu.RLock()
|
|
|
|
defer scheduler.mu.RUnlock()
|
|
|
|
}
|
2024-05-14 09:51:53 +07:00
|
|
|
|
2024-05-14 18:49:45 +07:00
|
|
|
for k, v := range scheduler.entries {
|
2024-05-14 09:51:53 +07:00
|
|
|
if !f(k, v.clone()) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|