Skip to content

Commit d7ee01b

Browse files
Code changes based on reviews.
1 parent 320da7e commit d7ee01b

2 files changed

Lines changed: 23 additions & 16 deletions

File tree

‎chapter7/patterns/runner/main/main.go‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,24 @@ func main() {
2626

2727
// Add the tasks to be run and start running.
2828
r.Add(createTask(1), createTask(2), createTask(1))
29-
r.Start()
29+
err := r.Start()
30+
if err != nil {
31+
switch err {
32+
case runner.ErrTimeout:
33+
log.Println("Terminating Due To Timeout.")
34+
case runner.ErrInterrupt:
35+
log.Println("Terminating Due To Interrupt.")
36+
}
37+
}
3038

3139
log.Println("Process Ended")
3240
}
3341

34-
// createTask returns an example task that sleeps for the given
35-
// duration (in seconds).
36-
func createTask(d time.Duration) func(int) {
42+
// createTask returns an example task that sleeps for the specified
43+
// number of seconds.
44+
func createTask(seconds int) func(int) {
3745
return func(id int) {
3846
log.Printf("Processor - Task #%d", id)
39-
time.Sleep(d * time.Second)
47+
time.Sleep(time.Duration(seconds) * time.Second)
4048
}
4149
}

‎chapter7/patterns/runner/runner.go‎

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package runner
66

77
import (
88
"errors"
9-
"log"
109
"os"
1110
"os/signal"
1211
"time"
@@ -30,6 +29,12 @@ type Runner struct {
3029
tasks []func(int)
3130
}
3231

32+
// ErrTimeout is returned when a value is received on the timeout channel.
33+
var ErrTimeout = errors.New("Received timeout.")
34+
35+
// ErrInterrupt is returned when an event from the OS is received.
36+
var ErrInterrupt = errors.New("Received interrupt.")
37+
3338
// New returns a new ready-to-use Runner.
3439
func New(d time.Duration) *Runner {
3540
return &Runner{
@@ -46,28 +51,23 @@ func (r *Runner) Add(tasks ...func(int)) {
4651
}
4752

4853
// Start runs all tasks and monitors channel events.
49-
func (r *Runner) Start() {
54+
func (r *Runner) Start() error {
5055
// We want to receive all interrupt based signals.
5156
signal.Notify(r.interrupt, os.Interrupt)
5257

5358
// Run the different tasks on a different goroutine.
5459
go func() {
5560
r.complete <- r.run()
56-
log.Println("Finished work.")
5761
}()
5862

5963
select {
6064
// Signaled when processing is done.
6165
case err := <-r.complete:
62-
if err != nil {
63-
log.Printf("Exiting with error: %s", err)
64-
}
65-
return
66+
return err
6667

6768
// Signaled when we run out of time.
6869
case <-r.timeout:
69-
log.Println("Timeout - Killing Program")
70-
os.Exit(1)
70+
return ErrTimeout
7171
}
7272
}
7373

@@ -76,7 +76,7 @@ func (r *Runner) run() error {
7676
for id, task := range r.tasks {
7777
// Check for an interrupt signal from the OS.
7878
if r.gotInterrupt() {
79-
return errors.New("Interrupt - Shutdown Program")
79+
return ErrInterrupt
8080
}
8181

8282
// Execute the registered task.
@@ -93,7 +93,6 @@ func (r *Runner) gotInterrupt() bool {
9393
case <-r.interrupt:
9494
// Stop receiving any further signals.
9595
signal.Stop(r.interrupt)
96-
log.Println("Received interrupt.")
9796
return true
9897

9998
// Continue running as normal.

0 commit comments

Comments
 (0)