forked from mission-liao/dingo
/
example_test.go
420 lines (371 loc) · 8.58 KB
/
example_test.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
package dingo_test
import (
"encoding/json"
"fmt"
"os"
"time"
"github.com/mission-liao/dingo"
"github.com/mission-liao/dingo/amqp"
)
func ExampleApp_local() {
// this example demonstrates a job queue runs in background
var err error
defer func() {
if err != nil {
fmt.Printf("%v\n", err)
}
}()
// a App in local mode
app, err := dingo.NewApp("local", nil)
if err != nil {
return
}
// register a worker function murmur
err = app.Register("murmur", func(msg string, repeat int, interval time.Duration) {
for ; repeat > 0; repeat-- {
select {
case <-time.After(interval):
fmt.Printf("%v\n", msg)
}
}
})
if err != nil {
return
}
// allocate 5 workers, sharing 1 report channel
_, err = app.Allocate("murmur", 5, 1)
if err != nil {
return
}
results := []*dingo.Result{}
// invoke 10 tasks
for i := 0; i < 10; i++ {
results = append(
results,
dingo.NewResult(
// name, option, parameter#1, parameter#2, parameter#3...
app.Call("murmur", nil, fmt.Sprintf("this is %d speaking", i), 10, 100*time.Millisecond),
))
}
// wait until those tasks are done
for _, v := range results {
err = v.Wait(0)
if err != nil {
return
}
}
// release resource
err = app.Close()
if err != nil {
return
}
}
func ExampleApp_Use_caller() {
/*
import (
"fmt"
"time"
"github.com/mission-liao/dingo"
"github.com/mission-liao/dingo/amqp"
)
*/
// this example demostrate a caller based on AMQP, used along with ExampleApp_Use_worker
// make sure you install a rabbitmq server locally.
var err error
defer func() {
if err != nil {
fmt.Printf("%v\n", err)
}
}()
// an App in remote mode
app, err := dingo.NewApp("remote", nil)
if err != nil {
return
}
// attach an AMQP producer to publish your tasks
broker, err := dgamqp.NewBroker(dgamqp.DefaultAmqpConfig())
if err != nil {
return
}
_, _, err = app.Use(broker, dingo.ObjT.Producer)
if err != nil {
return
}
// attach an AMQP store to receive reports from datastores.
backend, err := dgamqp.NewBackend(dgamqp.DefaultAmqpConfig())
if err != nil {
return
}
_, _, err = app.Use(backend, dingo.ObjT.Store)
if err != nil {
return
}
// register a work function that murmur
err = app.Register("murmur", func(speech *struct {
Prologue string
Script []string
}, interval time.Duration) (countOfSentence int) {
// speak the prologue
fmt.Printf("%v:\n", speech.Prologue)
countOfSentence++
// speak the script
for _, v := range speech.Script {
<-time.After(interval)
fmt.Printf("%v\n", v)
countOfSentence++
}
// return the total sentence we talked
return
})
if err != nil {
return
}
// compose a script to talk
script := &struct {
Prologue string
Script []string
}{
Script: []string{
"Today, I'm announcing this library.",
"It should be easy to use, ",
"and fun to play with.",
"Merry X'mas.",
},
}
// invoke 20 tasks
results := []*dingo.Result{}
for i := 0; i < 20; i++ {
script.Prologue = fmt.Sprintf("this is %d speaking", i)
results = append(results,
dingo.NewResult(
// name, option, parameter#1, parameter#2 ...
app.Call("murmur", nil, script, 100*time.Millisecond),
))
}
// wait until those tasks are done
for _, v := range results {
err = v.Wait(0)
if err != nil {
return
}
// result is accessible
fmt.Printf("one worker spoke %v sentences\n", v.Last.Return()[0].(int))
}
// release resource
err = app.Close()
if err != nil {
return
}
}
func ExampleApp_Use_worker() {
/*
import (
"fmt"
"os"
"time"
"github.com/mission-liao/dingo"
"github.com/mission-liao/dingo/amqp"
)
*/
// this example demonstrate a worker based on AMQP, used along with ExampleApp_Use_caller
// make sure you install a rabbitmq server locally.
var err error
defer func() {
if err != nil {
fmt.Printf("%v\n", err)
}
}()
// an App in remote mode
app, err := dingo.NewApp("remote", nil)
if err != nil {
return
}
// attach an AMQP consumer to receive tasks
broker, err := dgamqp.NewBroker(dgamqp.DefaultAmqpConfig())
if err != nil {
return
}
_, _, err = app.Use(broker, dingo.ObjT.NamedConsumer)
if err != nil {
return
}
// attach an AMQP reporter to publish reports
backend, err := dgamqp.NewBackend(dgamqp.DefaultAmqpConfig())
if err != nil {
return
}
_, _, err = app.Use(backend, dingo.ObjT.Reporter)
if err != nil {
return
}
// register a work function that murmur
err = app.Register("murmur", func(speech *struct {
Prologue string
Script []string
}, interval time.Duration) (countOfSentence int) {
// speak the prologue
fmt.Printf("%v:\n", speech.Prologue)
countOfSentence++
// speak the script
for _, v := range speech.Script {
<-time.After(interval)
fmt.Printf("%v\n", v)
countOfSentence++
}
// return the total sentence we talked
return
})
if err != nil {
return
}
// allocate 1 workers, sharing 1 report channel
_, err = app.Allocate("murmur", 1, 1)
if err != nil {
return
}
// wait until a key stroke
var stroke []byte = make([]byte, 100)
fmt.Println("waiting for tasks...stop waiting by pressing enter")
os.Stdin.Read(stroke)
// release resource
err = app.Close()
if err != nil {
return
}
}
// implement CustomMarshallerCodec for this worker function
// func concate(words []string) (ret string)
type testCustomMarshallerCodec struct{}
func (me *testCustomMarshallerCodec) Prepare(name string, fn interface{}) (err error) { return }
func (me *testCustomMarshallerCodec) EncodeArgument(_ interface{}, val []interface{}) (bs [][]byte, err error) {
fmt.Println("encode argument is called")
b, err := json.Marshal(val[0])
if err != nil {
return
}
bs = [][]byte{b}
return
}
func (me *testCustomMarshallerCodec) DecodeArgument(_ interface{}, bs [][]byte) (val []interface{}, err error) {
fmt.Println("decode argument is called")
var input []string
err = json.Unmarshal(bs[0], &input)
if err != nil {
return
}
val = []interface{}{input}
return
}
func (me *testCustomMarshallerCodec) EncodeReturn(_ interface{}, val []interface{}) (bs [][]byte, err error) {
fmt.Println("encode return is called")
b, err := json.Marshal(val[0])
if err != nil {
return
}
bs = [][]byte{b}
return
}
func (me *testCustomMarshallerCodec) DecodeReturn(_ interface{}, bs [][]byte) (val []interface{}, err error) {
fmt.Println("decode return is called")
var ret string
err = json.Unmarshal(bs[0], &ret)
if err != nil {
return
}
val = []interface{}{ret}
return
}
type testMyInvoker3 struct{}
func (me *testMyInvoker3) Call(f interface{}, param []interface{}) ([]interface{}, error) {
fmt.Println("my invoker is called for Call")
// type assertion
ret := f.(func([]string) string)(param[0].([]string))
return []interface{}{ret}, nil
}
func (me *testMyInvoker3) Return(f interface{}, returns []interface{}) ([]interface{}, error) {
fmt.Println("my invoker is called for Return")
return returns, nil
}
func ExampleCustomMarshaller() {
/*
import (
"encoding/json"
"fmt"
"github.com/mission-liao/dingo"
)
*/
// this example demonstrate the usage of using a
// customized marshaller by encoding every parameter
// in JSON.
// And invoke it with customized invoker
var err error
defer func() {
if err != nil {
fmt.Printf("%v\n", err)
}
}()
// an App in remote mode, with local backend/broker
app, err := dingo.NewApp("remote", nil)
if err != nil {
return
}
// attach a local broker.
broker, err := dingo.NewLocalBroker(dingo.DefaultConfig(), nil)
if err != nil {
return
}
_, _, err = app.Use(broker, dingo.ObjT.Default)
// attach a local backend
backend, err := dingo.NewLocalBackend(dingo.DefaultConfig(), nil)
if err != nil {
return
}
_, _, err = app.Use(backend, dingo.ObjT.Default)
if err != nil {
return
}
// register customize marshaller & invoker
err = app.AddMarshaller(101, &struct {
testMyInvoker3
dingo.CustomMarshaller
}{
testMyInvoker3{},
dingo.CustomMarshaller{Codec: &testCustomMarshallerCodec{}},
})
if err != nil {
return
}
// register worker function
err = app.Register("concat", func(words []string) (ret string) {
for _, v := range words {
ret = ret + v
}
return
})
if err != nil {
return
}
// change marshaller of worker function
err = app.SetMarshaller("concat", 101, 101)
if err != nil {
return
}
// allocate workers
_, err = app.Allocate("concat", 1, 1)
if err != nil {
return
}
// trigger the fire...
result := dingo.NewResult(app.Call("concat", nil, []string{"Merry ", "X", "'mas"}))
err = result.Wait(0)
if err != nil {
return
}
result.OnOK(func(ret string) {
fmt.Printf("%v\n", ret)
})
err = app.Close()
if err != nil {
return
}
}