| package rotationmanager |
|
|
| import ( |
| "context" |
| "database/sql" |
| "errors" |
| "fmt" |
| "time" |
|
|
| "github.com/google/uuid" |
| "github.com/riverqueue/river" |
| "github.com/target/goalert/gadb" |
| "github.com/target/goalert/schedule/rotation" |
| "github.com/target/goalert/util" |
| ) |
|
|
| type UpdateArgs struct { |
| RotationID uuid.UUID |
| } |
|
|
| func (UpdateArgs) Kind() string { return "rotation-manager-update" } |
|
|
| |
| func (db *DB) updateRotation(ctx context.Context, j *river.Job[UpdateArgs]) error { |
| return db.lock.WithTxShared(ctx, func(ctx context.Context, tx *sql.Tx) error { |
| g := gadb.New(tx) |
|
|
| row, err := g.RotMgrRotationData(ctx, j.Args.RotationID) |
| if errors.Is(err, sql.ErrNoRows) { |
| |
| return nil |
| } |
| if err != nil { |
| return fmt.Errorf("load rotation data: %w", err) |
| } |
|
|
| if len(row.Participants) == 0 { |
| if row.StateVersion != 0 { |
| |
| err = g.RotMgrEnd(ctx, j.Args.RotationID) |
| if err != nil { |
| return fmt.Errorf("end rotation: %w", err) |
| } |
| return nil |
| } |
|
|
| return nil |
| } |
|
|
| loc, err := util.LoadLocation(row.Rotation.TimeZone) |
| if err != nil { |
| return fmt.Errorf("load location: %w", err) |
| } |
|
|
| r := rotation.Rotation{ |
| Type: rotation.Type(row.Rotation.Type), |
| Start: row.Rotation.StartTime.In(loc), |
| ShiftLength: int(row.Rotation.ShiftLength), |
| } |
|
|
| |
| _, err = db.riverDBSQL.InsertTx(ctx, tx, UpdateArgs{RotationID: j.Args.RotationID}, &river.InsertOpts{ |
| UniqueOpts: river.UniqueOpts{ |
| ByArgs: true, |
| ByPeriod: time.Minute, |
| }, |
| Priority: PriorityScheduled, |
| ScheduledAt: r.EndTime(row.Now), |
| Queue: QueueName, |
| }) |
| if err != nil { |
| return fmt.Errorf("schedule next run: %w", err) |
| } |
|
|
| if row.StateVersion == 0 { |
| |
| err = g.RotMgrStart(ctx, j.Args.RotationID) |
| if err != nil { |
| return fmt.Errorf("start rotation: %w", err) |
| } |
| return nil |
| } |
|
|
| s := rotState{ |
| ShiftStart: row.StateShiftStart.Time.In(loc), |
| Position: int(row.StatePosition), |
| Version: int(row.StateVersion), |
| } |
| adv, err := calcAdvance(ctx, row.Now, &r, s, len(row.Participants)) |
| if err != nil { |
| return fmt.Errorf("calc advance: %w", err) |
| } |
| if adv == nil { |
| |
| return nil |
| } |
|
|
| err = g.RotMgrUpdate(ctx, gadb.RotMgrUpdateParams{ |
| RotationID: j.Args.RotationID, |
| Position: int32(adv.newPosition), |
| RotationParticipantID: row.Participants[adv.newPosition], |
| }) |
| if err != nil { |
| return fmt.Errorf("update rotation state (advance): %w", err) |
| } |
|
|
| return nil |
| }) |
| } |
|
|