2025-02-12 15:51:55 +01:00
|
|
|
package queue
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/jackc/pgx/v5"
|
2025-02-27 11:49:12 +01:00
|
|
|
"github.com/riverqueue/river"
|
2025-02-12 15:51:55 +01:00
|
|
|
"github.com/riverqueue/river/riverdriver"
|
|
|
|
"github.com/riverqueue/river/riverdriver/riverpgxv5"
|
2025-02-27 11:49:12 +01:00
|
|
|
"github.com/zitadel/logging"
|
2025-02-12 15:51:55 +01:00
|
|
|
|
|
|
|
"github.com/zitadel/zitadel/internal/database"
|
|
|
|
)
|
|
|
|
|
2025-02-27 11:49:12 +01:00
|
|
|
// Queue abstracts the underlying queuing library
|
|
|
|
// For more information see github.com/riverqueue/river
|
|
|
|
type Queue struct {
|
|
|
|
driver riverdriver.Driver[pgx.Tx]
|
|
|
|
client *river.Client[pgx.Tx]
|
2025-02-12 15:51:55 +01:00
|
|
|
|
2025-02-27 11:49:12 +01:00
|
|
|
config *river.Config
|
|
|
|
shouldStart bool
|
|
|
|
}
|
2025-02-12 15:51:55 +01:00
|
|
|
|
2025-02-27 11:49:12 +01:00
|
|
|
type Config struct {
|
|
|
|
Client *database.DB `mapstructure:"-"` // mapstructure is needed if we would like to use viper to configure the queue
|
|
|
|
}
|
2025-02-12 15:51:55 +01:00
|
|
|
|
2025-02-27 11:49:12 +01:00
|
|
|
func NewQueue(config *Config) (_ *Queue, err error) {
|
2025-03-06 07:26:33 +01:00
|
|
|
if config.Client.Type() == "cockroach" {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2025-02-27 11:49:12 +01:00
|
|
|
return &Queue{
|
|
|
|
driver: riverpgxv5.New(config.Client.Pool),
|
|
|
|
config: &river.Config{
|
|
|
|
Workers: river.NewWorkers(),
|
|
|
|
Queues: make(map[string]river.QueueConfig),
|
|
|
|
JobTimeout: -1,
|
|
|
|
},
|
|
|
|
}, nil
|
2025-02-12 15:51:55 +01:00
|
|
|
}
|
|
|
|
|
2025-02-27 11:49:12 +01:00
|
|
|
func (q *Queue) ShouldStart() {
|
|
|
|
if q == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
q.shouldStart = true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queue) Start(ctx context.Context) (err error) {
|
|
|
|
if q == nil || !q.shouldStart {
|
2025-02-12 15:51:55 +01:00
|
|
|
return nil
|
2025-02-27 11:49:12 +01:00
|
|
|
}
|
|
|
|
ctx = WithQueue(ctx)
|
|
|
|
|
|
|
|
q.client, err = river.NewClient(q.driver, q.config)
|
|
|
|
if err != nil {
|
2025-02-12 15:51:55 +01:00
|
|
|
return err
|
2025-02-27 11:49:12 +01:00
|
|
|
}
|
2025-02-12 15:51:55 +01:00
|
|
|
|
2025-02-27 11:49:12 +01:00
|
|
|
return q.client.Start(ctx)
|
2025-02-12 15:51:55 +01:00
|
|
|
}
|
|
|
|
|
2025-02-27 11:49:12 +01:00
|
|
|
func (q *Queue) AddWorkers(w ...Worker) {
|
|
|
|
if q == nil {
|
|
|
|
logging.Info("skip adding workers because queue is not set")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, worker := range w {
|
|
|
|
worker.Register(q.config.Workers, q.config.Queues)
|
|
|
|
}
|
2025-02-12 15:51:55 +01:00
|
|
|
}
|
|
|
|
|
2025-02-27 11:49:12 +01:00
|
|
|
type InsertOpt func(*river.InsertOpts)
|
|
|
|
|
|
|
|
func WithMaxAttempts(maxAttempts uint8) InsertOpt {
|
|
|
|
return func(opts *river.InsertOpts) {
|
|
|
|
opts.MaxAttempts = int(maxAttempts)
|
2025-02-12 15:51:55 +01:00
|
|
|
}
|
2025-02-27 11:49:12 +01:00
|
|
|
}
|
2025-02-12 15:51:55 +01:00
|
|
|
|
2025-02-27 11:49:12 +01:00
|
|
|
func WithQueueName(name string) InsertOpt {
|
|
|
|
return func(opts *river.InsertOpts) {
|
|
|
|
opts.Queue = name
|
2025-02-12 15:51:55 +01:00
|
|
|
}
|
2025-02-27 11:49:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queue) Insert(ctx context.Context, args river.JobArgs, opts ...InsertOpt) error {
|
|
|
|
options := new(river.InsertOpts)
|
2025-02-12 15:51:55 +01:00
|
|
|
ctx = WithQueue(ctx)
|
2025-02-27 11:49:12 +01:00
|
|
|
for _, opt := range opts {
|
|
|
|
opt(options)
|
|
|
|
}
|
|
|
|
_, err := q.client.Insert(ctx, args, options)
|
2025-02-12 15:51:55 +01:00
|
|
|
return err
|
|
|
|
}
|
2025-02-27 11:49:12 +01:00
|
|
|
|
|
|
|
type Worker interface {
|
|
|
|
Register(workers *river.Workers, queues map[string]river.QueueConfig)
|
|
|
|
}
|