mirror of
https://github.com/hibiken/asynq.git
synced 2024-12-25 23:32:17 +08:00
Change newRDB signature
This commit is contained in:
parent
e6b1230c36
commit
fab2dcb56e
@ -6,8 +6,6 @@ import (
|
|||||||
"os/signal"
|
"os/signal"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/go-redis/redis/v7"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Background is a top-level entity for the background-task processing.
|
// Background is a top-level entity for the background-task processing.
|
||||||
@ -21,12 +19,7 @@ type Background struct {
|
|||||||
|
|
||||||
// NewBackground returns a new Background instance.
|
// NewBackground returns a new Background instance.
|
||||||
func NewBackground(numWorkers int, opt *RedisOpt) *Background {
|
func NewBackground(numWorkers int, opt *RedisOpt) *Background {
|
||||||
client := redis.NewClient(&redis.Options{
|
rdb := newRDB(opt)
|
||||||
Addr: opt.Addr,
|
|
||||||
Password: opt.Password,
|
|
||||||
DB: opt.DB,
|
|
||||||
})
|
|
||||||
rdb := newRDB(client)
|
|
||||||
poller := newPoller(rdb, 5*time.Second, []string{scheduled, retry})
|
poller := newPoller(rdb, 5*time.Second, []string{scheduled, retry})
|
||||||
processor := newProcessor(rdb, numWorkers, nil)
|
processor := newProcessor(rdb, numWorkers, nil)
|
||||||
return &Background{
|
return &Background{
|
||||||
|
@ -3,7 +3,6 @@ package asynq
|
|||||||
import (
|
import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/go-redis/redis/v7"
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -14,12 +13,7 @@ type Client struct {
|
|||||||
|
|
||||||
// NewClient creates and returns a new client.
|
// NewClient creates and returns a new client.
|
||||||
func NewClient(opt *RedisOpt) *Client {
|
func NewClient(opt *RedisOpt) *Client {
|
||||||
client := redis.NewClient(&redis.Options{
|
return &Client{rdb: newRDB(opt)}
|
||||||
Addr: opt.Addr,
|
|
||||||
Password: opt.Password,
|
|
||||||
DB: opt.DB,
|
|
||||||
})
|
|
||||||
return &Client{rdb: newRDB(client)}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Process enqueues the task to be performed at a given time.
|
// Process enqueues the task to be performed at a given time.
|
||||||
|
7
rdb.go
7
rdb.go
@ -32,7 +32,12 @@ type rdb struct {
|
|||||||
client *redis.Client
|
client *redis.Client
|
||||||
}
|
}
|
||||||
|
|
||||||
func newRDB(client *redis.Client) *rdb {
|
func newRDB(opt *RedisOpt) *rdb {
|
||||||
|
client := redis.NewClient(&redis.Options{
|
||||||
|
Addr: opt.Addr,
|
||||||
|
Password: opt.Password,
|
||||||
|
DB: opt.DB,
|
||||||
|
})
|
||||||
return &rdb{client}
|
return &rdb{client}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
36
rdb_test.go
36
rdb_test.go
@ -12,8 +12,6 @@ import (
|
|||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
var client *redis.Client
|
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
rand.Seed(time.Now().UnixNano())
|
rand.Seed(time.Now().UnixNano())
|
||||||
}
|
}
|
||||||
@ -28,15 +26,15 @@ var sortStrOpt = cmp.Transformer("SortStr", func(in []string) []string {
|
|||||||
// before returning an instance of rdb.
|
// before returning an instance of rdb.
|
||||||
func setup(t *testing.T) *rdb {
|
func setup(t *testing.T) *rdb {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
client = redis.NewClient(&redis.Options{
|
r := newRDB(&RedisOpt{
|
||||||
Addr: "localhost:6379",
|
Addr: "localhost:6379",
|
||||||
DB: 15, // use database 15 to separate from other applications
|
DB: 15, // use database 15 to separate from other applications
|
||||||
})
|
})
|
||||||
// Start each test with a clean slate.
|
// Start each test with a clean slate.
|
||||||
if err := client.FlushDB().Err(); err != nil {
|
if err := r.client.FlushDB().Err(); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
return newRDB(client)
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
func randomTask(taskType, qname string, payload map[string]interface{}) *taskMessage {
|
func randomTask(taskType, qname string, payload map[string]interface{}) *taskMessage {
|
||||||
@ -63,19 +61,19 @@ func TestEnqueue(t *testing.T) {
|
|||||||
|
|
||||||
for _, tc := range tests {
|
for _, tc := range tests {
|
||||||
// clean up db before each test case.
|
// clean up db before each test case.
|
||||||
if err := client.FlushDB().Err(); err != nil {
|
if err := r.client.FlushDB().Err(); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
err := r.enqueue(tc.msg)
|
err := r.enqueue(tc.msg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
res := client.LRange(defaultQueue, 0, -1).Val()
|
res := r.client.LRange(defaultQueue, 0, -1).Val()
|
||||||
if len(res) != 1 {
|
if len(res) != 1 {
|
||||||
t.Errorf("LIST %q has length %d, want 1", defaultQueue, len(res))
|
t.Errorf("LIST %q has length %d, want 1", defaultQueue, len(res))
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if !client.SIsMember(allQueues, defaultQueue).Val() {
|
if !r.client.SIsMember(allQueues, defaultQueue).Val() {
|
||||||
t.Errorf("SISMEMBER %q %q = false, want true", allQueues, defaultQueue)
|
t.Errorf("SISMEMBER %q %q = false, want true", allQueues, defaultQueue)
|
||||||
}
|
}
|
||||||
var persisted taskMessage
|
var persisted taskMessage
|
||||||
@ -104,7 +102,7 @@ func TestDequeue(t *testing.T) {
|
|||||||
|
|
||||||
for _, tc := range tests {
|
for _, tc := range tests {
|
||||||
// clean up db before each test case.
|
// clean up db before each test case.
|
||||||
if err := client.FlushDB().Err(); err != nil {
|
if err := r.client.FlushDB().Err(); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
for _, m := range tc.queued {
|
for _, m := range tc.queued {
|
||||||
@ -116,7 +114,7 @@ func TestDequeue(t *testing.T) {
|
|||||||
defaultQueue, got, err, tc.want, tc.err)
|
defaultQueue, got, err, tc.want, tc.err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if l := client.LLen(inProgress).Val(); l != tc.inProgress {
|
if l := r.client.LLen(inProgress).Val(); l != tc.inProgress {
|
||||||
t.Errorf("LIST %q has length %d, want %d", inProgress, l, tc.inProgress)
|
t.Errorf("LIST %q has length %d, want %d", inProgress, l, tc.inProgress)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -168,17 +166,17 @@ func TestMoveAll(t *testing.T) {
|
|||||||
|
|
||||||
for _, tc := range tests {
|
for _, tc := range tests {
|
||||||
// clean up db before each test case.
|
// clean up db before each test case.
|
||||||
if err := client.FlushDB().Err(); err != nil {
|
if err := r.client.FlushDB().Err(); err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
// seed src list.
|
// seed src list.
|
||||||
for _, msg := range tc.beforeSrc {
|
for _, msg := range tc.beforeSrc {
|
||||||
client.LPush(inProgress, msg)
|
r.client.LPush(inProgress, msg)
|
||||||
}
|
}
|
||||||
// seed dst list.
|
// seed dst list.
|
||||||
for _, msg := range tc.beforeDst {
|
for _, msg := range tc.beforeDst {
|
||||||
client.LPush(defaultQueue, msg)
|
r.client.LPush(defaultQueue, msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := r.moveAll(inProgress, defaultQueue); err != nil {
|
if err := r.moveAll(inProgress, defaultQueue); err != nil {
|
||||||
@ -186,11 +184,11 @@ func TestMoveAll(t *testing.T) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
gotSrc := client.LRange(inProgress, 0, -1).Val()
|
gotSrc := r.client.LRange(inProgress, 0, -1).Val()
|
||||||
if diff := cmp.Diff(tc.afterSrc, gotSrc, sortStrOpt); diff != "" {
|
if diff := cmp.Diff(tc.afterSrc, gotSrc, sortStrOpt); diff != "" {
|
||||||
t.Errorf("mismatch found in %q (-want, +got)\n%s", inProgress, diff)
|
t.Errorf("mismatch found in %q (-want, +got)\n%s", inProgress, diff)
|
||||||
}
|
}
|
||||||
gotDst := client.LRange(defaultQueue, 0, -1).Val()
|
gotDst := r.client.LRange(defaultQueue, 0, -1).Val()
|
||||||
if diff := cmp.Diff(tc.afterDst, gotDst, sortStrOpt); diff != "" {
|
if diff := cmp.Diff(tc.afterDst, gotDst, sortStrOpt); diff != "" {
|
||||||
t.Errorf("mismatch found in %q (-want, +got)\n%s", defaultQueue, diff)
|
t.Errorf("mismatch found in %q (-want, +got)\n%s", defaultQueue, diff)
|
||||||
}
|
}
|
||||||
@ -242,10 +240,10 @@ func TestForward(t *testing.T) {
|
|||||||
|
|
||||||
for _, tc := range tests {
|
for _, tc := range tests {
|
||||||
// clean up db before each test case.
|
// clean up db before each test case.
|
||||||
if err := client.FlushDB().Err(); err != nil {
|
if err := r.client.FlushDB().Err(); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
if err := client.ZAdd(scheduled, tc.tasks...).Err(); err != nil {
|
if err := r.client.ZAdd(scheduled, tc.tasks...).Err(); err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@ -255,12 +253,12 @@ func TestForward(t *testing.T) {
|
|||||||
t.Errorf("(*rdb).forward(%q) = %v, want nil", scheduled, err)
|
t.Errorf("(*rdb).forward(%q) = %v, want nil", scheduled, err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
gotQueued := client.LRange(defaultQueue, 0, -1).Val()
|
gotQueued := r.client.LRange(defaultQueue, 0, -1).Val()
|
||||||
if diff := cmp.Diff(tc.wantQueued, gotQueued, sortStrOpt); diff != "" {
|
if diff := cmp.Diff(tc.wantQueued, gotQueued, sortStrOpt); diff != "" {
|
||||||
t.Errorf("%q has %d tasks, want %d tasks; (-want, +got)\n%s", defaultQueue, len(gotQueued), len(tc.wantQueued), diff)
|
t.Errorf("%q has %d tasks, want %d tasks; (-want, +got)\n%s", defaultQueue, len(gotQueued), len(tc.wantQueued), diff)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
gotScheduled := client.ZRangeByScore(scheduled, &redis.ZRangeBy{Min: "-inf", Max: "+inf"}).Val()
|
gotScheduled := r.client.ZRangeByScore(scheduled, &redis.ZRangeBy{Min: "-inf", Max: "+inf"}).Val()
|
||||||
if diff := cmp.Diff(tc.wantScheduled, gotScheduled, sortStrOpt); diff != "" {
|
if diff := cmp.Diff(tc.wantScheduled, gotScheduled, sortStrOpt); diff != "" {
|
||||||
t.Errorf("%q has %d tasks, want %d tasks; (-want, +got)\n%s", scheduled, len(gotScheduled), len(tc.wantScheduled), diff)
|
t.Errorf("%q has %d tasks, want %d tasks; (-want, +got)\n%s", scheduled, len(gotScheduled), len(tc.wantScheduled), diff)
|
||||||
continue
|
continue
|
||||||
|
Loading…
Reference in New Issue
Block a user