Skip to content

Commit b81f65e

Browse files
authored
Merge pull request #52 from tinh-tinh/feat/ren/51-add-processor
feat: add processor to easy handle
2 parents c0c8b85 + 224a859 commit b81f65e

6 files changed

Lines changed: 133 additions & 18 deletions

File tree

‎go.mod‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
module github.com/tinh-tinh/queue/v2
22

3-
go 1.22.0
3+
go 1.23.0
4+
45
toolchain go1.24.1
56

67
require (
78
github.com/go-redsync/redsync/v4 v4.13.0
8-
github.com/redis/go-redis/v9 v9.6.3
9-
github.com/robfig/cron/v3 v3.0.0
9+
github.com/redis/go-redis/v9 v9.10.0
10+
github.com/robfig/cron/v3 v3.0.1
1011
github.com/stretchr/testify v1.9.0
11-
github.com/tinh-tinh/tinhtinh/v2 v2.0.0
12+
github.com/tinh-tinh/tinhtinh/v2 v2.1.3
1213
golang.org/x/crypto v0.35.0
1314
)
1415

1516
require (
16-
github.com/cespare/xxhash/v2 v2.2.0 // indirect
17+
github.com/cespare/xxhash/v2 v2.3.0 // indirect
1718
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
1819
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
1920
github.com/hashicorp/errwrap v1.1.0 // indirect

‎go.sum‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
44
github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0=
55
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
66
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
7+
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
8+
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
79
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
810
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
911
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
@@ -27,16 +29,24 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
2729
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
2830
github.com/redis/go-redis/v9 v9.6.3 h1:8Dr5ygF1QFXRxIH/m3Xg9MMG1rS8YCtAgosrsewT6i0=
2931
github.com/redis/go-redis/v9 v9.6.3/go.mod h1:0C0c6ycQsdpVNQpxb1njEQIqkx5UcsM8FJCQLgE9+RA=
32+
github.com/redis/go-redis/v9 v9.10.0 h1:FxwK3eV8p/CQa0Ch276C7u2d0eNC9kCmAYQ7mCXCzVs=
33+
github.com/redis/go-redis/v9 v9.10.0/go.mod h1:huWgSWd8mW6+m0VPhJjSSQ+d6Nh1VICQ6Q5lHuCH/Iw=
3034
github.com/redis/rueidis v1.0.19 h1:s65oWtotzlIFN8eMPhyYwxlwLR1lUdhza2KtWprKYSo=
3135
github.com/redis/rueidis v1.0.19/go.mod h1:8B+r5wdnjwK3lTFml5VtxjzGOQAC+5UmujoD12pDrEo=
3236
github.com/robfig/cron/v3 v3.0.0 h1:kQ6Cb7aHOHTSzNVNEhmp8EcWKLb4CbiMW9h9VyIhO4E=
3337
github.com/robfig/cron/v3 v3.0.0/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
38+
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
39+
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
3440
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
3541
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
3642
github.com/stvp/tempredis v0.0.0-20181119212430-b82af8480203 h1:QVqDTf3h2WHt08YuiTGPZLls0Wq99X9bWd0Q5ZSBesM=
3743
github.com/stvp/tempredis v0.0.0-20181119212430-b82af8480203/go.mod h1:oqN97ltKNihBbwlX8dLpwxCl3+HnXKV/R0e+sRLd9C8=
3844
github.com/tinh-tinh/tinhtinh/v2 v2.0.0 h1:+AUbzU4EHS2HuXA7pk+bswRSURd9duF4vBLRTjg4vmk=
3945
github.com/tinh-tinh/tinhtinh/v2 v2.0.0/go.mod h1:4nppE7KAIswZKutI9ElMqAD9kyash7aea0Ewowsqj5g=
46+
github.com/tinh-tinh/tinhtinh/v2 v2.1.2 h1:rd3m0YMt2Og1VTgF9ktWkj86FY3+caCvJNxhSw/qpnk=
47+
github.com/tinh-tinh/tinhtinh/v2 v2.1.2/go.mod h1:4nppE7KAIswZKutI9ElMqAD9kyash7aea0Ewowsqj5g=
48+
github.com/tinh-tinh/tinhtinh/v2 v2.1.3 h1:ug+9XFhKH/Z6jY5TqV5ioWq2kOhO/9q1+jG7wGjHp2s=
49+
github.com/tinh-tinh/tinhtinh/v2 v2.1.3/go.mod h1:4nppE7KAIswZKutI9ElMqAD9kyash7aea0Ewowsqj5g=
4050
golang.org/x/crypto v0.35.0 h1:b15kiHdrGCHrP6LvwaQ3c03kgNhhiMgvlhxHQhmg2Xs=
4151
golang.org/x/crypto v0.35.0/go.mod h1:dy7dXNW32cAb/6/PRuTNsix8T+vJAqvuIy5Bli/x0YQ=
4252
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=

‎processor.go‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package queue
2+
3+
import "github.com/tinh-tinh/tinhtinh/v2/core"
4+
5+
type Processor struct {
6+
core.DynamicProvider
7+
module core.Module
8+
name string
9+
}
10+
11+
func NewProcessor(name string, module core.Module) *Processor {
12+
return &Processor{
13+
module: module,
14+
name: name,
15+
}
16+
}
17+
18+
func (p *Processor) Process(jobFnc JobFnc) {
19+
pQueue := Inject(p.module, p.name)
20+
if pQueue != nil {
21+
pQueue.Process(jobFnc)
22+
}
23+
}

‎processor_test.go‎

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package queue_test
2+
3+
import (
4+
"encoding/json"
5+
"errors"
6+
"net/http"
7+
"net/http/httptest"
8+
"strconv"
9+
"testing"
10+
"time"
11+
12+
"github.com/redis/go-redis/v9"
13+
"github.com/stretchr/testify/require"
14+
"github.com/tinh-tinh/queue/v2"
15+
"github.com/tinh-tinh/tinhtinh/v2/core"
16+
)
17+
18+
func Test_Proccessor(t *testing.T) {
19+
service := func(module core.Module) core.Provider {
20+
processor := queue.NewProcessor("test", module)
21+
22+
processor.Process(func(job *queue.Job) {
23+
job.Process(func() error {
24+
num, err := strconv.Atoi(job.Id)
25+
require.Nil(t, err)
26+
if num%3 == 0 {
27+
return errors.New("error by test")
28+
}
29+
30+
key, err := json.Marshal(job.Data)
31+
require.Nil(t, err)
32+
_, err = HeaveTask(string(key))
33+
require.Nil(t, err)
34+
return nil
35+
})
36+
})
37+
38+
return processor
39+
}
40+
41+
controller := func(module core.Module) core.Controller {
42+
ctrl := module.NewController("test")
43+
44+
testQueue := queue.Inject(module, "test")
45+
ctrl.Post("", func(ctx core.Ctx) error {
46+
testQueue.AddJob(queue.AddJobOptions{
47+
Id: "1",
48+
Data: "value 1",
49+
})
50+
return ctx.JSON(core.Map{
51+
"data": "ok",
52+
})
53+
})
54+
55+
return ctrl
56+
}
57+
58+
appMoule := func() core.Module {
59+
return core.NewModule(core.NewModuleOptions{
60+
Imports: []core.Modules{
61+
queue.ForRootFactory(func(ref core.RefProvider) *queue.Options {
62+
return &queue.Options{
63+
Connect: &redis.Options{
64+
Addr: "localhost:6379",
65+
DB: 0,
66+
Password: "",
67+
},
68+
Workers: 6,
69+
RetryFailures: 3,
70+
}
71+
}),
72+
queue.Register("test"),
73+
},
74+
Controllers: []core.Controllers{controller},
75+
Providers: []core.Providers{service},
76+
})
77+
}
78+
79+
server := core.CreateFactory(appMoule)
80+
server.SetGlobalPrefix("api")
81+
82+
testServer := httptest.NewServer(server.PrepareBeforeListen())
83+
defer testServer.Close()
84+
85+
testClient := testServer.Client()
86+
resp, err := testClient.Post(testServer.URL+"/api/test", "application/json", nil)
87+
require.Nil(t, err)
88+
require.Equal(t, http.StatusOK, resp.StatusCode)
89+
90+
time.Sleep(1 * time.Second)
91+
}

‎queue.go‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/go-redsync/redsync/v4/redis/goredis/v9"
1414
"github.com/redis/go-redis/v9"
1515
"github.com/robfig/cron/v3"
16+
"github.com/tinh-tinh/tinhtinh/v2/common"
1617
)
1718

1819
type JobFnc func(job *Job)
@@ -362,15 +363,15 @@ func (q *Queue) Resume() {
362363

363364
func (q *Queue) RemoveCompleted() {
364365
if q.config.RemoveOnComplete {
365-
q.jobs = Remove(q.jobs, func(j Job) bool {
366+
q.jobs = common.Remove(q.jobs, func(j Job) bool {
366367
return j.Status == CompletedStatus
367368
})
368369
}
369370
}
370371

371372
func (q *Queue) RemoveFailed() {
372373
if q.config.RemoveOnFail {
373-
q.jobs = Remove(q.jobs, func(j Job) bool {
374+
q.jobs = common.Remove(q.jobs, func(j Job) bool {
374375
return j.Status == FailedStatus
375376
})
376377
}

‎utils.go‎

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)