Skip to content

Commit 871b188

Browse files
authored
docs: update example (#13)
Signed-off-by: Bo-Yi Wu <[email protected]>
1 parent 79774a8 commit 871b188

File tree

7 files changed

+24
-12
lines changed

7 files changed

+24
-12
lines changed

README.md

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,21 @@ Queue is a Golang library for spawning and managing a Goroutine pool, Alloowing
1313

1414
## Installation
1515

16+
Install the stable version:
17+
1618
```sh
1719
go get github.com/appleboy/queue
1820
```
1921

22+
Install the latest verison:
23+
24+
```sh
25+
go get github.com/appleboy/queue@master
26+
```
27+
2028
## Usage
2129

22-
First to create new job as `QueueMessage` interface:
30+
The first step to create a new job as `QueueMessage` interface:
2331

2432
```go
2533
type job struct {
@@ -31,13 +39,13 @@ func (j *job) Bytes() []byte {
3139
}
3240
```
3341

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.
3543

3644
```go
3745
// define the worker
3846
w := simple.NewWorker(
3947
simple.WithQueueNum(taskN),
40-
simple.WithRunFunc(func(m queue.QueuedMessage) error {
48+
simple.WithRunFunc(func(m queue.QueuedMessage, stop <-chan struct{}) error {
4149
v, ok := m.(*job)
4250
if !ok {
4351
if err := json.Unmarshal(m.Bytes(), &v); err != nil {
@@ -51,7 +59,7 @@ Second to create the new worker, use buffered channel as example:
5159
)
5260
```
5361

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:
5563

5664
```go
5765
// define the worker
@@ -61,7 +69,7 @@ or you can use the [NSQ](https://nsq.io/) as backend, see the worker example:
6169
nsq.WithChannel("foobar"),
6270
// concurrent job number
6371
nsq.WithMaxInFlight(10),
64-
nsq.WithRunFunc(func(m queue.QueuedMessage) error {
72+
nsq.WithRunFunc(func(m queue.QueuedMessage, stop <-chan struct{}) error {
6573
v, ok := m.(*job)
6674
if !ok {
6775
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:
7583
)
7684
```
7785

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:
7987

8088
```go
8189
// define the queue
@@ -109,7 +117,7 @@ Third to create queue and initialize multiple worker, receive all job message:
109117
q.Wait()
110118
```
111119

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).
113121

114122
```go
115123
package main
@@ -139,7 +147,7 @@ func main() {
139147
// define the worker
140148
w := simple.NewWorker(
141149
simple.WithQueueNum(taskN),
142-
simple.WithRunFunc(func(m queue.QueuedMessage) error {
150+
simple.WithRunFunc(func(m queue.QueuedMessage, _ <-chan struct{}) error {
143151
v, ok := m.(*job)
144152
if !ok {
145153
if err := json.Unmarshal(m.Bytes(), &v); err != nil {

_example/nsq/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ module example
22

33
go 1.16
44

5-
require github.com/appleboy/queue v0.0.4-0.20210725052905-4ef008157945
5+
require github.com/appleboy/queue v0.0.4-0.20210726005953-79774a856a1e

_example/nsq/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ github.com/appleboy/queue v0.0.3 h1:rntqVTm6ilh80VCVQjwA0vDMCl1cfveq6GS6X98fKwE=
22
github.com/appleboy/queue v0.0.3/go.mod h1:6Mn0z4hURZW/26huvRXG0SJ4o7pBdo6hOryRiegy/4Q=
33
github.com/appleboy/queue v0.0.4-0.20210725052905-4ef008157945 h1:Mec41QJ0hGDtbyfWgHfuXKqTu0a8hSIIBhL3sNbNjqQ=
44
github.com/appleboy/queue v0.0.4-0.20210725052905-4ef008157945/go.mod h1:6Mn0z4hURZW/26huvRXG0SJ4o7pBdo6hOryRiegy/4Q=
5+
github.com/appleboy/queue v0.0.4-0.20210726005953-79774a856a1e h1:+Mj8GY3F5J6rJCUoUlqAu68UUwaP2B3G4Xce5eSBNY0=
6+
github.com/appleboy/queue v0.0.4-0.20210726005953-79774a856a1e/go.mod h1:6Mn0z4hURZW/26huvRXG0SJ4o7pBdo6hOryRiegy/4Q=
57
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
68
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
79
github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4=

_example/nsq/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func main() {
3333
nsq.WithChannel("foobar"),
3434
// concurrent job number
3535
nsq.WithMaxInFlight(10),
36-
nsq.WithRunFunc(func(m queue.QueuedMessage) error {
36+
nsq.WithRunFunc(func(m queue.QueuedMessage, _ <-chan struct{}) error {
3737
v, ok := m.(*job)
3838
if !ok {
3939
if err := json.Unmarshal(m.Bytes(), &v); err != nil {

_example/simple/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ module example
22

33
go 1.16
44

5-
require github.com/appleboy/queue v0.0.4-0.20210725052905-4ef008157945 // indirect
5+
require github.com/appleboy/queue v0.0.4-0.20210726005953-79774a856a1e // indirect

_example/simple/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ github.com/appleboy/queue v0.0.3 h1:rntqVTm6ilh80VCVQjwA0vDMCl1cfveq6GS6X98fKwE=
22
github.com/appleboy/queue v0.0.3/go.mod h1:6Mn0z4hURZW/26huvRXG0SJ4o7pBdo6hOryRiegy/4Q=
33
github.com/appleboy/queue v0.0.4-0.20210725052905-4ef008157945 h1:Mec41QJ0hGDtbyfWgHfuXKqTu0a8hSIIBhL3sNbNjqQ=
44
github.com/appleboy/queue v0.0.4-0.20210725052905-4ef008157945/go.mod h1:6Mn0z4hURZW/26huvRXG0SJ4o7pBdo6hOryRiegy/4Q=
5+
github.com/appleboy/queue v0.0.4-0.20210726005953-79774a856a1e h1:+Mj8GY3F5J6rJCUoUlqAu68UUwaP2B3G4Xce5eSBNY0=
6+
github.com/appleboy/queue v0.0.4-0.20210726005953-79774a856a1e/go.mod h1:6Mn0z4hURZW/26huvRXG0SJ4o7pBdo6hOryRiegy/4Q=
57
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
68
github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
79
github.com/nsqio/go-nsq v1.0.8/go.mod h1:vKq36oyeVXgsS5Q8YEO7WghqidAVXQlcFxzQbQTuDEY=

_example/simple/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func main() {
2525
// define the worker
2626
w := simple.NewWorker(
2727
simple.WithQueueNum(taskN),
28-
simple.WithRunFunc(func(m queue.QueuedMessage) error {
28+
simple.WithRunFunc(func(m queue.QueuedMessage, _ <-chan struct{}) error {
2929
v, ok := m.(*job)
3030
if !ok {
3131
if err := json.Unmarshal(m.Bytes(), &v); err != nil {

0 commit comments

Comments
 (0)