2015-03-04 11:43:44 +00:00
|
|
|
package job
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
worker struct {
|
2015-03-04 19:08:36 +00:00
|
|
|
id string
|
|
|
|
job *Job
|
|
|
|
shutdown <-chan struct{}
|
2015-03-04 11:43:44 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func (w *worker) workHard() {
|
|
|
|
defer w.wg.Done()
|
|
|
|
for {
|
|
|
|
select {
|
2015-03-04 19:08:36 +00:00
|
|
|
case <-w.shutdown:
|
|
|
|
return
|
|
|
|
case t := <-w.job.tasks:
|
|
|
|
w.perform(t)
|
2015-03-04 11:43:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-04 19:08:36 +00:00
|
|
|
func (w *worker) perform(t Task) {
|
2015-03-04 11:43:44 +00:00
|
|
|
start := time.Now()
|
|
|
|
defer func() {
|
|
|
|
err := recover()
|
2015-03-04 19:08:36 +00:00
|
|
|
t.report(report{
|
2015-03-04 11:43:44 +00:00
|
|
|
duration: time.Since(start),
|
2015-03-04 19:08:36 +00:00
|
|
|
success: err,
|
|
|
|
})
|
2015-03-04 11:43:44 +00:00
|
|
|
}()
|
|
|
|
|
2015-03-04 19:08:36 +00:00
|
|
|
w.job.actor(t)
|
2015-03-04 11:43:44 +00:00
|
|
|
}
|