File size: 1,399 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
58
59
60
61
62
63
64
package rotationmanager

import (
	"context"
	"database/sql"
	"errors"
	"fmt"
	"time"

	"github.com/riverqueue/river"
	"github.com/target/goalert/gadb"
)

type LookForWorkArgs struct{}

func (LookForWorkArgs) Kind() string { return "rotation-manager-lfw" }

// lookForWork will schedule jobs for rotations in the entity_updates table.
func (db *DB) lookForWork(ctx context.Context, j *river.Job[LookForWorkArgs]) error {
	var hadWork bool
	err := db.lock.WithTxShared(ctx, func(ctx context.Context, tx *sql.Tx) error {
		g := gadb.New(tx)

		rotations, err := g.RotMgrFindWork(ctx)
		if errors.Is(err, sql.ErrNoRows) {
			// done, no more work
			return nil
		}
		if err != nil {
			return fmt.Errorf("find work: %w", err)
		}
		if len(rotations) == 0 {
			return nil
		}

		var params []river.InsertManyParams
		for _, r := range rotations {
			params = append(params, river.InsertManyParams{
				Args: UpdateArgs{RotationID: r},
				InsertOpts: &river.InsertOpts{
					Queue:    QueueName,
					Priority: PriorityEvent,
				},
			})
		}

		_, err = db.riverDBSQL.InsertManyFastTx(ctx, tx, params)
		if err != nil {
			return fmt.Errorf("insert many: %w", err)
		}
		hadWork = true
		return nil
	})
	if err != nil {
		return fmt.Errorf("look for work: %w", err)
	}
	if !hadWork {
		return nil
	}

	// There was work to do, so wait a bit before looking again.
	return river.JobSnooze(time.Second)
}