/
worker.go
200 lines (175 loc) · 5.26 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
// Copyright (c) TFG Co. All Rights Reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package worker
import (
"context"
"encoding/json"
"os"
"github.com/BaalDev/pitaya/v2/config"
"github.com/BaalDev/pitaya/v2/constants"
"github.com/BaalDev/pitaya/v2/logger"
"github.com/BaalDev/pitaya/v2/logger/interfaces"
"github.com/golang/protobuf/proto"
workers "github.com/topfreegames/go-workers"
)
// Worker executes RPCs with retry and backoff time
type Worker struct {
concurrency int
registered bool
opts *config.EnqueueOpts
started bool
}
// NewWorker configures and returns a *Worker
func NewWorker(config config.WorkerConfig, opts config.EnqueueOpts) (*Worker, error) {
hostname, err := os.Hostname()
if err != nil {
return nil, err
}
workers.Configure(map[string]string{
"server": config.Redis.ServerURL,
"pool": config.Redis.Pool,
"password": config.Redis.Password,
"namespace": config.Namespace,
"process": hostname,
})
return &Worker{
concurrency: config.Concurrency,
opts: &opts,
}, nil
}
// SetLogger overwrites worker logger
func (w *Worker) SetLogger(logger interfaces.Logger) {
workers.Logger = logger
}
// Start starts worker in another gorotine
func (w *Worker) Start() {
go workers.Start()
w.started = true
}
// Started returns true if worker was started
func (w *Worker) Started() bool {
return w != nil && w.started
}
// EnqueueRPC enqueues rpc job to worker
func (w *Worker) EnqueueRPC(
routeStr string,
metadata map[string]interface{},
reply, arg proto.Message,
) (jid string, err error) {
opts := w.enqueueOptions(w.opts)
return workers.EnqueueWithOptions(rpcQueue, class, &rpcInfo{
Route: routeStr,
Metadata: metadata,
Arg: arg,
Reply: reply,
}, opts)
}
// EnqueueRPCWithOptions enqueues rpc job to worker
func (w *Worker) EnqueueRPCWithOptions(
routeStr string,
metadata map[string]interface{},
reply, arg proto.Message,
opts *config.EnqueueOpts,
) (jid string, err error) {
return workers.EnqueueWithOptions(rpcQueue, class, &rpcInfo{
Route: routeStr,
Metadata: metadata,
Arg: arg,
Reply: reply,
}, w.enqueueOptions(opts))
}
// RegisterRPCJob registers a RPC job
func (w *Worker) RegisterRPCJob(rpcJob RPCJob) error {
if w.registered {
return constants.ErrRPCJobAlreadyRegistered
}
job := w.parsedRPCJob(rpcJob)
workers.Process(rpcQueue, job, w.concurrency)
w.registered = true
return nil
}
func (w *Worker) parsedRPCJob(rpcJob RPCJob) func(*workers.Msg) {
return func(jobArg *workers.Msg) {
logger.Log.Debug("executing rpc job")
bts, rpcRoute, err := w.unmarshalRouteMetadata(jobArg)
if err != nil {
logger.Log.Errorf("failed to get job arg: %q", err)
panic(err)
}
logger.Log.Debug("getting route arg and reply")
arg, reply, err := rpcJob.GetArgReply(rpcRoute.Route)
if err != nil {
logger.Log.Errorf("failed to get methods arg and reply: %q", err)
panic(err)
}
rpcInfo := &rpcInfo{
Arg: arg,
Reply: reply,
}
logger.Log.Debug("unmarshalling rpc info")
err = json.Unmarshal(bts, rpcInfo)
if err != nil {
logger.Log.Errorf("failed to unmarshal rpc info: %q", err)
panic(err)
}
logger.Log.Debug("choosing server to make rpc")
serverID, err := rpcJob.ServerDiscovery(rpcInfo.Route, rpcInfo.Metadata)
if err != nil {
logger.Log.Errorf("failed get server: %q", err)
panic(err)
}
ctx := context.Background()
logger.Log.Debugf("executing rpc func to %s", rpcInfo.Route)
err = rpcJob.RPC(ctx, serverID, rpcInfo.Route, reply, arg)
if err != nil {
logger.Log.Errorf("failed make rpc: %q", err)
panic(err)
}
logger.Log.Debug("finished executing rpc job")
}
}
func (w *Worker) enqueueOptions(
opts *config.EnqueueOpts,
) workers.EnqueueOptions {
return workers.EnqueueOptions{
Retry: opts.Enabled,
RetryMax: opts.Max,
RetryOptions: workers.RetryOptions{
Exp: opts.Exponential,
MinDelay: opts.MinDelay,
MaxDelay: opts.MaxDelay,
MaxRand: opts.MaxRandom,
},
}
}
func (w *Worker) unmarshalRouteMetadata(
jobArg *workers.Msg,
) ([]byte, *rpcRoute, error) {
bts, err := jobArg.Args().MarshalJSON()
if err != nil {
return nil, nil, err
}
rpcRoute := new(rpcRoute)
err = json.Unmarshal(bts, rpcRoute)
if err != nil {
return nil, nil, err
}
return bts, rpcRoute, nil
}