Skip to content

Commit 60ec169

Browse files
committed
docs: fix indent
Signed-off-by: Bo-Yi Wu <[email protected]>
1 parent 3dc0b91 commit 60ec169

File tree

1 file changed

+125
-125
lines changed

1 file changed

+125
-125
lines changed

README.md

Lines changed: 125 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -31,92 +31,92 @@ The first step to create a new job as `QueueMessage` interface:
3131

3232
```go
3333
type job struct {
34-
Message string
34+
Message string
3535
}
3636

3737
func (j *job) Bytes() []byte {
38-
return []byte(j.Message)
38+
return []byte(j.Message)
3939
}
4040
```
4141

4242
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.
4343

4444
```go
45-
// define the worker
46-
w := simple.NewWorker(
47-
simple.WithQueueNum(taskN),
48-
simple.WithRunFunc(func(m queue.QueuedMessage, stop <-chan struct{}) error {
49-
v, ok := m.(*job)
50-
if !ok {
51-
if err := json.Unmarshal(m.Bytes(), &v); err != nil {
52-
return err
53-
}
54-
}
55-
56-
rets <- v.Message
57-
return nil
58-
}),
59-
)
45+
// define the worker
46+
w := simple.NewWorker(
47+
simple.WithQueueNum(taskN),
48+
simple.WithRunFunc(func(m queue.QueuedMessage, stop <-chan struct{}) error {
49+
v, ok := m.(*job)
50+
if !ok {
51+
if err := json.Unmarshal(m.Bytes(), &v); err != nil {
52+
return err
53+
}
54+
}
55+
56+
rets <- v.Message
57+
return nil
58+
}),
59+
)
6060
```
6161

