33// This example is provided with help by Gabriel Aszalos.
44
55// Package runner manages the running and lifetime of
6- // the process.
6+ // a process.
77package runner
88
99import (
@@ -14,18 +14,21 @@ import (
1414 "time"
1515)
1616
17- // runner runs a set of tasks on a given timeout and shuts down on os.Interrupt.
17+ // runner runs a set of tasks within a given timeout and can be
18+ // shut down on an operating system interrupt.
1819type runner struct {
19- // interrupt channel will be used to signal the runner to shut down.
20+ // interrupt channel reports a signal from the
21+ // operating system.
2022 interrupt chan os.Signal
2123
22- // complete channel will receive the outcome of the timer .
24+ // complete channel reports that processing is done .
2325 complete chan error
2426
25- // timeout will signal us after the TTL has run out.
27+ // timeout reports that time has run out.
2628 timeout <- chan time.Time
2729
28- // tasks holds a set of runner functions that run based on a given ID.
30+ // tasks holds a set of functions that are executed
31+ // synchronously in index order.
2932 tasks []func (int )
3033}
3134
@@ -38,25 +41,26 @@ func New(d time.Duration) *runner {
3841 }
3942}
4043
41- // Add attaches tasks to the runner. A task is a function that takes an int ID.
44+ // Add attaches tasks to the runner. A task is a function that
45+ // takes an int ID.
4246func (r * runner ) Add (tasks ... func (int )) {
4347 r .tasks = append (r .tasks , tasks ... )
4448}
4549
46- // Run runs all tasks.
50+ // Start runs all tasks and monitors channel events .
4751func (r * runner ) Start () {
4852 // We want to receive all interrupt based signals.
4953 signal .Notify (r .interrupt , os .Interrupt )
5054
51- // Run the different tasks.
55+ // Run the different tasks on a different goroutine .
5256 go func () {
5357 r .complete <- r .run (r .tasks ... )
5458 log .Println ("Finished work." )
5559 }()
5660
5761 for {
5862 select {
59- // Signaled when the tasks are complete .
63+ // Signaled when processing is done .
6064 case err := <- r .complete :
6165 if err != nil {
6266 log .Printf ("Exiting with error: %s" , err )
@@ -74,7 +78,7 @@ func (r *runner) Start() {
7478// run executes each registered task.
7579func (r * runner ) run (tasks ... func (int )) error {
7680 for id , task := range tasks {
77- // Check for an interrupt signal for the OS.
81+ // Check for an interrupt signal from the OS.
7882 if r .gotInterrupt () {
7983 return errors .New ("Early Shutdown" )
8084 }
@@ -89,7 +93,7 @@ func (r *runner) run(tasks ...func(int)) error {
8993// gotInterrupt verifies if the interrupt signal has been issued.
9094func (r * runner ) gotInterrupt () bool {
9195 select {
92- // Signaled when an interrupt event is signaled .
96+ // Signaled when an interrupt event is sent .
9397 case <- r .interrupt :
9498 // Stop receiving any further signals.
9599 signal .Stop (r .interrupt )
0 commit comments