@@ -13,13 +13,21 @@ Queue is a Golang library for spawning and managing a Goroutine pool, Alloowing
13
13
14
14
## Installation
15
15
16
+ Install the stable version:
17
+
16
18
``` sh
17
19
go get github.com/appleboy/queue
18
20
```
19
21
22
+ Install the latest verison:
23
+
24
+ ``` sh
25
+ go get github.com/appleboy/queue@master
26
+ ```
27
+
20
28
## Usage
21
29
22
- First to create new job as ` QueueMessage ` interface:
30
+ The first step to create a new job as ` QueueMessage ` interface:
23
31
24
32
``` go
25
33
type job struct {
@@ -31,13 +39,13 @@ func (j *job) Bytes() []byte {
31
39
}
32
40
```
33
41
34
- Second to create the new worker, use buffered channel as example:
42
+ The second step to create the new worker, use the buffered channel as an example, you can use the ` stop ` channel to terminate the job immediately after shutdown the queue service if need.
35
43
36
44
``` go
37
45
// define the worker
38
46
w := simple.NewWorker (
39
47
simple.WithQueueNum (taskN),
40
- simple.WithRunFunc (func (m queue.QueuedMessage ) error {
48
+ simple.WithRunFunc (func (m queue.QueuedMessage , stop <- chan struct {} ) error {
41
49
v , ok := m.(*job)
42
50
if !ok {
43
51
if err := json.Unmarshal (m.Bytes (), &v); err != nil {
@@ -51,7 +59,7 @@ Second to create the new worker, use buffered channel as example:
51
59
)
52
60
```
53
61
54
- or you can use the [ NSQ] ( https://nsq.io/ ) as backend, see the worker example:
62
+ or use the [ NSQ] ( https://nsq.io/ ) as backend, see the worker example:
55
63
56
64
``` go
57
65
// define the worker
@@ -61,7 +69,7 @@ or you can use the [NSQ](https://nsq.io/) as backend, see the worker example:
61
69
nsq.WithChannel (" foobar" ),
62
70
// concurrent job number
63
71
nsq.WithMaxInFlight (10 ),
64
- nsq.WithRunFunc (func (m queue.QueuedMessage ) error {
72
+ nsq.WithRunFunc (func (m queue.QueuedMessage , stop <- chan struct {} ) error {
65
73
v , ok := m.(*job)
66
74
if !ok {
67
75
if err := json.Unmarshal (m.Bytes (), &v); err != nil {
@@ -75,7 +83,7 @@ or you can use the [NSQ](https://nsq.io/) as backend, see the worker example:
75
83
)
76
84
```
77
85
78
- Third to create queue and initialize multiple worker , receive all job message :
86
+ The third step to create a queue and initialize multiple workers , receive all job messages :
79
87
80
88
``` go
81
89
// define the queue
@@ -109,7 +117,7 @@ Third to create queue and initialize multiple worker, receive all job message:
109
117
q.Wait ()
110
118
```
111
119
112
- Full example code as below or [ try it in playground] ( https://play.golang.org/p/ZM3XAnYcAs7 ) .
120
+ Full example code as below or [ try it in playground] ( https://play.golang.org/p/yaTUoYxdcaK ) .
113
121
114
122
``` go
115
123
package main
@@ -139,7 +147,7 @@ func main() {
139
147
// define the worker
140
148
w := simple.NewWorker (
141
149
simple.WithQueueNum (taskN),
142
- simple.WithRunFunc (func (m queue.QueuedMessage ) error {
150
+ simple.WithRunFunc (func (m queue.QueuedMessage , _ <- chan struct {} ) error {
143
151
v , ok := m.(*job)
144
152
if !ok {
145
153
if err := json.Unmarshal (m.Bytes (), &v); err != nil {
0 commit comments