2
0
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:
FogDong
2023-09-07 15:04:16 +08:00
parent 6b98c0bbae
commit 40df09cb36
5 changed files with 49 additions and 8 deletions

View File

@@ -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)
}

View File

@@ -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
}

View File

@@ -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()