Skip to content

Commit c1ee161

Browse files
authored
Add WithTime method for scheduling tasks. WithStartTime is deprecated. (#10)
* Add WithTime method for scheduling tasks. WithStartTime is deprecated. * Add new go versions for travis
1 parent 69e273a commit c1ee161

File tree

4 files changed

+132
-3
lines changed

4 files changed

+132
-3
lines changed

.travis.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ go:
44
- 1.13.x
55
- 1.14.x
66
- 1.15.x
7+
- 1.16.x
8+
- 1.17.x
9+
- 1.18.x
710

811
before_install:
912
- go get -t -v ./...

README.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,24 @@
77
Chrono is a scheduler library that lets you run your tasks and code periodically. It provides different scheduling functionalities to make it easier to create a scheduling task.
88

99
## Scheduling a One-Shot Task
10-
The Schedule method helps us schedule the task to run once at the specified time. In the following example, the task will first be executed 1 second after the current time. 
11-
**WithStartTime** option is used to specify the execution time.
10+
The Schedule method helps us schedule the task to run once at the specified time. In the following example, the task will first be executed 1 second after the current time.
11+
**WithTime** option is used to specify the execution time.
12+
13+
```go
14+
taskScheduler := chrono.NewDefaultTaskScheduler()
15+
now := time.Now()
16+
startTime := now.Add(time.Second * 1)
17+
18+
task, err := taskScheduler.Schedule(func(ctx context.Context) {
19+
log.Print("One-Shot Task")
20+
}, WithTime(startTime))
21+
22+
if err == nil {
23+
log.Print("Task has been scheduled successfully.")
24+
}
25+
```
26+
27+
Also, **WithStartTime** option can be used to specify the execution time. **But It's deprecated.**
1228

1329
```go
1430
taskScheduler := chrono.NewDefaultTaskScheduler()

scheduler_test.go

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,25 @@ func TestDefaultTaskScheduler(t *testing.T) {
2626
"number of scheduled task execution must be 1, actual: %d", counter)
2727
}
2828

29+
func TestDefaultTaskSchedulerWithTimeOption(t *testing.T) {
30+
scheduler := NewDefaultTaskScheduler()
31+
32+
var counter int32
33+
now := time.Now()
34+
starTime := now.Add(time.Second * 1)
35+
36+
task, err := scheduler.Schedule(func(ctx context.Context) {
37+
atomic.AddInt32(&counter, 1)
38+
}, WithTime(starTime))
39+
40+
assert.Nil(t, err)
41+
42+
<-time.After(2 * time.Second)
43+
assert.True(t, task.IsCancelled(), "scheduled task must have been cancelled")
44+
assert.True(t, counter == 1,
45+
"number of scheduled task execution must be 1, actual: %d", counter)
46+
}
47+
2948
func TestSimpleTaskScheduler_ScheduleWithoutTask(t *testing.T) {
3049
scheduler := NewDefaultTaskScheduler()
3150
task, err := scheduler.Schedule(nil)
@@ -81,7 +100,26 @@ func TestSimpleTaskScheduler_WithoutScheduledExecutor(t *testing.T) {
81100
"number of scheduled task execution must be 1, actual: %d", counter)
82101
}
83102

84-
func TestSimpleTaskScheduler_Schedule_OneShotTask(t *testing.T) {
103+
func TestSimpleTaskScheduler_WithoutScheduledExecutorWithTimeOption(t *testing.T) {
104+
scheduler := NewSimpleTaskScheduler(nil)
105+
106+
var counter int32
107+
now := time.Now()
108+
startTime := now.Add(time.Second * 1)
109+
110+
task, err := scheduler.Schedule(func(ctx context.Context) {
111+
atomic.AddInt32(&counter, 1)
112+
}, WithTime(startTime))
113+
114+
assert.Nil(t, err)
115+
116+
<-time.After(2 * time.Second)
117+
assert.True(t, task.IsCancelled(), "scheduled task must have been cancelled")
118+
assert.True(t, counter == 1,
119+
"number of scheduled task execution must be 1, actual: %d", counter)
120+
}
121+
122+
func TestSimpleTaskScheduler_ScheduleOneShotTaskWithStartTimeOption(t *testing.T) {
85123
scheduler := NewSimpleTaskScheduler(NewDefaultTaskExecutor())
86124

87125
var counter int32
@@ -99,6 +137,25 @@ func TestSimpleTaskScheduler_Schedule_OneShotTask(t *testing.T) {
99137
"number of scheduled task execution must be 1, actual: %d", counter)
100138
}
101139

140+
func TestSimpleTaskScheduler_ScheduleOneShotTaskWithTimeOption(t *testing.T) {
141+
scheduler := NewSimpleTaskScheduler(NewDefaultTaskExecutor())
142+
143+
var counter int32
144+
now := time.Now()
145+
startTime := now.Add(time.Second * 1)
146+
147+
task, err := scheduler.Schedule(func(ctx context.Context) {
148+
atomic.AddInt32(&counter, 1)
149+
}, WithTime(startTime))
150+
151+
assert.Nil(t, err)
152+
153+
<-time.After(2 * time.Second)
154+
assert.True(t, task.IsCancelled(), "scheduled task must have been cancelled")
155+
assert.True(t, counter == 1,
156+
"number of scheduled task execution must be 1, actual: %d", counter)
157+
}
158+
102159
func TestSimpleTaskScheduler_ScheduleWithFixedDelay(t *testing.T) {
103160
scheduler := NewSimpleTaskScheduler(NewDefaultTaskExecutor())
104161

@@ -136,6 +193,26 @@ func TestSimpleTaskScheduler_ScheduleWithFixedDelayWithStartTimeOption(t *testin
136193
"number of scheduled task execution must be between 1 and 3, actual: %d", counter)
137194
}
138195

196+
func TestSimpleTaskScheduler_ScheduleWithFixedDelayWithTimeOption(t *testing.T) {
197+
scheduler := NewSimpleTaskScheduler(NewDefaultTaskExecutor())
198+
199+
var counter int32
200+
now := time.Now()
201+
startTime := now.Add(time.Second * 1)
202+
203+
task, err := scheduler.ScheduleWithFixedDelay(func(ctx context.Context) {
204+
atomic.AddInt32(&counter, 1)
205+
<-time.After(500 * time.Millisecond)
206+
}, 200*time.Millisecond, WithTime(startTime))
207+
208+
assert.Nil(t, err)
209+
210+
<-time.After(2*time.Second + 500*time.Millisecond)
211+
task.Cancel()
212+
assert.True(t, counter >= 1 && counter <= 3,
213+
"number of scheduled task execution must be between 1 and 3, actual: %d", counter)
214+
}
215+
139216
func TestSimpleTaskScheduler_ScheduleAtFixedRate(t *testing.T) {
140217
scheduler := NewSimpleTaskScheduler(NewDefaultTaskExecutor())
141218

@@ -172,6 +249,26 @@ func TestSimpleTaskScheduler_ScheduleAtFixedRateWithStartTimeOption(t *testing.T
172249
"number of scheduled task execution must be between 5 and 10, actual: %d", counter)
173250
}
174251

252+
func TestSimpleTaskScheduler_ScheduleAtFixedRateWithTimeOption(t *testing.T) {
253+
scheduler := NewSimpleTaskScheduler(NewDefaultTaskExecutor())
254+
255+
var counter int32
256+
now := time.Now()
257+
startTime := now.Add(time.Second * 1)
258+
259+
task, err := scheduler.ScheduleAtFixedRate(func(ctx context.Context) {
260+
atomic.AddInt32(&counter, 1)
261+
<-time.After(500 * time.Millisecond)
262+
}, 200*time.Millisecond, WithTime(startTime))
263+
264+
assert.Nil(t, err)
265+
266+
<-time.After(3*time.Second - 50*time.Millisecond)
267+
task.Cancel()
268+
assert.True(t, counter >= 5 && counter <= 10,
269+
"number of scheduled task execution must be between 5 and 10, actual: %d", counter)
270+
}
271+
175272
func TestSimpleTaskScheduler_ScheduleWithCron(t *testing.T) {
176273
scheduler := NewSimpleTaskScheduler(NewDefaultTaskExecutor())
177274

task.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,19 @@ func (task *SchedulerTask) GetInitialDelay() time.Duration {
5757

5858
type Option func(task *SchedulerTask) error
5959

60+
func WithTime(t time.Time) Option {
61+
return func(task *SchedulerTask) error {
62+
task.startTime = time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), 0, time.Local)
63+
64+
if t.Location() != nil && t.Location() != time.Local {
65+
task.location = t.Location()
66+
}
67+
68+
return nil
69+
}
70+
}
71+
72+
// Deprecated: Use WithTime instead.
6073
func WithStartTime(year int, month time.Month, day, hour, min, sec int) Option {
6174
return func(task *SchedulerTask) error {
6275
task.startTime = time.Date(year, month, day, hour, min, sec, 0, time.Local)

0 commit comments

Comments
 (0)