forked from glycerine/goq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.go
234 lines (190 loc) · 5.75 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package main
import (
"fmt"
"os"
schema "github.com/glycerine/goq/schema"
//nn "github.com/glycerine/go-nanomsg"
nn "github.com/go-mangos/mangos/compat"
)
// encapsulate the state that only NanomsgListener go routine should be touching
type NanoRecv struct {
Addr string
Nnsock *nn.Socket // recv
NanomsgRecv chan *Job
Nanoerr chan error
// set Cfg *once*, before any goroutines start, then
// treat it as immutable and never changing.
Cfg Config
Ctrl chan control
Done chan bool
Deaf bool // simulate worker failure during testing.
MonitorRecv chan bool // instrumentation for testing, possible nil
}
func NewNanoRecv(pulladdr string, cfg *Config, deaf bool) *NanoRecv {
var err error
var pullsock *nn.Socket
if !deaf {
if pulladdr != "" {
pullsock, err = MkPullNN(pulladdr, cfg, false)
if err != nil {
panic(err)
}
}
}
n := &NanoRecv{
Addr: pulladdr,
Nnsock: pullsock,
NanomsgRecv: make(chan *Job),
Nanoerr: make(chan error),
Cfg: *CopyConfig(cfg),
Ctrl: make(chan control),
Done: make(chan bool),
Deaf: deaf,
}
return n
}
// encapuslate the sending to server socket
type NanoSender struct {
ServerAddr string
ServerPushSock *nn.Socket
Ctrl chan control
Done chan bool
AckToServer chan *Job
ReconnectSrv chan string // send the receiver address for proper logging, and we'll reconnect to server.
// set Cfg *once*, before any goroutines start, then
// treat it as immutable and never changing.
Cfg Config
LastHeardRecvAddr string
MonitorSend chan bool // instrumentation for testing, possible nil
}
func NewNanoSender(cfg *Config, recvaddr string) *NanoSender {
n := &NanoSender{
Ctrl: make(chan control),
Done: make(chan bool),
AckToServer: make(chan *Job, 100),
ReconnectSrv: make(chan string),
Cfg: *CopyConfig(cfg),
LastHeardRecvAddr: recvaddr,
}
n.setServerPrivate(cfg.JservAddr())
return n
}
func (n *NanoSender) setServerPrivate(pushaddr string) {
var err error
var pushsock *nn.Socket
if pushaddr != "" {
pushsock, err = MkPushNN(pushaddr, &n.Cfg, false)
if err != nil {
panic(err)
}
n.ServerAddr = pushaddr
if n.ServerPushSock != nil {
n.ServerPushSock.Close()
}
n.ServerPushSock = pushsock
}
}
// Worker represents a process that is willing to do work
// for the server. It asks for jobs with JOBMSG_REQUESTFORWORK.
type Worker struct {
NR *NanoRecv
NS *NanoSender
Name string
Addr string // copy of same held in NR, to avoid data race.
ServerAddr string // copy of same held in NS, to avoid data race.
// test/external client monitoring, can be nil
MonitorShepJobStart chan bool
MonitorShepJobDone chan bool
ShepSaysJobStarted chan int // send pid on it, or 0 if error.
ShepSaysJobDone chan *Job // cancels come back here too
DoSingleJob chan bool
ServerReconNeeded chan string
JobFinished chan *Job // local clients can wait for a finished job here.
DoneQ []*Job
TellShepPidKilled chan int // speed up Shepard detected process kill.
ShutdownSequenceStarted chan bool
Ctrl chan control
Done chan bool
WorkOpts
// job-running state: three possible job states
// (or, how to shutdown cleanly without crashing on a race condition).
//
// a) W.RunningJob == nil, or no shepard started.
// b) w.RunningJob set to non-nil but w.Pid == 0: gotta wait for both ShepSaysJobStarted, then ShepSaysJobDone.
// c) w.RunningJob set to non-nil and w.Pid set to non-zero: gotta wait for ShepSaysJobDone.
//
// all of these are cleared by receipt on w.ShepSaysJobDone
RunningJid int64 // filled by JOBMSG_DELEGATETOWORKER handler
RunningJob *Job // filled by JOBMSG_DELEGATETOWORKER handler
Pid int // filled by receipt of w.ShepSaysJobStarted
// set Cfg *once*, before any goroutines start, then
// treat it as immutable and never changing.
Cfg Config
NoReplay *NonceRegistry
BadNonceCount int64
}
type WorkOpts struct {
Forever bool
IsDeaf bool
Monitor bool
DontStart bool // for testing shepard in isolation, shep_test.go
}
func NewWorker(pulladdr string, cfg *Config, opts *WorkOpts) (*Worker, error) {
if opts == nil {
opts = &WorkOpts{}
}
w := &Worker{
Addr: pulladdr,
ServerAddr: cfg.JservAddr(),
WorkOpts: *opts,
NR: NewNanoRecv(pulladdr, cfg, opts.IsDeaf),
NS: NewNanoSender(cfg, pulladdr),
Name: fmt.Sprintf("worker.pid.%d", os.Getpid()),
Done: make(chan bool),
Ctrl: make(chan control),
Cfg: *CopyConfig(cfg),
ShepSaysJobStarted: make(chan int),
ShepSaysJobDone: make(chan *Job),
TellShepPidKilled: make(chan int, 1),
DoSingleJob: make(chan bool),
ServerReconNeeded: make(chan string),
JobFinished: make(chan *Job),
DoneQ: make([]*Job, 0),
ShutdownSequenceStarted: make(chan bool),
NoReplay: NewNonceRegistry(NewRealTimeSource()),
}
if opts.Monitor {
w.NR.MonitorRecv = make(chan bool)
w.NS.MonitorSend = make(chan bool)
w.MonitorShepJobStart = make(chan bool)
w.MonitorShepJobDone = make(chan bool)
}
if !opts.DontStart {
w.Start()
}
return w, nil
}
func (w *Worker) StandaloneExeStart() {
pid := os.Getpid()
if len(os.Args) >= 3 {
if os.Args[2] == "forever" {
w.Forever = true
TSPrintf("---- [worker pid %d; %s] looping forever, looking for work every %d msec from server '%s'\n", pid, w.Addr, w.Cfg.SendTimeoutMsec, w.ServerAddr)
}
} else {
TSPrintf("---- [worker pid %d; %s] doing one job for server: '%s'\n", pid, w.Addr, w.ServerAddr)
}
for {
w.DoOneJob()
if !w.Forever {
break
}
}
// all done
w.Destroy()
}
func CopyJobWithMsg(job *Job, msg schema.JobMsg) *Job {
j := *job
j.Msg = msg
return &j
}