2
0
mirror of https://github.com/hibiken/asynq.git synced 2025-10-24 10:36:12 +08:00

adds mongo broker first commit

adds support for Mongo as a broker with support for enqueue, dequeue, schedules,
retries and retention; task cleanup via ttl indexes
This commit is contained in:
Vlad Fratila
2022-11-20 23:18:11 +02:00
parent c08f142b56
commit 721561ddd8
5 changed files with 595 additions and 43 deletions

View File

@@ -14,7 +14,10 @@ import (
"github.com/google/uuid"
"github.com/hibiken/asynq/internal/base"
"github.com/hibiken/asynq/internal/errors"
"github.com/hibiken/asynq/internal/mongobroker"
"github.com/hibiken/asynq/internal/rdb"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
// A Client is responsible for scheduling tasks.
@@ -36,6 +39,25 @@ func NewClient(r RedisConnOpt) *Client {
return &Client{broker: rdb.NewRDB(c)}
}
// NewClient returns a new Client instance given a redis connection option.
func NewMongoClient(opts *options.ClientOptions, db string) *Client {
if opts == nil {
opts = &options.ClientOptions{}
}
c, err := mongo.NewClient(opts)
if err != nil {
panic(fmt.Sprintf("asynq: could not create mongo client %s %v", err, opts))
}
ctx := context.Background()
err = c.Connect(ctx)
if err != nil {
panic(fmt.Sprintf("asynq: could not connect to mongo %s %v", err, opts))
}
mgb := mongobroker.NewBroker(c, db, &options.DatabaseOptions{}, ctx)
return &Client{broker: mgb}
}
type OptionType int
const (
@@ -150,9 +172,9 @@ func (t deadlineOption) Value() interface{} { return time.Time(t) }
// TTL duration must be greater than or equal to 1 second.
//
// Uniqueness of a task is based on the following properties:
// - Task Type
// - Task Payload
// - Queue Name
// - Task Type
// - Task Payload
// - Queue Name
func Unique(ttl time.Duration) Option {
return uniqueOption(ttl)
}