Browse Source

Add Prev time record to the entry

Prev records the previous time (if any) that the job was run.
Rob Figueiredo 13 năm trước cách đây
mục cha
commit
1f4c26d7c0
1 tập tin đã thay đổi với 12 bổ sung2 xóa
  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
 }