Skip to content

Commit

Permalink
refactor(rotation): enhance calcAdvance to handle multiple rotation v…
Browse files Browse the repository at this point in the history
…ersions and return error for unknown versions
  • Loading branch information
mastercactapus committed Feb 10, 2025
1 parent cb245d7 commit 13933da
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
15 changes: 10 additions & 5 deletions engine/rotationmanager/advance.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,20 @@ type rotState struct {
}

// calcAdvance will calculate rotation advancement if it is required. If not, nil is returned
func calcAdvance(ctx context.Context, t time.Time, rot *rotation.Rotation, state rotState, partCount int) *advance {
func calcAdvance(ctx context.Context, t time.Time, rot *rotation.Rotation, state rotState, partCount int) (*advance, error) {
var mustUpdate bool
origPos := state.Position

// get next shift start time
newStart := rot.EndTime(state.ShiftStart)
if state.Version == 1 {
switch state.Version {
case 1:
newStart = calcVersion1EndTime(rot, state.ShiftStart)
mustUpdate = true
case 2:
// no-op
default:
return nil, fmt.Errorf("unknown rotation version (supported: 1,2): %d", state.Version)
}

if state.Position >= partCount {
Expand All @@ -49,10 +54,10 @@ func calcAdvance(ctx context.Context, t time.Time, rot *rotation.Rotation, state
// If migrating from version 1 to 2 without changing
// who's on-call do so silently.
silent: state.Version == 1 && state.Position == origPos,
}
}, nil
}
// in the future, so nothing to do yet
return nil
return nil, nil
}

if !newStart.After(t.Add(-15 * time.Minute)) {
Expand All @@ -79,5 +84,5 @@ func calcAdvance(ctx context.Context, t time.Time, rot *rotation.Rotation, state
return &advance{
id: rot.ID,
newPosition: state.Position,
}
}, nil
}
5 changes: 4 additions & 1 deletion engine/rotationmanager/updaterotation.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ func (db *DB) updateRotation(ctx context.Context, j *river.Job[UpdateArgs]) erro
Position: int(row.Position.Int32),
Version: int(row.Version.Int32),
}
adv := calcAdvance(ctx, row.Now, &r, s, len(row.Participants))
adv, err := calcAdvance(ctx, row.Now, &r, s, len(row.Participants))
if err != nil {
return fmt.Errorf("calc advance: %w", err)
}
if adv == nil {
// no advancement needed
return nil
Expand Down

0 comments on commit 13933da

Please sign in to comment.