forked from kdar/goqless
-
Notifications
You must be signed in to change notification settings - Fork 3
/
worker.go
139 lines (119 loc) · 2.93 KB
/
worker.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// This worker does not model the way qless does it.
// I more or less modeled it after my own needs.
package goqless
import (
// "encoding/json"
"fmt"
// "github.com/garyburd/redigo/redis"
"log"
"errors"
"reflect"
"strconv"
"time"
"sync"
)
type JobFunc func(*Job) error
type JobCallback func(*Job) error
type Worker struct {
Interval int // in time.Duration
funcs map[string]JobFunc
queue *Queue
// events *Events
cli *Client
}
func NewWorker(cli *Client, queueName string, interval int) *Worker {
w := &Worker{
Interval: interval,
funcs: make(map[string]JobFunc),
// events: c.Events(),
cli: cli,
}
w.queue = cli.Queue(queueName)
return w
}
func haertbeatStart(job *Job, done chan bool, heartbeat int, clientLock sync.Mutex) {
tick := time.Tick(time.Duration(heartbeat-5) * time.Duration(time.Second))
for {
select {
case <-done:
return
case <-tick:
clientLock.Lock()
job.Heartbeat()
clientLock.Unlock()
log.Printf("heartbeat***%v,cli:%+v", job.Jid, job.cli)
}
}
}
func (w *Worker) Start() error {
// log.Println("worker Start")
var clientLock sync.Mutex
func(q *Queue) {
heartbeatStr, err := w.cli.GetConfig("heartbeat")
heartbeat, err := strconv.Atoi(heartbeatStr)
//log.Println("heartbeatStr:", heartbeat)
if err != nil {
heartbeat = 60
}
for {
clientLock.Lock()
jobs, err := q.Pop(1)
clientLock.Unlock()
if err != nil {
log.Println(err)
// report to some error channel?
} else {
if len(jobs) > 0 {
done := make(chan bool)
go haertbeatStart(jobs[0], done, heartbeat, clientLock)
err := w.funcs[jobs[0].Klass](jobs[0])
if err != nil {
// TODO: probably do something with this
clientLock.Lock()
jobs[0].Fail("fail", err.Error())
clientLock.Unlock()
done <- false
} else {
clientLock.Lock()
jobs[0].Complete()
clientLock.Unlock()
done <- true
//log.Printf("===job:%+v", jobs[0])
}
} else {
time.Sleep(time.Duration(w.Interval))
}
}
}
}(w.queue)
return nil
}
func (w *Worker) AddFunc(name string, f JobFunc) error {
if _, ok := w.funcs[name]; ok {
return fmt.Errorf("function \"%s\" already exists", name)
}
w.funcs[name] = f
return nil
}
// Adds all the methods in the passed interface as job functions.
// Job names are in the form of: name.methodname
func (w *Worker) AddService(name string, rcvr interface{}) error {
typ := reflect.TypeOf(rcvr)
val := reflect.ValueOf(rcvr)
for i := 0; i < typ.NumMethod(); i++ {
method := typ.Method(i)
w.AddFunc(name+"."+method.Name, func(job *Job) error {
ret := method.Func.Call([]reflect.Value{val, reflect.ValueOf(job)})
if len(ret) > 0 {
if err, ok := ret[0].Interface().(error); ok {
return err
}
} else {
errStr := "reflect len less than zero." + strconv.Itoa(len(ret))
return errors.New(errStr)
}
return nil
})
}
return nil
}