Skip to content

Commit 35d6703

Browse files
author
linx
committed
init
1 parent e78485a commit 35d6703

File tree

7 files changed

+60
-11
lines changed

7 files changed

+60
-11
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.idea

cron.go

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package gron
22

33
import (
4+
"fmt"
45
"sort"
56
"time"
67
)
@@ -17,6 +18,12 @@ type Entry struct {
1718
// the last time the job was run. This is zero time if the job has not been
1819
// run.
1920
Prev time.Time
21+
22+
// job begin time, default is time.Now()
23+
Begin time.Time
24+
25+
// job name
26+
Name string
2027
}
2128

2229
// byTime is a handy wrapper to chronologically sort entries.
@@ -57,13 +64,15 @@ type Cron struct {
5764
running bool
5865
add chan *Entry
5966
stop chan struct{}
67+
debug bool
6068
}
6169

6270
// New instantiates new Cron instant c.
63-
func New() *Cron {
71+
func New(debug bool) *Cron {
6472
return &Cron{
65-
stop: make(chan struct{}),
66-
add: make(chan *Entry),
73+
stop: make(chan struct{}),
74+
add: make(chan *Entry),
75+
debug: debug,
6776
}
6877
}
6978

@@ -77,11 +86,20 @@ func (c *Cron) Start() {
7786
//
7887
// if cron instant is not running, adding to entries is trivial.
7988
// otherwise, to prevent data-race, adds through channel.
80-
func (c *Cron) Add(s Schedule, j Job) {
89+
func (c *Cron) Add(n string, s Schedule, j Job, begin ...time.Time) {
8190

91+
var b = time.Now().Local()
92+
if len(begin) >= 1 {
93+
b = begin[0]
94+
}
8295
entry := &Entry{
8396
Schedule: s,
8497
Job: j,
98+
Begin: b,
99+
Name: n,
100+
}
101+
if c.debug {
102+
fmt.Println("add job:", n, " begin:", b.Format(time.RFC3339))
85103
}
86104

87105
if !c.running {
@@ -92,8 +110,8 @@ func (c *Cron) Add(s Schedule, j Job) {
92110
}
93111

94112
// AddFunc registers the Job function for the given Schedule.
95-
func (c *Cron) AddFunc(s Schedule, j func()) {
96-
c.Add(s, JobFunc(j))
113+
func (c *Cron) AddFunc(n string, s Schedule, j func(), begin ...time.Time) {
114+
c.Add(n, s, JobFunc(j), begin...)
97115
}
98116

99117
// Stop halts cron instant c from running.
@@ -119,7 +137,7 @@ func (c *Cron) run() {
119137

120138
// to figure next trig time for entries, referenced from now
121139
for _, e := range c.entries {
122-
e.Next = e.Schedule.Next(now)
140+
e.Next = e.Schedule.Next(e.Begin) //first time start from e.Begin
123141
}
124142

125143
for {
@@ -139,10 +157,13 @@ func (c *Cron) run() {
139157
}
140158
entry.Prev = now
141159
entry.Next = entry.Schedule.Next(now)
160+
if c.debug {
161+
fmt.Println(now.Format(time.RFC3339), " run job:", entry.Name, " next:", entry.Next.Format(time.RFC3339))
162+
}
142163
go entry.Job.Run()
143164
}
144165
case e := <-c.add:
145-
e.Next = e.Schedule.Next(time.Now())
166+
e.Next = e.Schedule.Next(e.Begin)
146167
c.entries = append(c.entries, e)
147168
case <-c.stop:
148169
return // terminate go-routine.

cron_test.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"testing"
1010
"time"
1111

12-
"github.com/roylee0704/gron/xtime"
12+
"github.com/linxlib/gron/xtime"
1313
)
1414

1515
// Most test jobs scheduled to run at 1 second mark.
@@ -67,6 +67,23 @@ func TestAddBeforeRun(t *testing.T) {
6767
}
6868
}
6969

70+
func TestEntryNextRunTimeWithBeginTime(t *testing.T) {
71+
done := make(chan time.Time)
72+
cron := New()
73+
cron.AddFunc(Every(10*time.Second), func() { done <- time.Now() }, time.Now().Add(-time.Second*5))
74+
cron.Start()
75+
defer cron.Stop()
76+
fmt.Println("start:", time.Now())
77+
78+
select {
79+
case <-time.After(10 * OneSecond):
80+
t.FailNow()
81+
case y := <-done:
82+
fmt.Println("end:", y)
83+
}
84+
85+
}
86+
7087
// start a cron, add a job, expect it runs
7188
func TestAddWhileRun(t *testing.T) {
7289
done := make(chan struct{})

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/linxlib/gron
2+
3+
go 1.13

schedule.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"errors"
55
"time"
66

7-
"github.com/roylee0704/gron/xtime"
7+
"github.com/linxlib/gron/xtime"
88
)
99

1010
// Schedule is the interface that wraps the basic Next method.

schedule_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"testing"
66
"time"
77

8-
"github.com/roylee0704/gron/xtime"
8+
"github.com/linxlib/gron/xtime"
99
)
1010

1111
func TestPeriodicAtNext(t *testing.T) {

xtime/xtime.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,10 @@ const (
1616
//Week has 7 days
1717
Week time.Duration = Day * 7
1818
)
19+
20+
var (
21+
BeginOfMonth = time.Now().AddDate(0, 0, -time.Now().Day()+1)
22+
BeginOfMonthZero = time.Date(BeginOfMonth.Year(), BeginOfMonth.Month(), BeginOfMonth.Day(), 0, 0, 0, 0, BeginOfMonth.Location())
23+
EndOfMonth = BeginOfMonth.AddDate(0, 1, -1)
24+
EndOfMonthZero = time.Date(EndOfMonth.Year(), EndOfMonth.Month(), EndOfMonth.Day(), 0, 0, 0, 0, EndOfMonth.Location())
25+
)

0 commit comments

Comments
 (0)