Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: don't unnecessarily restart scheduler #689

Merged
merged 1 commit into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion internal/scheduler/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"

"github.com/go-logr/logr"
"github.com/leg100/otf/internal"
otfrun "github.com/leg100/otf/internal/run"
"github.com/leg100/otf/internal/workspace"
)
Expand Down Expand Up @@ -82,7 +83,18 @@ func (q *queue) handleRun(ctx context.Context, run *otfrun.Run) error {
// no current run & queue is empty; unlock workspace
q.current = nil
ws, err := q.Unlock(ctx, q.ws.ID, &run.ID, false)
if err != nil {
if errors.Is(err, internal.ErrResourceNotFound) {
// Workspace not found occurs when the workspace has been
// deleted and the scheduler hasn't yet processed the
// corresponding "workspace deleted" event. In which there
// is nothing to unlock and the scheduler can continue as
// normal.
return nil
} else if err != nil {
// Any other error is treated as a transient or unexpected
// error, so propagate the error which'll notify the user
// via the logs and trigger the scheduler to be restarted
// with a backoff-and-retry.
return err
}
q.ws = ws
Expand Down
8 changes: 6 additions & 2 deletions internal/scheduler/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,12 @@ func (s *scheduler) handleRunEvent(ctx context.Context, event pubsub.Event[*run.
}
q, ok := s.queues[event.Payload.WorkspaceID]
if !ok {
// should never happen
s.Error(nil, "workspace queue does not exist for run event", "workspace", event.Payload.WorkspaceID, "run", event.Payload.ID, "event", event.Type)
// No queue exists for the workspace because the workspace has
// since been deleted, which can occur when run events arrive *after*
// the "workspace deleted" event, which is entirely possible with the
// way the scheduler processes events.
//
// In this case no action need be taken on the run.
return nil
}
if err := q.handleRun(ctx, event.Payload); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions internal/workspace/lock_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ func (db *pgdb) toggleLock(ctx context.Context, workspaceID string, togglefn fun
}
}
if err := q.UpdateWorkspaceLockByID(ctx, params); err != nil {
return sql.Error(err)
return err
}
return nil
})
return ws, err
return ws, sql.Error(err)
}
Loading