mirror of
https://github.com/hibiken/asynq.git
synced 2025-10-18 08:57:55 +08:00
feat: add publish and getpubsub for streaming
Signed-off-by: FogDong <fog@bentoml.com>
This commit is contained in:
@@ -752,4 +752,6 @@ type Broker interface {
|
||||
PublishCancelation(id string) error
|
||||
|
||||
WriteResult(qname, id string, data []byte) (n int, err error)
|
||||
|
||||
Publish(qname, id string, data []byte) (n int, err error)
|
||||
}
|
||||
|
@@ -1519,3 +1519,14 @@ func (r *RDB) WriteResult(qname, taskID string, data []byte) (int, error) {
|
||||
}
|
||||
return len(data), nil
|
||||
}
|
||||
|
||||
// Publish publishes the given task message to the specified channel.
|
||||
func (r *RDB) Publish(qname, taskID string, data []byte) (int, error) {
|
||||
var op errors.Op = "rdb.Publish"
|
||||
ctx := context.Background()
|
||||
taskKey := base.TaskKey(qname, taskID)
|
||||
if err := r.client.Publish(ctx, taskKey, data).Err(); err != nil {
|
||||
return 0, errors.E(op, errors.Unknown, &errors.RedisCommandError{Command: "publish", Err: err})
|
||||
}
|
||||
return len(data), nil
|
||||
}
|
||||
|
@@ -11,8 +11,8 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
"github.com/hibiken/asynq/internal/base"
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
var errRedisDown = errors.New("testutil: redis is down")
|
||||
@@ -217,6 +217,15 @@ func (tb *TestBroker) WriteResult(qname, id string, data []byte) (int, error) {
|
||||
return tb.real.WriteResult(qname, id, data)
|
||||
}
|
||||
|
||||
func (tb *TestBroker) Publish(qname, id string, data []byte) (int, error) {
|
||||
tb.mu.Lock()
|
||||
defer tb.mu.Unlock()
|
||||
if tb.sleeping {
|
||||
return 0, errRedisDown
|
||||
}
|
||||
return tb.real.Publish(qname, id, data)
|
||||
}
|
||||
|
||||
func (tb *TestBroker) Ping() error {
|
||||
tb.mu.Lock()
|
||||
defer tb.mu.Unlock()
|
||||
|
Reference in New Issue
Block a user