@@ -9,7 +9,8 @@ type Job struct {
99 Context interface {}
1010}
1111
12- // provides a mechanism for shared data and context across calls to the work function
12+ // Context provides a mechanism for shared data and context across calls to the work function, as well as to provide
13+ // the id of the worker for tracking/logging purposes to the job functions
1314type Context struct {
1415 Data interface {}
1516 Id int
@@ -42,6 +43,7 @@ type Worker struct {
4243 workerContext Context
4344}
4445
46+ // GetRunningCount returns the total number of running workers this worker should count as (either 1 or 0)
4547func (w Worker ) GetRunningCount () int32 {
4648 return w .runningCount
4749}
@@ -51,17 +53,17 @@ func (w Worker) GetRunTimeNs() int64 {
5153 return time .Now ().Sub (w .startTime ).Nanoseconds ()
5254}
5355
54- // how long the worker spent doing things across all runs
56+ // GetTotalActiveTimeNs returns how long the worker spent doing things across all runs
5557func (w Worker ) GetTotalActiveTimeNs () int64 {
5658 return w .totalProcessingTimeNs
5759}
5860
59- // how long the worker spent doing nothing across all runs
61+ // GetTotalIdleTimeNs returns how long the worker spent doing nothing across all runs
6062func (w Worker ) GetTotalIdleTimeNs () int64 {
6163 return time .Now ().Sub (w .startTime ).Nanoseconds () - w .GetTotalActiveTimeNs ()
6264}
6365
64- // how much of the time the worker spent doing things across all runs, by %
66+ // GetPercentUtilization returns how much of the time the worker spent doing things across all runs, by %
6567func (w Worker ) GetPercentUtilization () float32 {
6668 if w .GetRunTimeNs () == 0 {
6769 return 0.0
@@ -80,22 +82,22 @@ func (w *Worker) start() {
8082 case job := <- w .jobQueue :
8183 workFnStart := time .Now ()
8284 atomic .AddInt32 (& w .runningCount , 1 )
83- _ , _ = w .log ("worker%d: started job" , w .workerContext .Id )
85+ w .log ("worker%d: started job" , w .workerContext .Id )
8486 err := w .workFn (job , & w .workerContext )
8587 atomic .AddInt32 (& w .runningCount , - 1 )
8688 atomic .AddInt64 (& w .totalProcessingTimeNs , time .Now ().Sub (workFnStart ).Nanoseconds ())
8789
8890 if err != nil {
89- _ , _ = w .log ("worker%d: had error: %s" , w .workerContext .Id , err .Error ())
91+ w .log ("worker%d: had error: %s" , w .workerContext .Id , err .Error ())
9092 w .error (job , & w .workerContext , err )
9193 }
9294
9395 // nil out data to clue GC
9496 job .Context = nil
9597
96- _ , _ = w .log ("worker%d: completed job" , w .workerContext .Id )
98+ w .log ("worker%d: completed job" , w .workerContext .Id )
9799 case <- w .quitChan :
98- _ , _ = w .log ("worker%d stopping" , w .workerContext .Id )
100+ w .log ("worker%d stopping" , w .workerContext .Id )
99101 return
100102 }
101103 }
@@ -108,12 +110,10 @@ func (w Worker) stop() {
108110 }()
109111}
110112
111- func (w Worker ) log (format string , a ... interface {}) ( n int , err error ) {
113+ func (w Worker ) log (format string , a ... interface {}) {
112114 if w .logFn != nil {
113- return w .logFn (format , a ... )
115+ _ , _ = w .logFn (format , a ... )
114116 }
115-
116- return 0 , nil
117117}
118118
119119func (w Worker ) error (job Job , workerContext * Context , err error ) {
0 commit comments