Skip to content

Commit b27e64d

Browse files
authored
Update task tutorial (#2)
* Add task package. * Update task package. * Make task package works as expected. * Add task examples. * Add task stats and error handler. * Add Coordinator. * Update coordinator. * Bugs fix. * Add more examples. * Some fixes. * Add test cases. * Add go.sum. * Change coordination implementation to setnx. * Fix no at time periodic schedule. * Limit job execution time; limit worker count. * Add jobLock, add Remove and Clear job methods in scheduler. * Add ClearJobs; Update OnceJob scheduler implementation. * Add redis cluster support for coordinator. * Move Coordinate to Run; add unit tests for Coordinate. * Extract commonJob from OnceJob and PeriodicJob. * unexport cron expression type. * Add JobCount. * Add timezone support in cron. * Add Stop for Scheduler. * Add coordinate for oncejob. * extract coordinate to commonJob. * Update unit tests. * Add GetLatestScheduledTime. * Move unit test into a separate package. * Add start flag for scheduler. * make some functions and methods unexported. * Add examples for task. * Change IntervalType to cronIntervalType. * Add comments for exported items. * Add task tutorial. * Update README. * Update README. * Update go.sum. * Update task tutorial.
1 parent 29ec883 commit b27e64d

File tree

1 file changed

+31
-19
lines changed

1 file changed

+31
-19
lines changed

task/README.md

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Examples as below:
1212
package main
1313

1414
import (
15+
"time"
1516
"github.com/byte-power/gorich/task"
1617
)
1718

@@ -49,6 +50,8 @@ You can also monitor the scheduled jobs via JobStats.
4950
package main
5051

5152
import (
53+
"fmt"
54+
"time"
5255
"github.com/byte-power/gorich/task"
5356
)
5457

@@ -65,13 +68,16 @@ func main() {
6568
}
6669

6770
func monitorScheduler() {
68-
// handle all job stats
69-
allJobStats := task.JobStats()
70-
for jobName, jobStats := range allJobStats {
71-
fmt.Printf("job %s stat:\n", jobName)
72-
for _, stat := range jobStats {
73-
fmt.Println(stat.ToMap())
71+
for {
72+
// handle all job stats
73+
allJobStats := task.JobStats()
74+
for jobName, jobStats := range allJobStats {
75+
fmt.Printf("job %s stat:\n", jobName)
76+
for _, stat := range jobStats {
77+
fmt.Println(stat.ToMap())
78+
}
7479
}
80+
time.Sleep(5 * time.Second)
7581
}
7682
}
7783

@@ -90,6 +96,8 @@ Notice that Coordinate use a lock that will unlock automatically 5 seconds later
9096
package main
9197

9298
import (
99+
"fmt"
100+
"time"
93101
"github.com/byte-power/gorich/task"
94102
)
95103

@@ -110,27 +118,31 @@ func main () {
110118
go scheduler1.Start()
111119
go scheduler2.Start()
112120

113-
jobStats := job1.Stats()
114-
fmt.Println("job1 stats:")
115-
for _, stat := range jobStats {
116-
fmt.Println(stat.ToMap())
117-
}
121+
go monitorJob(job1)
122+
go monitorJob(job2)
118123

119-
jobStats = job2.Stats()
120-
fmt.Println("job2 stats:")
121-
for _, stat := range jobStats {
122-
fmt.Println(stat.ToMap())
123-
}
124-
// stop schedulers after 10 seconds
125-
time.Sleep(10 * time.Second)
124+
125+
// stop schedulers after 30 seconds
126+
time.Sleep(30 * time.Second)
126127

127128
scheduler1.Stop(false)
128129
scheduler2.Stop(false)
129130
}
130131

132+
func monitorJob(job task.Job) {
133+
for {
134+
jobStats := job.Stats()
135+
fmt.Printf("job %s stats:\n", job.Name())
136+
for _, stat := range jobStats {
137+
fmt.Println(stat.ToMap())
138+
}
139+
time.Sleep(5 * time.Second)
140+
}
141+
}
142+
131143
func sum(a, b int) int {
132144
return a + b
133145
}
134146
```
135147

136-
See more examples [here](./example_test.go).
148+
See more examples [here](./example_test.go).

0 commit comments

Comments
 (0)