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
|
package roadrunner
import (
"context"
"fmt"
"time"
"github.com/pkg/errors"
"github.com/spiral/goridge/v2"
)
var EmptyPayload = Payload{}
type SyncWorker interface {
// WorkerBase provides basic functionality for the SyncWorker
WorkerBase
// Exec used to execute payload on the SyncWorker, there is no TIMEOUTS
Exec(rqs Payload) (Payload, error)
// ExecWithContext allow to set ExecTTL
ExecWithContext(ctx context.Context, rqs Payload) (Payload, error)
}
type taskWorker struct {
w WorkerBase
}
func NewSyncWorker(w WorkerBase) (SyncWorker, error) {
return &taskWorker{
w: w,
}, nil
}
type twexec struct {
payload Payload
err error
}
func (tw *taskWorker) ExecWithContext(ctx context.Context, rqs Payload) (Payload, error) {
c := make(chan twexec)
go func() {
if len(rqs.Body) == 0 && len(rqs.Context) == 0 {
c <- twexec{
payload: EmptyPayload,
err: fmt.Errorf("payload can not be empty"),
}
return
}
if tw.w.State().Value() != StateReady {
c <- twexec{
payload: EmptyPayload,
err: fmt.Errorf("WorkerProcess is not ready (%s)", tw.w.State().String()),
}
return
}
// set last used time
tw.w.State().SetLastUsed(uint64(time.Now().UnixNano()))
tw.w.State().Set(StateWorking)
rsp, err := tw.execPayload(rqs)
if err != nil {
if _, ok := err.(TaskError); !ok {
tw.w.State().Set(StateErrored)
tw.w.State().RegisterExec()
}
c <- twexec{
payload: EmptyPayload,
err: err,
}
return
}
tw.w.State().Set(StateReady)
tw.w.State().RegisterExec()
c <- twexec{
payload: rsp,
err: nil,
}
return
}()
for {
select {
case <-ctx.Done():
return EmptyPayload, ctx.Err()
case res := <-c:
if res.err != nil {
return EmptyPayload, res.err
}
return res.payload, nil
}
}
}
//
func (tw *taskWorker) Exec(rqs Payload) (Payload, error) {
if len(rqs.Body) == 0 && len(rqs.Context) == 0 {
return EmptyPayload, fmt.Errorf("payload can not be empty")
}
if tw.w.State().Value() != StateReady {
return EmptyPayload, fmt.Errorf("WorkerProcess is not ready (%s)", tw.w.State().String())
}
// set last used time
tw.w.State().SetLastUsed(uint64(time.Now().UnixNano()))
tw.w.State().Set(StateWorking)
rsp, err := tw.execPayload(rqs)
if err != nil {
if _, ok := err.(TaskError); !ok {
tw.w.State().Set(StateErrored)
tw.w.State().RegisterExec()
}
return EmptyPayload, err
}
tw.w.State().Set(StateReady)
tw.w.State().RegisterExec()
return rsp, nil
}
func (tw *taskWorker) execPayload(rqs Payload) (Payload, error) {
// two things; todo: merge
if err := sendControl(tw.w.Relay(), rqs.Context); err != nil {
return EmptyPayload, errors.Wrap(err, "header error")
}
if err := tw.w.Relay().Send(rqs.Body, 0); err != nil {
return EmptyPayload, errors.Wrap(err, "sender error")
}
var pr goridge.Prefix
rsp := Payload{}
var err error
if rsp.Context, pr, err = tw.w.Relay().Receive(); err != nil {
return EmptyPayload, errors.Wrap(err, "WorkerProcess error")
}
if !pr.HasFlag(goridge.PayloadControl) {
return EmptyPayload, fmt.Errorf("malformed WorkerProcess response")
}
if pr.HasFlag(goridge.PayloadError) {
return EmptyPayload, TaskError(rsp.Context)
}
// add streaming support :)
if rsp.Body, pr, err = tw.w.Relay().Receive(); err != nil {
return EmptyPayload, errors.Wrap(err, "WorkerProcess error")
}
return rsp, nil
}
func (tw *taskWorker) String() string {
return tw.w.String()
}
func (tw *taskWorker) Created() time.Time {
return tw.w.Created()
}
func (tw *taskWorker) Events() <-chan WorkerEvent {
return tw.w.Events()
}
func (tw *taskWorker) Pid() int64 {
return tw.w.Pid()
}
func (tw *taskWorker) State() State {
return tw.w.State()
}
func (tw *taskWorker) Start() error {
return tw.w.Start()
}
func (tw *taskWorker) Wait(ctx context.Context) error {
return tw.w.Wait(ctx)
}
func (tw *taskWorker) Stop(ctx context.Context) error {
return tw.w.Stop(ctx)
}
func (tw *taskWorker) Kill(ctx context.Context) error {
return tw.w.Kill(ctx)
}
func (tw *taskWorker) Relay() goridge.Relay {
return tw.w.Relay()
}
func (tw *taskWorker) AttachRelay(rl goridge.Relay) {
tw.w.AttachRelay(rl)
}
|