feat(eda): link binding dispatches to the captures and tasks they started
This commit is contained in:
24 files changed
+1315
-518
No files matched your search
@@ -14,7 +14,8 @@ import (
|
||||
)
|
||||
|
||||
// A capture a binding evaluated without dispatching is recorded once and not
|
||||
// offered to that binding again; a later dispatch of the pair is refused.
|
||||
// offered to that binding again; a later dispatch of the pair is refused. A
|
||||
// dispatch records the task it started, and the ledger lists by any end.
|
||||
func TestEvaluatedCaptureIsNotReoffered(t *testing.T) {
|
||||
ctx := t.Context()
|
||||
eda := pgstore.EDA(t, nil)
|
||||
@@ -51,6 +52,34 @@ func TestEvaluatedCaptureIsNotReoffered(t *testing.T) {
|
||||
if err := eda.RecordDispatch(ctx, bindingID, 1, captureID, 0, ""); !errors.Is(err, storecontract.ErrAlreadyDispatched) {
|
||||
t.Fatalf("dispatch after a filtered evaluation = %v, want ErrAlreadyDispatched", err)
|
||||
}
|
||||
|
||||
// A second capture dispatches; the ledger links it to the task it started.
|
||||
dispatched, err := eda.InsertCapture(ctx, storecontract.CapturedEvent{
|
||||
Source: "forge", Body: "{}", Authenticated: true, ReceivedAt: time.Now(),
|
||||
}, time.Hour, 100)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := eda.RecordDispatch(ctx, bindingID, 1, dispatched, 0, ""); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := eda.SetDispatchTask(ctx, bindingID, dispatched, 42); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
rows, err := eda.ListDispatches(ctx, storecontract.DispatchFilter{TaskID: 42, Limit: 10})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(rows) != 1 || rows[0].CaptureID != dispatched || rows[0].BindingID != bindingID {
|
||||
t.Fatalf("dispatches for task 42 = %+v, want the dispatched capture", rows)
|
||||
}
|
||||
all, err := eda.ListDispatches(ctx, storecontract.DispatchFilter{BindingID: bindingID, Limit: 10})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(all) != 2 {
|
||||
t.Fatalf("dispatches for the binding = %d, want 2", len(all))
|
||||
}
|
||||
}
|
||||
|
||||
func assertUndispatched(t *testing.T, eda *postgres.EDA, want int) {
|
||||
|
||||
@@ -13,6 +13,7 @@ import (
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/binding"
|
||||
@@ -498,6 +499,41 @@ func (s *EDA) RecordDispatch(ctx context.Context, bindingID string, bindingVersi
|
||||
return ledgerWrite(err, "edastore: record dispatch")
|
||||
}
|
||||
|
||||
// SetDispatchTask records the task a claimed dispatch started.
|
||||
func (s *EDA) SetDispatchTask(ctx context.Context, bindingID, captureID string, taskID int64) error {
|
||||
err := s.q.SetBindingDispatchTask(ctx, postgresdb.SetBindingDispatchTaskParams{Binding: bindingID, Capture: captureID, TaskID: taskID})
|
||||
if err != nil {
|
||||
return fmt.Errorf("edastore: set dispatch task: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ListDispatches returns the newest ledger rows matching filter.
|
||||
func (s *EDA) ListDispatches(ctx context.Context, filter storecontract.DispatchFilter) ([]storecontract.Dispatch, error) {
|
||||
params := postgresdb.ListBindingDispatchesParams{EntryLimit: int32(min(max(filter.Limit, 1), 500))} //nolint:gosec // clamped
|
||||
if filter.BindingID != "" {
|
||||
params.Binding = pgtype.Text{String: filter.BindingID, Valid: true}
|
||||
}
|
||||
if filter.CaptureID != "" {
|
||||
params.Capture = pgtype.Text{String: filter.CaptureID, Valid: true}
|
||||
}
|
||||
if filter.TaskID != 0 {
|
||||
params.TaskID = pgtype.Int8{Int64: filter.TaskID, Valid: true}
|
||||
}
|
||||
rows, err := s.q.ListBindingDispatches(ctx, params)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("edastore: list dispatches: %w", err)
|
||||
}
|
||||
out := make([]storecontract.Dispatch, 0, len(rows))
|
||||
for _, r := range rows {
|
||||
out = append(out, storecontract.Dispatch{
|
||||
BindingID: r.Binding, BindingVersion: r.BindingVersion, CaptureID: r.Capture,
|
||||
TaskID: r.TaskID, Reason: r.Reason, DispatchedAt: r.DispatchedAt,
|
||||
})
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (s *EDA) RecordPlaybookDispatch(ctx context.Context, playbookID, playbookVersion, eventID, actionID string) error {
|
||||
err := s.q.InsertPlaybookDispatch(ctx, postgresdb.InsertPlaybookDispatchParams{
|
||||
PlaybookID: playbookID,
|
||||
|
||||
@@ -8,6 +8,8 @@ package postgresdb
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
const armedBindingsForSource = `-- name: ArmedBindingsForSource :many
|
||||
@@ -597,6 +599,64 @@ func (q *Queries) InsertToolCall(ctx context.Context, arg InsertToolCallParams)
|
||||
return err
|
||||
}
|
||||
|
||||
const listBindingDispatches = `-- name: ListBindingDispatches :many
|
||||
SELECT binding, binding_version, capture, task_id, reason, dispatched_at
|
||||
FROM binding_dispatches
|
||||
WHERE ($1::text IS NULL OR binding = $1)
|
||||
AND ($2::text IS NULL OR capture = $2)
|
||||
AND ($3::bigint IS NULL OR task_id = $3)
|
||||
ORDER BY dispatched_at DESC
|
||||
LIMIT $4
|
||||
`
|
||||
|
||||
type ListBindingDispatchesParams struct {
|
||||
Binding pgtype.Text
|
||||
Capture pgtype.Text
|
||||
TaskID pgtype.Int8
|
||||
EntryLimit int32
|
||||
}
|
||||
|
||||
type ListBindingDispatchesRow struct {
|
||||
Binding string
|
||||
BindingVersion int64
|
||||
Capture string
|
||||
TaskID int64
|
||||
Reason string
|
||||
DispatchedAt time.Time
|
||||
}
|
||||
|
||||
func (q *Queries) ListBindingDispatches(ctx context.Context, arg ListBindingDispatchesParams) ([]ListBindingDispatchesRow, error) {
|
||||
rows, err := q.db.Query(ctx, listBindingDispatches,
|
||||
arg.Binding,
|
||||
arg.Capture,
|
||||
arg.TaskID,
|
||||
arg.EntryLimit,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []ListBindingDispatchesRow
|
||||
for rows.Next() {
|
||||
var i ListBindingDispatchesRow
|
||||
if err := rows.Scan(
|
||||
&i.Binding,
|
||||
&i.BindingVersion,
|
||||
&i.Capture,
|
||||
&i.TaskID,
|
||||
&i.Reason,
|
||||
&i.DispatchedAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const listBindings = `-- name: ListBindings :many
|
||||
SELECT id, name, source, mapping, workflow, owner, repo, version, status, created_at, updated_at, filter, inputs, repo_param, org_id
|
||||
FROM bindings ORDER BY created_at DESC
|
||||
@@ -883,6 +943,21 @@ func (q *Queries) SetBindingArmed(ctx context.Context, id string) (int64, error)
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
|
||||
const setBindingDispatchTask = `-- name: SetBindingDispatchTask :exec
|
||||
UPDATE binding_dispatches SET task_id = $3 WHERE binding = $1 AND capture = $2
|
||||
`
|
||||
|
||||
type SetBindingDispatchTaskParams struct {
|
||||
Binding string
|
||||
Capture string
|
||||
TaskID int64
|
||||
}
|
||||
|
||||
func (q *Queries) SetBindingDispatchTask(ctx context.Context, arg SetBindingDispatchTaskParams) error {
|
||||
_, err := q.db.Exec(ctx, setBindingDispatchTask, arg.Binding, arg.Capture, arg.TaskID)
|
||||
return err
|
||||
}
|
||||
|
||||
const setSourceSecret = `-- name: SetSourceSecret :execrows
|
||||
UPDATE sources SET secret = $2, updated_at = now() WHERE path = $1
|
||||
`
|
||||
|
||||
@@ -91,6 +91,18 @@ DELETE FROM bindings WHERE id = $1;
|
||||
INSERT INTO binding_dispatches (binding, binding_version, capture, task_id, reason)
|
||||
VALUES ($1, $2, $3, $4, $5);
|
||||
|
||||
-- name: SetBindingDispatchTask :exec
|
||||
UPDATE binding_dispatches SET task_id = $3 WHERE binding = $1 AND capture = $2;
|
||||
|
||||
-- name: ListBindingDispatches :many
|
||||
SELECT binding, binding_version, capture, task_id, reason, dispatched_at
|
||||
FROM binding_dispatches
|
||||
WHERE (sqlc.narg(binding)::text IS NULL OR binding = sqlc.narg(binding))
|
||||
AND (sqlc.narg(capture)::text IS NULL OR capture = sqlc.narg(capture))
|
||||
AND (sqlc.narg(task_id)::bigint IS NULL OR task_id = sqlc.narg(task_id))
|
||||
ORDER BY dispatched_at DESC
|
||||
LIMIT @entry_limit;
|
||||
|
||||
-- name: InsertPlaybookDispatch :exec
|
||||
INSERT INTO playbook_dispatches (playbook_id, playbook_version, event_id, action_id)
|
||||
VALUES ($1, $2, $3, $4);
|
||||
|
||||
Reference in new issue
Block a user