4
4
"context"
5
5
"fmt"
6
6
"os"
7
+ "os/signal"
7
8
"sync"
8
9
"time"
9
10
22
23
executions map [string ]* Execution
23
24
// node is the node the worker is registered with.
24
25
node * pool.Node
25
- // done is closed when the worker is stopped .
26
- done chan struct {}
26
+ // logctx is the logger context .
27
+ logctx context. Context
27
28
}
28
29
29
30
// Execution represents a single execution.
@@ -34,31 +35,42 @@ type (
34
35
)
35
36
36
37
func main () {
37
- ctx := log . Context ( context . Background (), log . WithDebug ())
38
+ // Setup Redis connection
38
39
rdb := redis .NewClient (& redis.Options {
39
40
Addr : "localhost:6379" ,
40
41
Password : os .Getenv ("REDIS_PASSWORD" ),
41
42
})
42
43
44
+ // Setup clue logger.
45
+ ctx := log .Context (context .Background ())
46
+ log .FlushAndDisableBuffering (ctx )
47
+
48
+ var logger pulse.Logger
49
+ if len (os .Args ) > 1 && os .Args [1 ] == "-v" {
50
+ logger = pulse .ClueLogger (ctx )
51
+ }
52
+
43
53
// Create node for pool "example".
44
- node , err := pool .AddNode (ctx ,
45
- "example" ,
46
- rdb ,
47
- pool .WithLogger (pulse .ClueLogger (ctx )))
54
+ node , err := pool .AddNode (ctx , "example" , rdb , pool .WithLogger (logger ))
48
55
if err != nil {
49
56
panic (err )
50
57
}
51
58
52
59
// Create a new worker for pool "example".
53
- c := make (chan struct {})
54
- handler := & JobHandler {executions : make (map [string ]* Execution ), node : node , done : c }
60
+ handler := & JobHandler {executions : make (map [string ]* Execution ), node : node , logctx : ctx }
55
61
if _ , err := node .AddWorker (ctx , handler ); err != nil {
56
62
panic (err )
57
63
}
58
64
59
65
// Wait for jobs to complete.
60
- fmt .Println ("Waiting for jobs..." )
61
- <- c
66
+ log .Infof (ctx , "Waiting for jobs... CTRL+C to stop." )
67
+
68
+ // Close done channel on CTRL-C.
69
+ sigc := make (chan os.Signal , 1 )
70
+ signal .Notify (sigc , os .Interrupt )
71
+ <- sigc
72
+ close (sigc )
73
+
62
74
if err := node .Shutdown (ctx ); err != nil {
63
75
panic (err )
64
76
}
@@ -70,7 +82,7 @@ func (w *JobHandler) Start(job *pool.Job) error {
70
82
defer w .lock .Unlock ()
71
83
exec := & Execution {c : make (chan struct {})}
72
84
w .executions [job .Key ] = exec
73
- go exec .Start (job )
85
+ go exec .Start (w . logctx , job )
74
86
return nil
75
87
}
76
88
@@ -84,31 +96,29 @@ func (w *JobHandler) Stop(key string) error {
84
96
}
85
97
close (exec .c )
86
98
delete (w .executions , key )
87
- if key == "two" {
88
- close (w .done )
89
- }
90
99
return nil
91
100
}
92
101
93
102
// Print notification.
94
103
func (w * JobHandler ) HandleNotification (key string , payload []byte ) error {
95
- fmt . Printf ( ">> Notification: %s \n " , string (payload ))
104
+ log . Info ( w . logctx , log. Fields { "msg" : "notification" , "key" : key , "payload" : string (payload )} )
96
105
return nil
97
106
}
98
107
99
108
// Start execution.
100
- func (c * Execution ) Start (job * pool.Job ) {
101
- defer fmt .Printf ("Worker %s, Job %s, Done\n " , job .Worker .ID , job .Key )
109
+ func (c * Execution ) Start (ctx context.Context , job * pool.Job ) {
110
+ log .Info (ctx , log.Fields {"msg" : "job started" , "worker-id" : job .Worker .ID , "job" : job .Key })
111
+ defer log .Info (ctx , log.Fields {"msg" : "job done" , "worker-id" : job .Worker .ID , "job" : job .Key })
102
112
i := 1
103
- ticker := time .NewTicker (500 * time .Millisecond )
113
+ ticker := time .NewTicker (200 * time .Millisecond )
104
114
defer ticker .Stop ()
105
115
for {
106
116
select {
107
117
case <- c .c :
108
118
return
109
119
case <- ticker .C :
110
120
i ++
111
- fmt . Printf ( ">> Worker %s, Job %s, Iteration %d \n " , job .Worker .ID , job .Key , i )
121
+ log . Info ( ctx , log. Fields { "msg" : "tick" , "worker-id" : job .Worker .ID , " job" : job .Key , "i" : i } )
112
122
}
113
123
}
114
124
}
0 commit comments