Skip to content

Commit 1621be0

Browse files
authored
Add examples from README to example_test.go (#66)
1 parent 31e21f6 commit 1621be0

File tree

1 file changed

+62
-3
lines changed

1 file changed

+62
-3
lines changed

example_test.go

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,40 @@ var task = func() {
1313

1414
func ExampleScheduler_StartBlocking() {
1515
s := gocron.NewScheduler(time.UTC)
16-
s.Every(3).Seconds().Do(task)
16+
_, _ = s.Every(3).Seconds().Do(task)
1717
s.StartBlocking()
1818
}
1919

20+
func ExampleScheduler_StartAsync() {
21+
s := gocron.NewScheduler(time.UTC)
22+
_, _ = s.Every(3).Seconds().Do(task)
23+
<-s.StartAsync()
24+
}
25+
26+
func ExampleScheduler_StartImmediately() {
27+
s := gocron.NewScheduler(time.UTC)
28+
_, _ = s.Every(1).Hour().StartImmediately().Do(task)
29+
s.StartBlocking()
30+
}
31+
32+
func ExampleScheduler_StartAt() {
33+
s := gocron.NewScheduler(time.UTC)
34+
specificTime := time.Date(2019, time.November, 10, 15, 0, 0, 0, time.UTC)
35+
_, _ = s.Every(1).Hour().StartAt(specificTime).Do(task)
36+
s.StartBlocking()
37+
}
38+
39+
func ExampleScheduler_Stop() {
40+
s := gocron.NewScheduler(time.UTC)
41+
_, _ = s.Every(1).Second().Do(task)
42+
s.StartAsync()
43+
s.Stop()
44+
}
45+
2046
func ExampleScheduler_At() {
2147
s := gocron.NewScheduler(time.UTC)
22-
s.Every(1).Day().At("10:30").Do(task)
23-
s.Every(1).Monday().At("10:30:01").Do(task)
48+
_, _ = s.Every(1).Day().At("10:30").Do(task)
49+
_, _ = s.Every(1).Monday().At("10:30:01").Do(task)
2450
}
2551

2652
func ExampleJob_ScheduledTime() {
@@ -29,3 +55,36 @@ func ExampleJob_ScheduledTime() {
2955
fmt.Println(job.ScheduledAtTime())
3056
// Output: 10:30
3157
}
58+
59+
func ExampleScheduler_RemoveJobByTag() {
60+
s := gocron.NewScheduler(time.UTC)
61+
tag1 := []string{"tag1"}
62+
tag2 := []string{"tag2"}
63+
_, _ = s.Every(1).Week().SetTag(tag1).Do(task)
64+
_, _ = s.Every(1).Week().SetTag(tag2).Do(task)
65+
s.StartAsync()
66+
_ = s.RemoveJobByTag("tag1")
67+
}
68+
69+
func ExampleScheduler_NextRun() {
70+
s := gocron.NewScheduler(time.UTC)
71+
_, _ = s.Every(1).Day().At("10:30").Do(task)
72+
s.StartAsync()
73+
_, time := s.NextRun()
74+
fmt.Println(time.Format("15:04")) // print only the hour and minute (hh:mm)
75+
// Output: 10:30
76+
}
77+
78+
func ExampleScheduler_Clear() {
79+
s := gocron.NewScheduler(time.UTC)
80+
_, _ = s.Every(1).Second().Do(task)
81+
_, _ = s.Every(1).Minute().Do(task)
82+
_, _ = s.Every(1).Month(1).Do(task)
83+
fmt.Println(len(s.Jobs())) // Print the number of jobs before clearing
84+
s.Clear() // Clear all the jobs
85+
fmt.Println(len(s.Jobs())) // Print the number of jobs after clearing
86+
s.StartAsync()
87+
// Output:
88+
// 3
89+
// 0
90+
}

0 commit comments

Comments
 (0)