Skip to content

Commit a4ac305

Browse files
authored
chore(simple): update simple queue example (#21)
Signed-off-by: Bo-Yi Wu <[email protected]>
1 parent eaaf46b commit a4ac305

File tree

4 files changed

+18
-7
lines changed

4 files changed

+18
-7
lines changed

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,11 @@ type job struct {
3636
}
3737

3838
func (j *job) Bytes() []byte {
39-
return []byte(j.Message)
39+
b, err := json.Marshal(j)
40+
if err != nil {
41+
panic(err)
42+
}
43+
return b
4044
}
4145
```
4246

@@ -46,7 +50,7 @@ The second step to create the new worker, use the buffered channel as an example
4650
// define the worker
4751
w := simple.NewWorker(
4852
simple.WithQueueNum(taskN),
49-
simple.WithRunFunc(func(m queue.QueuedMessage, stop <-chan struct{}) error {
53+
simple.WithRunFunc(func(ctx context.Context, m queue.QueuedMessage) error {
5054
v, ok := m.(*job)
5155
if !ok {
5256
if err := json.Unmarshal(m.Bytes(), &v); err != nil {
@@ -70,7 +74,7 @@ w := nsq.NewWorker(
7074
nsq.WithChannel("foobar"),
7175
// concurrent job number
7276
nsq.WithMaxInFlight(10),
73-
nsq.WithRunFunc(func(m queue.QueuedMessage, stop <-chan struct{}) error {
77+
nsq.WithRunFunc(func(ctx context.Context, m queue.QueuedMessage) error {
7478
v, ok := m.(*job)
7579
if !ok {
7680
if err := json.Unmarshal(m.Bytes(), &v); err != nil {
@@ -91,7 +95,7 @@ w := nats.NewWorker(
9195
nats.WithAddr("127.0.0.1:4222"),
9296
nats.WithSubj("example"),
9397
nats.WithQueue("foobar"),
94-
nats.WithRunFunc(func(m queue.QueuedMessage, _ <-chan struct{}) error {
98+
nats.WithRunFunc(func(ctx context.Context, m queue.QueuedMessage) error {
9599
v, ok := m.(*job)
96100
if !ok {
97101
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.20210726090849-82cf3ebeafc5
5+
require github.com/appleboy/queue v0.0.4-0.20210727142412-eaaf46bd6231

_example/simple/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
github.com/appleboy/queue v0.0.4-0.20210726090849-82cf3ebeafc5 h1:UE6zMJQObLz/E/RcV3EMEXnU2wmeftvf9kNHozS5300=
22
github.com/appleboy/queue v0.0.4-0.20210726090849-82cf3ebeafc5/go.mod h1:cEQW2y7dduAUqUGnGJEK9oM5bZLlc0+3HI9bTUL0+Ek=
3+
github.com/appleboy/queue v0.0.4-0.20210727142412-eaaf46bd6231 h1:OGNVcnnlPLRiK27xIcinWDCITRNJYHdO5SBLfl2DuFA=
4+
github.com/appleboy/queue v0.0.4-0.20210727142412-eaaf46bd6231/go.mod h1:cEQW2y7dduAUqUGnGJEK9oM5bZLlc0+3HI9bTUL0+Ek=
35
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
46
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
57
github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8=

_example/simple/main.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"context"
45
"encoding/json"
56
"fmt"
67
"log"
@@ -15,7 +16,11 @@ type job struct {
1516
}
1617

1718
func (j *job) Bytes() []byte {
18-
return []byte(j.Message)
19+
b, err := json.Marshal(j)
20+
if err != nil {
21+
panic(err)
22+
}
23+
return b
1924
}
2025

2126
func main() {
@@ -25,7 +30,7 @@ func main() {
2530
// define the worker
2631
w := simple.NewWorker(
2732
simple.WithQueueNum(taskN),
28-
simple.WithRunFunc(func(m queue.QueuedMessage, _ <-chan struct{}) error {
33+
simple.WithRunFunc(func(ctx context.Context, m queue.QueuedMessage) error {
2934
v, ok := m.(*job)
3035
if !ok {
3136
if err := json.Unmarshal(m.Bytes(), &v); err != nil {

0 commit comments

Comments
 (0)