Skip to content

Commit 3abc0f8

Browse files
committed
Adjust changes for Gogs
1 parent 32d9c27 commit 3abc0f8

File tree

1 file changed

+27
-17
lines changed

1 file changed

+27
-17
lines changed

cron.go

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ type Schedule interface {
3232

3333
// Entry consists of a schedule and the func to execute on that schedule.
3434
type Entry struct {
35+
Description string
36+
Spec string
37+
3538
// The schedule on which this job should be run.
3639
Schedule Schedule
3740

@@ -45,6 +48,8 @@ type Entry struct {
4548

4649
// The Job to run.
4750
Job Job
51+
52+
ExecTimes int // Execute times count.
4853
}
4954

5055
// byTime is a wrapper for sorting the entry array by time
@@ -83,32 +88,33 @@ type FuncJob func()
8388
func (f FuncJob) Run() { f() }
8489

8590
// AddFunc adds a func to the Cron to be run on the given schedule.
86-
func (c *Cron) AddFunc(spec string, cmd func()) error {
87-
return c.AddJob(spec, FuncJob(cmd))
91+
func (c *Cron) AddFunc(desc, spec string, cmd func()) (*Entry, error) {
92+
return c.AddJob(desc, spec, FuncJob(cmd))
8893
}
8994

9095
// AddFunc adds a Job to the Cron to be run on the given schedule.
91-
func (c *Cron) AddJob(spec string, cmd Job) error {
96+
func (c *Cron) AddJob(desc, spec string, cmd Job) (*Entry, error) {
9297
schedule, err := Parse(spec)
9398
if err != nil {
94-
return err
99+
return nil, err
95100
}
96-
c.Schedule(schedule, cmd)
97-
return nil
101+
return c.Schedule(desc, spec, schedule, cmd), nil
98102
}
99103

100104
// Schedule adds a Job to the Cron to be run on the given schedule.
101-
func (c *Cron) Schedule(schedule Schedule, cmd Job) {
105+
func (c *Cron) Schedule(desc, spec string, schedule Schedule, cmd Job) *Entry {
102106
entry := &Entry{
103-
Schedule: schedule,
104-
Job: cmd,
107+
Description: desc,
108+
Spec: spec,
109+
Schedule: schedule,
110+
Job: cmd,
105111
}
106-
if !c.running {
112+
if c.running {
113+
c.add <- entry
114+
} else {
107115
c.entries = append(c.entries, entry)
108-
return
109116
}
110-
111-
c.add <- entry
117+
return entry
112118
}
113119

114120
// Entries returns a snapshot of the cron entries.
@@ -157,6 +163,7 @@ func (c *Cron) run() {
157163
break
158164
}
159165
go e.Job.Run()
166+
e.ExecTimes++
160167
e.Prev = e.Next
161168
e.Next = e.Schedule.Next(effective)
162169
}
@@ -192,10 +199,13 @@ func (c *Cron) entrySnapshot() []*Entry {
192199
entries := []*Entry{}
193200
for _, e := range c.entries {
194201
entries = append(entries, &Entry{
195-
Schedule: e.Schedule,
196-
Next: e.Next,
197-
Prev: e.Prev,
198-
Job: e.Job,
202+
Description: e.Description,
203+
Spec: e.Spec,
204+
Schedule: e.Schedule,
205+
Next: e.Next,
206+
Prev: e.Prev,
207+
Job: e.Job,
208+
ExecTimes: e.ExecTimes,
199209
})
200210
}
201211
return entries

0 commit comments

Comments
 (0)