diff --git a/internal/infrastructure/postgres/postgresdb/tasks.sql.go b/internal/infrastructure/postgres/postgresdb/tasks.sql.go index 9e0106a59..4602d4672 100644 --- a/internal/infrastructure/postgres/postgresdb/tasks.sql.go +++ b/internal/infrastructure/postgres/postgresdb/tasks.sql.go @@ -866,6 +866,23 @@ func (q *Queries) LockTaskStatus(ctx context.Context, id int64) (string, error) return status, err } +const markBlockerChanged = `-- name: MarkBlockerChanged :execrows +UPDATE tasks +SET park_reason = park_reason || ' [blocker changed]', updated_at = now() +WHERE status = 'parked' AND park_blocker = ANY($1::text[]) + AND park_reason NOT LIKE '%[blocker changed]' +` + +// A parked task that names one of these blockers is flagged once, so the +// operator sees the resource it waited on was edited; retry stays theirs. +func (q *Queries) MarkBlockerChanged(ctx context.Context, blockers []string) (int64, error) { + result, err := q.db.Exec(ctx, markBlockerChanged, blockers) + if err != nil { + return 0, err + } + return result.RowsAffected(), nil +} + const markHandedOff = `-- name: MarkHandedOff :execrows UPDATE tasks SET handoff_to_task_id = $1, updated_at = now() WHERE id = $2 AND handoff_to_task_id IS NULL diff --git a/internal/infrastructure/postgres/queries/tasks.sql b/internal/infrastructure/postgres/queries/tasks.sql index e6e3ccc5e..769c263ad 100644 --- a/internal/infrastructure/postgres/queries/tasks.sql +++ b/internal/infrastructure/postgres/queries/tasks.sql @@ -330,3 +330,11 @@ SELECT sqlc.arg(successor_id), t.owner, t.repo, t.issue_number, t.title, t.body, t.binding_addr, t.origin, t.call_parent_task_id, t.call_depth, t.id FROM tasks t WHERE t.id = sqlc.arg(id) RETURNING *; + +-- name: MarkBlockerChanged :execrows +-- A parked task that names one of these blockers is flagged once, so the +-- operator sees the resource it waited on was edited; retry stays theirs. +UPDATE tasks +SET park_reason = park_reason || ' [blocker changed]', updated_at = now() +WHERE status = 'parked' AND park_blocker = ANY(sqlc.arg(blockers)::text[]) + AND park_reason NOT LIKE '%[blocker changed]'; diff --git a/internal/infrastructure/postgres/resources.go b/internal/infrastructure/postgres/resources.go index d8c3594f4..34b7a7603 100644 --- a/internal/infrastructure/postgres/resources.go +++ b/internal/infrastructure/postgres/resources.go @@ -12,6 +12,7 @@ import ( "github.com/samcharles93/archie-core/internal/domain/storecontract" "github.com/samcharles93/archie-core/internal/infrastructure/postgres/postgresdb" + "github.com/samcharles93/archie-core/internal/taskstate" ) // Resources is the PostgreSQL implementation of the State Store's @@ -85,6 +86,26 @@ func checkResourceKey(orgID, kind string) error { return nil } +// blockersFixedBy names the park blockers an edit of a resource kind may have +// cleared. +func blockersFixedBy(kind string) []string { + switch kind { + case "forge-connections": + return []string{taskstate.BlockerConnectionPermit, taskstate.BlockerConnectionCredential, taskstate.BlockerForgeExtension, taskstate.BlockerRepository} + case "identity-grants": + return []string{taskstate.BlockerConnectionPermit, taskstate.BlockerAgentAssignment} + case "repository-policies": + return []string{taskstate.BlockerRepository} + case "credential-bindings": + return []string{taskstate.BlockerConnectionCredential, taskstate.BlockerModelCredential} + case "provider-settings": + return []string{taskstate.BlockerModelCredential} + case "workflow-enablement": + return []string{taskstate.BlockerWorkflowDisabled} + } + return nil +} + // PutResource applies one control-plane resource write, idempotent per // request ID. func (s *Resources) PutResource(ctx context.Context, write storecontract.ResourceWrite) (_ storecontract.Resource, retErr error) { @@ -125,6 +146,11 @@ func (s *Resources) PutResource(ctx context.Context, write storecontract.Resourc if err != nil { return storecontract.Resource{}, err } + if blockers := blockersFixedBy(write.Kind); len(blockers) > 0 { + if _, err := queries.MarkBlockerChanged(ctx, blockers); err != nil { + return storecontract.Resource{}, err + } + } if err := tx.Commit(ctx); err != nil { return storecontract.Resource{}, err }