2025-02-12 15:51:55 +01:00
|
|
|
package queue
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2025-09-01 08:21:10 +03:00
|
|
|
"database/sql"
|
2025-02-12 15:51:55 +01:00
|
|
|
|
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"
|
2025-09-01 08:21:10 +03:00
|
|
|
"github.com/riverqueue/river/riverdriver/riverdatabasesql"
|
2025-06-12 13:03:25 -04:00
|
|
|
"github.com/riverqueue/river/rivertype"
|
|
|
|
|
"github.com/riverqueue/rivercontrib/otelriver"
|
2025-07-02 07:57:41 -04:00
|
|
|
"github.com/robfig/cron/v3"
|
2025-02-27 11:49:12 +01:00
|
|
|
"github.com/zitadel/logging"
|
2026-01-12 06:51:39 +01:00
|
|
|
"go.opentelemetry.io/otel"
|
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 {
|
2025-09-01 08:21:10 +03:00
|
|
|
driver riverdriver.Driver[*sql.Tx]
|
|
|
|
|
client *river.Client[*sql.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-06-12 13:03:25 -04:00
|
|
|
middleware := []rivertype.Middleware{otelriver.NewMiddleware(&otelriver.MiddlewareConfig{
|
2026-01-12 06:51:39 +01:00
|
|
|
MeterProvider: otel.GetMeterProvider(),
|
2025-09-01 06:31:18 -04:00
|
|
|
DurationUnit: "ms",
|
2025-06-12 13:03:25 -04:00
|
|
|
})}
|
2025-02-27 11:49:12 +01:00
|
|
|
return &Queue{
|
2025-09-01 08:21:10 +03:00
|
|
|
driver: riverdatabasesql.New(config.Client.DB),
|
2025-02-27 11:49:12 +01:00
|
|
|
config: &river.Config{
|
|
|
|
|
Workers: river.NewWorkers(),
|
|
|
|
|
Queues: make(map[string]river.QueueConfig),
|
|
|
|
|
JobTimeout: -1,
|
2025-06-12 13:03:25 -04:00
|
|
|
Middleware: middleware,
|
2025-07-29 09:09:00 +02:00
|
|
|
Schema: schema,
|
2025-02-27 11:49:12 +01:00
|
|
|
},
|
|
|
|
|
}, 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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-07-02 07:57:41 -04:00
|
|
|
func (q *Queue) AddPeriodicJob(schedule cron.Schedule, jobArgs river.JobArgs, opts ...InsertOpt) (handle rivertype.PeriodicJobHandle) {
|
|
|
|
|
if q == nil {
|
|
|
|
|
logging.Info("skip adding periodic job because queue is not set")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
options := new(river.InsertOpts)
|
|
|
|
|
for _, opt := range opts {
|
|
|
|
|
opt(options)
|
|
|
|
|
}
|
|
|
|
|
return q.client.PeriodicJobs().Add(
|
|
|
|
|
river.NewPeriodicJob(
|
|
|
|
|
schedule,
|
|
|
|
|
func() (river.JobArgs, *river.InsertOpts) {
|
|
|
|
|
return jobArgs, options
|
|
|
|
|
},
|
|
|
|
|
nil,
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
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 {
|
2025-09-01 08:21:10 +03:00
|
|
|
_, err := q.client.Insert(ctx, args, applyInsertOpts(opts))
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// InsertManyFastTx wraps [river.Client.InsertManyFastTx] to insert all jobs in
|
|
|
|
|
// a single `COPY FROM` execution, within the existing transaction.
|
|
|
|
|
//
|
|
|
|
|
// Opts are applied to each job before sending them to river.
|
|
|
|
|
func (q *Queue) InsertManyFastTx(ctx context.Context, tx *sql.Tx, args []river.JobArgs, opts ...InsertOpt) error {
|
|
|
|
|
params := make([]river.InsertManyParams, len(args))
|
|
|
|
|
for i, arg := range args {
|
|
|
|
|
params[i] = river.InsertManyParams{
|
|
|
|
|
Args: arg,
|
|
|
|
|
InsertOpts: applyInsertOpts(opts),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_, err := q.client.InsertManyFastTx(ctx, tx, params)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func applyInsertOpts(opts []InsertOpt) *river.InsertOpts {
|
2025-02-27 11:49:12 +01:00
|
|
|
options := new(river.InsertOpts)
|
|
|
|
|
for _, opt := range opts {
|
|
|
|
|
opt(options)
|
|
|
|
|
}
|
2025-09-01 08:21:10 +03:00
|
|
|
return options
|
2025-02-12 15:51:55 +01:00
|
|
|
}
|
2025-02-27 11:49:12 +01:00
|
|
|
|
|
|
|
|
type Worker interface {
|
|
|
|
|
Register(workers *river.Workers, queues map[string]river.QueueConfig)
|
|
|
|
|
}
|