File size: 1,375 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
package statusmgr
import (
"context"
"database/sql"
"errors"
"fmt"
"github.com/jackc/pgx/v5"
"github.com/riverqueue/river"
"github.com/target/goalert/gadb"
)
type LookForWorkArgs struct {
AlertID int64 `json:",omitempty"`
}
func (LookForWorkArgs) Kind() string { return "status-manager-look-for-work" }
// lookForWork is a worker function that will find any subscriptions that are out of date and need to be updated, and add them to the processing queue.
func (db *DB) lookForWork(ctx context.Context, j *river.Job[LookForWorkArgs]) error {
var outOfDate []int64
err := db.lock.WithTxShared(ctx, func(ctx context.Context, tx *sql.Tx) error {
var err error
outOfDate, err = gadb.New(tx).StatusMgrOutdated(ctx, sql.NullInt64{Int64: j.Args.AlertID, Valid: j.Args.AlertID != 0})
return err
})
if errors.Is(err, sql.ErrNoRows) {
return nil
}
if err != nil {
return err
}
if len(outOfDate) == 0 {
return nil
}
params := make([]river.InsertManyParams, len(outOfDate))
for i, id := range outOfDate {
params[i] = river.InsertManyParams{
Args: ProcessArgs{SubscriptionID: id},
InsertOpts: &river.InsertOpts{
Queue: QueueName,
Priority: PriorityProcess,
},
}
}
r := river.ClientFromContext[pgx.Tx](ctx)
_, err = r.InsertManyFast(ctx, params)
if err != nil {
return fmt.Errorf("insert many: %w", err)
}
return nil
}
|