Explorar el Código

Add Prev time record to the entry

Prev records the previous time (if any) that the job was run.
Rob Figueiredo hace 13 años
padre
commit
1f4c26d7c0
Se han modificado 1 ficheros con 12 adiciones y 2 borrados
  1. 12 2
      cron.go

+ 12 - 2
cron.go

@@ -27,6 +27,7 @@ type Job interface {
 type Entry struct {
 	*Schedule
 	Next time.Time
+	Prev time.Time
 	Job  Job
 }
 
@@ -67,7 +68,10 @@ func (c *Cron) AddFunc(spec string, cmd func()) {
 }
 
 func (c *Cron) AddJob(spec string, cmd Job) {
-	entry := &Entry{Parse(spec), time.Time{}, cmd}
+	entry := &Entry{
+		Schedule: Parse(spec),
+		Job:      cmd,
+	}
 	if !c.running {
 		c.entries = append(c.entries, entry)
 		return
@@ -121,6 +125,7 @@ func (c *Cron) run() {
 					break
 				}
 				go e.Job.Run()
+				e.Prev = e.Next
 				e.Next = e.Schedule.Next(effective)
 			}
 
@@ -145,7 +150,12 @@ func (c *Cron) Stop() {
 func (c *Cron) entrySnapshot() []*Entry {
 	entries := []*Entry{}
 	for _, e := range c.entries {
-		entries = append(entries, &Entry{e.Schedule, e.Next, e.Job})
+		entries = append(entries, &Entry{
+			Schedule: e.Schedule,
+			Next:     e.Next,
+			Prev:     e.Prev,
+			Job:      e.Job,
+		})
 	}
 	return entries
 }