1
0
Fork 0
empact/job/worker.go

46 lines
619 B
Go
Raw Normal View History

2015-03-04 11:43:44 +00:00
package job
import (
"time"
2015-03-04 20:51:17 +00:00
"code.google.com/p/go-uuid/uuid"
"github.com/localhots/steward/db"
2015-03-04 11:43:44 +00:00
)
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 20:51:17 +00:00
func (w *worker) perform(t *db.Task) {
t.Worker = w.id
t.StartedAt = time.Now()
2015-03-04 11:43:44 +00:00
defer func() {
err := recover()
2015-03-04 20:51:17 +00:00
t.Duration = time.Since(t.StartedAt).Nanoseconds()
t.Error = err.String()
t.Save()
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
}
2015-03-04 20:51:17 +00:00
func newID() string {
return uuid.New()
}