Commit b8b39edb authored by Andreas Bigger's avatar Andreas Bigger

don't schedule updates for completed games

parent b3c77b0a
......@@ -114,6 +114,10 @@ func NewGamePlayer(
}, nil
}
func (g *GamePlayer) Status() gameTypes.GameStatus {
return g.status
}
func (g *GamePlayer) ProgressGame(ctx context.Context) gameTypes.GameStatus {
if g.status != gameTypes.GameStatusInProgress {
// Game is already complete so don't try to perform further actions.
......
......@@ -64,8 +64,12 @@ func (c *coordinator) schedule(ctx context.Context, games []common.Address) erro
if j, err := c.createJob(addr); err != nil {
errs = append(errs, err)
} else if j != nil {
jobs = append(jobs, *j)
c.m.RecordGameUpdateScheduled()
if j.status == types.GameStatusInProgress {
jobs = append(jobs, *j)
c.m.RecordGameUpdateScheduled()
} else {
c.logger.Warn("Resolved game update not scheduled", "game", addr, "status", j.status)
}
}
state, ok := c.states[addr]
if ok {
......@@ -93,6 +97,7 @@ func (c *coordinator) schedule(ctx context.Context, games []common.Address) erro
// createJob updates the state for the specified game and returns the job to enqueue for it, if any
// Returns (nil, nil) when there is no error and no job to enqueue
func (c *coordinator) createJob(game common.Address) (*job, error) {
j := &job{addr: game}
state, ok := c.states[game]
if !ok {
state = &gameState{}
......@@ -109,9 +114,12 @@ func (c *coordinator) createJob(game common.Address) (*job, error) {
return nil, fmt.Errorf("failed to create game player: %w", err)
}
state.player = player
state.status = player.Status()
}
state.inflight = true
return &job{addr: game, player: state.player}, nil
j.player = state.player
j.status = state.status
return j, nil
}
func (c *coordinator) enqueueJob(ctx context.Context, j job) error {
......
......@@ -150,7 +150,10 @@ func TestDeleteDataForResolvedGames(t *testing.T) {
gameAddrs := []common.Address{gameAddr1, gameAddr2, gameAddr3}
require.NoError(t, c.schedule(ctx, gameAddrs))
require.Len(t, workQueue, len(gameAddrs), "should schedule all games")
// The work queue should only contain jobs for games 1 and 2
// A resolved game should not be scheduled for an update.
// This makes the inflight game metric more robust.
require.Len(t, workQueue, 2, "should schedule all games")
// Game 1 progresses and is still in progress
// Game 2 progresses and is now resolved
......@@ -249,6 +252,10 @@ func (g *stubGame) ProgressGame(_ context.Context) types.GameStatus {
return g.status
}
func (g *stubGame) Status() types.GameStatus {
return g.status
}
type createdGames struct {
t *testing.T
createCompleted common.Address
......
......@@ -10,6 +10,7 @@ import (
type GamePlayer interface {
ProgressGame(ctx context.Context) types.GameStatus
Status() types.GameStatus
}
type DiskManager interface {
......
......@@ -47,6 +47,10 @@ func (s *stubPlayer) ProgressGame(ctx context.Context) types.GameStatus {
return s.status
}
func (s *stubPlayer) Status() types.GameStatus {
return s.status
}
func readWithTimeout[T any](t *testing.T, ch <-chan T) T {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment