-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
58 lines (49 loc) · 1.51 KB
/
main_test.go
File metadata and controls
58 lines (49 loc) · 1.51 KB
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
package uwe
import (
"log"
"time"
)
func Example() {
// initialize new instance of Chief
chief := NewChief()
// will add workers into the pool
chief.AddWorker("dummy", NewDummy())
// pass handler for internal events like errors, panics, warning, etc.
// you can log it with you favorite logger (ex Logrus, Zap, etc)
chief.SetEventHandler(STDLogEventHandler())
// init all registered workers and run it all
chief.Run()
}
type dummy struct{}
// NewDummy initialize new instance of dummy Worker.
func NewDummy() Worker {
// At this point in most cases there we are preparing some state of the worker,
// like a logger, configuration, variable, and fields.
return &dummy{}
}
// Init is an interface method used to initialize some state of the worker
// that required interaction with outer context, for example, initialize some connectors.
func (d *dummy) Init() error { return nil }
// Run starts event loop of worker.
func (d *dummy) Run(ctx Context) error {
// initialize all required stuffs for the execution flow
ticker := time.NewTicker(time.Second)
for {
select {
case <-ticker.C:
// define all the processing code here
// or move it to a method and make a call here
log.Println("do something")
case msg := <-ctx.Messages():
if msg == nil {
continue
}
log.Printf("Received new message: sender(%s) kind(%d) data(%v)\n",
msg.Sender, msg.Kind, msg.Data)
case <-ctx.Done():
// close all connections, channels and finalise state if needed
log.Println("good bye")
return nil
}
}
}