Skip to content

Commit dc6be6e

Browse files
committed
Moved worker ID to worker context
This lets dispatch handlers get to the ID while keeping the model somewhat lean (consolidating the root-level id into context)
1 parent 04b8156 commit dc6be6e

File tree

2 files changed

+16
-16
lines changed

2 files changed

+16
-16
lines changed

dispatcher.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ func (d *Dispatcher) GetUtilization() Utilization {
142142
for _, v := range d.workers {
143143
results = append(results, WorkerUtilization{
144144
PercentUtilization: v.GetPercentUtilization(),
145-
Id: v.id,
145+
Id: v.workerContext.Id,
146146
})
147147
}
148148

worker.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,24 @@ type Job struct {
1313
// provides a mechanism for shared data and context across calls to the work function
1414
type Context struct {
1515
Data interface{}
16+
Id int
1617
}
1718

1819
func NewWorker(id int, workerPool chan chan Job, workFn WorkFunction, jobErrorFn JobErrorFunction, logFn LogFunction) Worker {
1920
return Worker{
20-
id: id,
21-
jobQueue: make(chan Job),
22-
workerPool: workerPool,
23-
quitChan: make(chan bool),
24-
workFn: workFn,
25-
jobErrorFn: jobErrorFn,
26-
logFn: logFn,
27-
workerContext: Context{},
21+
jobQueue: make(chan Job),
22+
workerPool: workerPool,
23+
quitChan: make(chan bool),
24+
workFn: workFn,
25+
jobErrorFn: jobErrorFn,
26+
logFn: logFn,
27+
workerContext: Context{
28+
Id: id,
29+
},
2830
}
2931
}
3032

3133
type Worker struct {
32-
id int
3334
jobQueue chan Job
3435
workerPool chan chan Job
3536
quitChan chan bool
@@ -80,22 +81,22 @@ func (w *Worker) start() {
8081
case job := <-w.jobQueue:
8182
workFnStart := time.Now()
8283
atomic.AddInt32(&w.runningCount, 1)
83-
_, _ = w.log("worker%d: started %s\n", w.id, job.Name)
84+
_, _ = w.log("worker%d: started %s\n", w.workerContext.Id, job.Name)
8485
err := w.workFn(job, &w.workerContext)
8586
atomic.AddInt32(&w.runningCount, -1)
8687
atomic.AddInt64(&w.totalProcessingTimeNs, time.Now().Sub(workFnStart).Nanoseconds())
8788

8889
if err != nil {
89-
_, _ = w.log("worker%d: had error in %s: %s!\n", w.id, job.Name, err.Error())
90+
_, _ = w.log("worker%d: had error in %s: %s!\n", w.workerContext.Id, job.Name, err.Error())
9091
w.error(job, &w.workerContext, err)
9192
}
9293

9394
// nil out data to clue GC
9495
job.Context = nil
9596

96-
_, _ = w.log("worker%d: completed %s!\n", w.id, job.Name)
97+
_, _ = w.log("worker%d: completed %s!\n", w.workerContext.Id, job.Name)
9798
case <-w.quitChan:
98-
_, _ = w.log("worker%d stopping\n", w.id)
99+
_, _ = w.log("worker%d stopping\n", w.workerContext.Id)
99100
return
100101
}
101102
}
@@ -108,7 +109,6 @@ func (w Worker) stop() {
108109
}()
109110
}
110111

111-
112112
func (w Worker) log(format string, a ...interface{}) (n int, err error) {
113113
if w.logFn != nil {
114114
return w.logFn(format, a)
@@ -121,4 +121,4 @@ func (w Worker) error(job Job, workerContext *Context, err error) {
121121
if w.jobErrorFn != nil {
122122
w.jobErrorFn(job, workerContext, err)
123123
}
124-
}
124+
}

0 commit comments

Comments
 (0)