@@ -19,8 +19,8 @@ import (
1919// flagSec is a command line flag to set the timeout in seconds.
2020var flagSec = flag .Int ("ttl" , 3 , "timeout in seconds" )
2121
22- // timer runs a set of tasks on a given timeout and shuts down on os.Interrupt.
23- type timer struct {
22+ // runner runs a set of tasks on a given timeout and shuts down on os.Interrupt.
23+ type runner struct {
2424 // interrupt channel will be used to signal the runner to shut down.
2525 interrupt chan os.Signal
2626
@@ -30,58 +30,59 @@ type timer struct {
3030 // timeout will signal us after the TTL has run out.
3131 timeout <- chan time.Time
3232
33- // tasks holds a set of worker functions that run based on a given ID.
33+ // tasks holds a set of runner functions that run based on a given ID.
3434 tasks []func (int )
3535}
3636
37- // NewTimer returns a new ready-to-use timer .
38- func NewTimer (d time.Duration ) * timer {
39- t := timer {
37+ // NewRunner returns a new ready-to-use runner .
38+ func NewRunner (d time.Duration ) * runner {
39+ r := runner {
4040 interrupt : make (chan os.Signal , 1 ),
4141 complete : make (chan error ),
4242 timeout : time .After (d * time .Second ),
4343 }
4444
45- // We want to receive all interrupt based signals.
46- signal .Notify (t .interrupt , os .Interrupt )
47- return & t
45+ return & r
4846}
4947
50- // Add attaches tasks to the timer . A task is a function that takes an int ID.
51- func (t * timer ) Add (tasks ... func (int )) {
52- t .tasks = append (t .tasks , tasks ... )
48+ // Add attaches tasks to the runner . A task is a function that takes an int ID.
49+ func (r * runner ) Add (tasks ... func (int )) {
50+ r .tasks = append (r .tasks , tasks ... )
5351}
5452
5553// Run runs all tasks.
56- func (t * timer ) Start () {
54+ func (r * runner ) Start () {
55+ // We want to receive all interrupt based signals.
56+ signal .Notify (r .interrupt , os .Interrupt )
57+
5758 // Run the different tasks.
5859 go func () {
59- t .complete <- t .run (t .tasks ... )
60+ r .complete <- r .run (r .tasks ... )
6061 log .Println ("Finished work." )
6162 }()
6263
6364 for {
6465 select {
6566 // Signaled when the tasks are complete.
66- case err := <- t .complete :
67+ case err := <- r .complete :
6768 if err != nil {
6869 log .Printf ("Exiting with error: %s" , err )
6970 }
7071 return
7172
7273 // Signaled when we run out of time.
73- case <- t .timeout :
74+ case <- r .timeout :
7475 log .Println ("Timeout - Killing Program" )
7576 os .Exit (1 )
7677 }
7778 }
7879}
7980
8081// run executes each registered task.
81- func (t * timer ) run (tasks ... func (int )) error {
82+ func (r * runner ) run (tasks ... func (int )) error {
8283 for id , task := range tasks {
8384 // Check for an interrupt signal for the OS.
84- if t .gotInterrupt () {
85+ if r .gotInterrupt () {
8586 return errors .New ("Early Shutdown" )
8687 }
8788
@@ -93,12 +94,12 @@ func (t *timer) run(tasks ...func(int)) error {
9394}
9495
9596// gotInterrupt verifies if the interrupt signal has been issued.
96- func (t * timer ) gotInterrupt () bool {
97+ func (r * runner ) gotInterrupt () bool {
9798 select {
9899 // Signaled when an interrupt event is signaled.
99- case <- t .interrupt :
100+ case <- r .interrupt :
100101 // Stop receiving any further signals.
101- signal .Stop (t .interrupt )
102+ signal .Stop (r .interrupt )
102103 log .Println ("Received interrupt." )
103104 return true
104105
@@ -115,11 +116,11 @@ func main() {
115116 log .Println ("Starting work." )
116117
117118 // Create a new timer value for this run.
118- timer := NewTimer (time .Duration (* flagSec ))
119+ r := NewRunner (time .Duration (* flagSec ))
119120
120121 // Add the tasks to be run and start running.
121- timer .Add (sleeper (1 ), sleeper (2 ), sleeper (1 ))
122- timer .Start ()
122+ r .Add (sleeper (1 ), sleeper (2 ), sleeper (1 ))
123+ r .Start ()
123124
124125 log .Println ("Process Ended" )
125126}
0 commit comments