feat(eda): pause and resume a binding; resuming goes back through approval
This commit is contained in:
24 files changed
+1032
-542
No files matched your search
@@ -1,6 +1,7 @@
|
||||
package postgres_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -92,3 +93,59 @@ func assertUndispatched(t *testing.T, eda *postgres.EDA, want int) {
|
||||
t.Fatalf("undispatched captures = %d, want %d", len(got), want)
|
||||
}
|
||||
}
|
||||
|
||||
// A binding moves pending -> armed -> paused -> pending, and refuses any other
|
||||
// transition; a paused binding is offered no captures.
|
||||
func TestBindingLifecycle(t *testing.T) {
|
||||
ctx := t.Context()
|
||||
eda := pgstore.EDA(t, nil)
|
||||
eventTypeID, err := eda.InsertEventType(ctx, eventtype.EventType{Source: "forge", Name: "push"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
mappingID, err := eda.InsertMapping(ctx, mapping.Mapping{Name: "push", EventTypeID: eventTypeID})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
id, err := eda.InsertBinding(ctx, binding.Binding{
|
||||
Name: "on-push", Matcher: binding.Matcher{Source: "forge"}, MappingID: mappingID, Workflow: "tdd",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := eda.InsertCapture(ctx, storecontract.CapturedEvent{
|
||||
Source: "forge", Body: "{}", Authenticated: true, ReceivedAt: time.Now(),
|
||||
}, time.Hour, 100); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
steps := []struct {
|
||||
name string
|
||||
apply func(context.Context, string) error
|
||||
wantErr error
|
||||
status binding.Status
|
||||
offered int
|
||||
}{
|
||||
{"a pending binding cannot pause", eda.PauseBinding, storecontract.ErrBindingTransition, binding.StatusPendingApproval, 0},
|
||||
{"approve arms it", eda.ApproveBinding, nil, binding.StatusArmed, 1},
|
||||
{"an armed binding cannot resume", eda.ResumeBinding, storecontract.ErrBindingTransition, binding.StatusArmed, 1},
|
||||
{"pause stops it", eda.PauseBinding, nil, binding.StatusPaused, 0},
|
||||
{"a paused binding cannot be approved", eda.ApproveBinding, storecontract.ErrBindingTransition, binding.StatusPaused, 0},
|
||||
{"resume returns it to approval", eda.ResumeBinding, nil, binding.StatusPendingApproval, 0},
|
||||
}
|
||||
for _, step := range steps {
|
||||
if err := step.apply(ctx, id); !errors.Is(err, step.wantErr) {
|
||||
t.Fatalf("%s: err = %v, want %v", step.name, err, step.wantErr)
|
||||
}
|
||||
b, err := eda.GetBinding(ctx, id)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if b.Status != step.status {
|
||||
t.Fatalf("%s: status = %q, want %q", step.name, b.Status, step.status)
|
||||
}
|
||||
assertUndispatched(t, eda, step.offered)
|
||||
}
|
||||
if err := eda.PauseBinding(ctx, "missing"); !errors.Is(err, storecontract.ErrBindingNotFound) {
|
||||
t.Fatalf("pause of a missing binding = %v, want ErrBindingNotFound", err)
|
||||
}
|
||||
}
|
||||
@@ -482,6 +482,34 @@ func (s *EDA) ApproveBinding(ctx context.Context, id string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// PauseBinding stops an armed binding from dispatching.
|
||||
func (s *EDA) PauseBinding(ctx context.Context, id string) error {
|
||||
return s.transitionBinding(ctx, id, binding.StatusArmed, binding.StatusPaused, "pause")
|
||||
}
|
||||
|
||||
// ResumeBinding returns a paused binding to pending_approval: it fires again
|
||||
// only once approved.
|
||||
func (s *EDA) ResumeBinding(ctx context.Context, id string) error {
|
||||
return s.transitionBinding(ctx, id, binding.StatusPaused, binding.StatusPendingApproval, "resume")
|
||||
}
|
||||
|
||||
func (s *EDA) transitionBinding(ctx context.Context, id string, from, to binding.Status, verb string) error {
|
||||
n, err := s.q.TransitionBindingStatus(ctx, postgresdb.TransitionBindingStatusParams{
|
||||
ID: id, FromStatus: string(from), ToStatus: string(to),
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if n == 0 {
|
||||
if _, err := s.q.GetBinding(ctx, id); errors.Is(err, pgx.ErrNoRows) {
|
||||
return storecontract.ErrBindingNotFound
|
||||
}
|
||||
return storecontract.ErrBindingTransition
|
||||
}
|
||||
s.notifyWrite(events.KindBindingChanged, "binding", verb, id)
|
||||
return nil
|
||||
}
|
||||
|
||||
// --- dispatch ledgers ---
|
||||
|
||||
// RecordDispatch writes one at-most-once binding dispatch. binding_version is
|
||||
|
||||
@@ -1048,6 +1048,25 @@ func (q *Queries) TaskToolCalls(ctx context.Context, taskID int64) ([]TaskToolCa
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const transitionBindingStatus = `-- name: TransitionBindingStatus :execrows
|
||||
UPDATE bindings SET status = $1, updated_at = now()
|
||||
WHERE id = $2 AND status = $3
|
||||
`
|
||||
|
||||
type TransitionBindingStatusParams struct {
|
||||
ToStatus string
|
||||
ID string
|
||||
FromStatus string
|
||||
}
|
||||
|
||||
func (q *Queries) TransitionBindingStatus(ctx context.Context, arg TransitionBindingStatusParams) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, transitionBindingStatus, arg.ToStatus, arg.ID, arg.FromStatus)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
|
||||
const updateBinding = `-- name: UpdateBinding :execrows
|
||||
UPDATE bindings
|
||||
SET name = $2, source = $3, mapping = $4, filter = $5, workflow = $6, owner = $7, repo = $8,
|
||||
|
||||
@@ -84,6 +84,10 @@ WHERE id = $1;
|
||||
-- name: SetBindingArmed :execrows
|
||||
UPDATE bindings SET status = 'armed', updated_at = now() WHERE id = $1;
|
||||
|
||||
-- name: TransitionBindingStatus :execrows
|
||||
UPDATE bindings SET status = @to_status, updated_at = now()
|
||||
WHERE id = @id AND status = @from_status;
|
||||
|
||||
-- name: DeleteBinding :execrows
|
||||
DELETE FROM bindings WHERE id = $1;
|
||||
|
||||
|
||||
Reference in new issue
Block a user