6262
or use the [NSQ](https://nsq.io/) as backend, see the worker example:
6363

6464
```go
65-
// define the worker
66-
w := nsq.NewWorker(
67-
nsq.WithAddr("127.0.0.1:4150"),
68-
nsq.WithTopic("example"),
69-
nsq.WithChannel("foobar"),
70-
// concurrent job number
71-
nsq.WithMaxInFlight(10),
72-
nsq.WithRunFunc(func(m queue.QueuedMessage, stop <-chan struct{}) error {
73-
v, ok := m.(*job)
74-
if !ok {
75-
if err := json.Unmarshal(m.Bytes(), &v); err != nil {
76-
return err
77-
}
78-
}
79-
80-
rets <- v.Message
81-
return nil
82-
}),
83-
)
65+
// define the worker
66+
w := nsq.NewWorker(
67+
nsq.WithAddr("127.0.0.1:4150"),
68+
nsq.WithTopic("example"),
69+
nsq.WithChannel("foobar"),
70+
// concurrent job number
71+
nsq.WithMaxInFlight(10),
72+
nsq.WithRunFunc(func(m queue.QueuedMessage, stop <-chan struct{}) error {
73+
v, ok := m.(*job)
74+
if !ok {
75+
if err := json.Unmarshal(m.Bytes(), &v); err != nil {
76+
return err
77+
}
78+
}
79+
80+
rets <- v.Message
81+
return nil
82+
}),
83+
)
8484
```
8585

8686
The third step to create a queue and initialize multiple workers, receive all job messages:
8787

8888
```go
89-
// define the queue
90-
q, err := queue.NewQueue(
91-
queue.WithWorkerCount(5),
92-
queue.WithWorker(w),
93-
)
94-
if err != nil {
95-
log.Fatal(err)
96-
}
97-
98-
// start the five worker
99-
q.Start()
100-
101-
// assign tasks in queue
102-
for i := 0; i < taskN; i++ {
103-
go func(i int) {
104-
q.Queue(&job{
105-
Message: fmt.Sprintf("handle the job: %d", i+1),
106-
})
107-
}(i)
108-
}
109-
110-
// wait until all tasks done
111-
for i := 0; i < taskN; i++ {
112-
fmt.Println("message:", <-rets)
113-
time.Sleep(50 * time.Millisecond)
114-
}
115-
116-
// shutdown the service and notify all the worker
117-
q.Shutdown()
118-
// wait all jobs are complete.
119-
q.Wait()
89+
// define the queue
90+
q, err := queue.NewQueue(
91+
queue.WithWorkerCount(5),
92+
queue.WithWorker(w),
93+
)
94+
if err != nil {
95+
log.Fatal(err)
96+
}
97+
98+
// start the five worker
99+
q.Start()
100+
101+
// assign tasks in queue
102+
for i := 0; i < taskN; i++ {
103+
go func(i int) {
104+
q.Queue(&job{
105+
Message: fmt.Sprintf("handle the job: %d", i+1),
106+
})
107+
}(i)
108+
}
109+
110+
// wait until all tasks done
111+
for i := 0; i < taskN; i++ {
112+
fmt.Println("message:", <-rets)
113+
time.Sleep(50 * time.Millisecond)
114+
}
115+
116+
// shutdown the service and notify all the worker
117+
q.Shutdown()
118+
// wait all jobs are complete.
119+
q.Wait()
120120
```
121121

122122
Full example code as below or [try it in playground](https://play.golang.org/p/yaTUoYxdcaK).
@@ -125,73 +125,73 @@ Full example code as below or [try it in playground](https://play.golang.org/p/y
125125
package main
126126

127127
import (
128-
"encoding/json"
129-
"fmt"
130-
"log"
131-
"time"
128+
"encoding/json"
129+
"fmt"
130+
"log"
131+
"time"
132132

133-
"github.com/appleboy/queue"
134-
"github.com/appleboy/queue/simple"
133+
"github.com/appleboy/queue"
134+
"github.com/appleboy/queue/simple"
135135
)
136136

137137
type job struct {
138-
Message string
138+
Message string
139139
}
140140

141141
func (j *job) Bytes() []byte {
142-
return []byte(j.Message)
142+
return []byte(j.Message)
143143
}
144144

145145
func main() {
146-
taskN := 100
147-
rets := make(chan string, taskN)
148-
149-
// define the worker
150-
w := simple.NewWorker(
151-
simple.WithQueueNum(taskN),
152-
simple.WithRunFunc(func(m queue.QueuedMessage, _ <-chan struct{}) error {
153-
v, ok := m.(*job)
154-
if !ok {
155-
if err := json.Unmarshal(m.Bytes(), &v); err != nil {
156-
return err
157-
}
158-
}
159-
160-
rets <- v.Message
161-
return nil
162-
}),
163-
)
164-
165-
// define the queue
166-
q, err := queue.NewQueue(
167-
queue.WithWorkerCount(5),
168-
queue.WithWorker(w),
169-
)
170-
if err != nil {
171-
log.Fatal(err)
172-
}
173-
174-
// start the five worker
175-
q.Start()
176-
177-
// assign tasks in queue
178-
for i := 0; i < taskN; i++ {
179-
go func(i int) {
180-
q.Queue(&job{
181-
Message: fmt.Sprintf("handle the job: %d", i+1),
182-
})
183-
}(i)
184-
}
185-
186-
// wait until all tasks done
187-
for i := 0; i < taskN; i++ {
188-
fmt.Println("message:", <-rets)
189-
time.Sleep(50 * time.Millisecond)
190-
}
191-
192-
// shutdown the service and notify all the worker
193-
q.Shutdown()
194-
// wait all jobs are complete.
195-
q.Wait()
146+
taskN := 100
147+
rets := make(chan string, taskN)
148+
149+
// define the worker
150+
w := simple.NewWorker(
151+
simple.WithQueueNum(taskN),
152+
simple.WithRunFunc(func(m queue.QueuedMessage, _ <-chan struct{}) error {
153+
v, ok := m.(*job)
154+
if !ok {
155+
if err := json.Unmarshal(m.Bytes(), &v); err != nil {
156+
return err
157+
}
158+
}
159+
160+
rets <- v.Message
161+
return nil
162+
}),
163+
)
164+
165+
// define the queue
166+
q, err := queue.NewQueue(
167+
queue.WithWorkerCount(5),
168+
queue.WithWorker(w),
169+
)
170+
if err != nil {
171+
log.Fatal(err)
172+
}
173+
174+
// start the five worker
175+
q.Start()
176+
177+
// assign tasks in queue
178+
for i := 0; i < taskN; i++ {
179+
go func(i int) {
180+
q.Queue(&job{
181+
Message: fmt.Sprintf("handle the job: %d", i+1),
182+
})
183+
}(i)
184+
}
185+
186+
// wait until all tasks done
187+
for i := 0; i < taskN; i++ {
188+
fmt.Println("message:", <-rets)
189+
time.Sleep(50 * time.Millisecond)
190+
}
191+
192+
// shutdown the service and notify all the worker
193+
q.Shutdown()
194+
// wait all jobs are complete.
195+
q.Wait()
196196
}
197197
```

0 commit comments

Comments
 (0)