@@ -45,7 +45,13 @@ ControlLoop:
4545 case <- sigChan :
4646 // Interrupt event signaled by the operation system.
4747 log .Println ("OS INTERRUPT - Shutting Down Early" )
48+
49+ // Close the channel to signal to the processor
50+ // it needs to shutdown.
4851 close (shutdown )
52+
53+ // Set the channel to nil so we no longer process
54+ // any more of these events.
4955 sigChan = nil
5056
5157 case <- timeout :
@@ -62,29 +68,29 @@ ControlLoop:
6268
6369 // Program finished.
6470 log .Println ("Process Ended" )
65- return
6671}
6772
6873// processor provides the main program logic for the program.
6974func processor (complete chan <- error ) {
7075 log .Println ("Processor - Starting" )
7176
72- // Message returned through the complete channel.
77+ // Variable to store any error that occurs.
78+ // Passed into the defer function via closures.
7379 var err error
7480
75- // Schedule this anonymous function to be executed when
76- // the function returns .
81+ // Defer the send on the channel so it happens
82+ // regardless of how this function terminates .
7783 defer func () {
7884 log .Println ("Processor - Completed" )
7985
80- // Signal the goroutine is shutdown.
86+ // Signal the goroutine we have shutdown.
8187 complete <- err
8288 }()
8389
8490 // Simulate some iterative work.
8591 for work := 0 ; work < 5 ; work ++ {
86- log . Println ( "Processor - Doing Work" )
87- time . Sleep ( 1 * time . Second )
92+ // Perform some work.
93+ err = doWork ( )
8894
8995 select {
9096 case <- shutdown :
@@ -98,3 +104,11 @@ func processor(complete chan<- error) {
98104 }
99105 }
100106}
107+
108+ // doWork simulates a function we call to get our work done.
109+ func doWork () error {
110+ log .Println ("Processor - Doing Work" )
111+ time .Sleep (1 * time .Second )
112+
113+ return nil
114+ }
0 commit comments