// This library implements a cron spec parser and runner. See the README for // more details. package cron import ( "sort" "time" ) // Cron keeps track of any number of entries, invoking the associated func as // specified by the spec. See http://en.wikipedia.org/wiki/Cron // It may be started and stopped. type Cron struct { entries []*Entry stop chan struct{} add chan *Entry snapshot chan []*Entry running bool } // Simple interface for submitted cron jobs. type Job interface { Run() } // A cron entry consists of a schedule and the func to execute on that schedule. type Entry struct { *Schedule Next time.Time Job Job } type byTime []*Entry func (s byTime) Len() int { return len(s) } func (s byTime) Swap(i, j int) { s[i], s[j] = s[j], s[i] } func (s byTime) Less(i, j int) bool { // Two zero times should return false. // Otherwise, zero is "greater" than any other time. // (To sort it at the end of the list.) if s[i].Next.IsZero() { return false } if s[j].Next.IsZero() { return true } return s[i].Next.Before(s[j].Next) } func New() *Cron { return &Cron{ entries: nil, add: make(chan *Entry), stop: make(chan struct{}), snapshot: make(chan []*Entry), running: false, } } // Provide a default implementation for those that want to run a simple func. type jobAdapter func() func (r jobAdapter) Run() { r() } func (c *Cron) AddFunc(spec string, cmd func()) { c.AddJob(spec, jobAdapter(cmd)) } func (c *Cron) AddJob(spec string, cmd Job) { entry := &Entry{Parse(spec), time.Time{}, cmd} if !c.running { c.entries = append(c.entries, entry) return } c.add <- entry } // Return a snapshot of the cron entries. func (c *Cron) Entries() []*Entry { if c.running { c.snapshot <- nil x := <-c.snapshot return x } return c.entrySnapshot() } func (c *Cron) Start() { c.running = true go c.run() } // Run the scheduler.. this is private just due to the need to synchronize // access to the 'running' state variable. func (c *Cron) run() { // Figure out the next activation times for each entry. now := time.Now().Local() for _, entry := range c.entries { entry.Next = entry.Schedule.Next(now) } for { // Determine the next entry to run. sort.Sort(byTime(c.entries)) var effective time.Time if len(c.entries) == 0 || c.entries[0].Next.IsZero() { // If there are no entries yet, just sleep - it still handles new entries // and stop requests. effective = now.AddDate(10, 0, 0) } else { effective = c.entries[0].Next } select { case now = <-time.After(effective.Sub(now)): // Run every entry whose next time was this effective time. for _, e := range c.entries { if e.Next != effective { break } go e.Job.Run() e.Next = e.Schedule.Next(effective) } case newEntry := <-c.add: c.entries = append(c.entries, newEntry) newEntry.Next = newEntry.Schedule.Next(now) case <-c.snapshot: c.snapshot <- c.entrySnapshot() case <-c.stop: return } } } func (c *Cron) Stop() { c.stop <- struct{}{} c.running = false } func (c *Cron) entrySnapshot() []*Entry { entries := []*Entry{} for _, e := range c.entries { entries = append(entries, &Entry{e.Schedule, e.Next, e.Job}) } return entries }