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
File diff suppressed because it is too large.
Load diff
@@ -100,6 +100,7 @@ const (
|
||||
StateStoreService_InsertBinding_FullMethodName = "/state.v1.StateStoreService/InsertBinding"
|
||||
StateStoreService_GetBinding_FullMethodName = "/state.v1.StateStoreService/GetBinding"
|
||||
StateStoreService_ListBindings_FullMethodName = "/state.v1.StateStoreService/ListBindings"
|
||||
StateStoreService_ListDispatches_FullMethodName = "/state.v1.StateStoreService/ListDispatches"
|
||||
StateStoreService_UpdateBinding_FullMethodName = "/state.v1.StateStoreService/UpdateBinding"
|
||||
StateStoreService_DeleteBinding_FullMethodName = "/state.v1.StateStoreService/DeleteBinding"
|
||||
StateStoreService_ApproveBinding_FullMethodName = "/state.v1.StateStoreService/ApproveBinding"
|
||||
@@ -110,6 +111,7 @@ const (
|
||||
StateStoreService_SetSourceSecret_FullMethodName = "/state.v1.StateStoreService/SetSourceSecret"
|
||||
StateStoreService_ArmedBindingsForSource_FullMethodName = "/state.v1.StateStoreService/ArmedBindingsForSource"
|
||||
StateStoreService_RecordDispatch_FullMethodName = "/state.v1.StateStoreService/RecordDispatch"
|
||||
StateStoreService_SetDispatchTask_FullMethodName = "/state.v1.StateStoreService/SetDispatchTask"
|
||||
StateStoreService_RecordPlaybookDispatch_FullMethodName = "/state.v1.StateStoreService/RecordPlaybookDispatch"
|
||||
StateStoreService_DeletePlaybookDispatches_FullMethodName = "/state.v1.StateStoreService/DeletePlaybookDispatches"
|
||||
StateStoreService_StreamUndispatchedCaptures_FullMethodName = "/state.v1.StateStoreService/StreamUndispatchedCaptures"
|
||||
@@ -289,6 +291,7 @@ type StateStoreServiceClient interface {
|
||||
InsertBinding(ctx context.Context, in *InsertBindingRequest, opts ...grpc.CallOption) (*InsertBindingResponse, error)
|
||||
GetBinding(ctx context.Context, in *GetBindingRequest, opts ...grpc.CallOption) (*GetBindingResponse, error)
|
||||
ListBindings(ctx context.Context, in *ListBindingsRequest, opts ...grpc.CallOption) (*ListBindingsResponse, error)
|
||||
ListDispatches(ctx context.Context, in *ListDispatchesRequest, opts ...grpc.CallOption) (*ListDispatchesResponse, error)
|
||||
UpdateBinding(ctx context.Context, in *UpdateBindingRequest, opts ...grpc.CallOption) (*UpdateBindingResponse, error)
|
||||
DeleteBinding(ctx context.Context, in *DeleteBindingRequest, opts ...grpc.CallOption) (*DeleteBindingResponse, error)
|
||||
ApproveBinding(ctx context.Context, in *ApproveBindingRequest, opts ...grpc.CallOption) (*ApproveBindingResponse, error)
|
||||
@@ -302,6 +305,7 @@ type StateStoreServiceClient interface {
|
||||
// Dispatch
|
||||
ArmedBindingsForSource(ctx context.Context, in *ArmedBindingsForSourceRequest, opts ...grpc.CallOption) (*ArmedBindingsForSourceResponse, error)
|
||||
RecordDispatch(ctx context.Context, in *RecordDispatchRequest, opts ...grpc.CallOption) (*RecordDispatchResponse, error)
|
||||
SetDispatchTask(ctx context.Context, in *SetDispatchTaskRequest, opts ...grpc.CallOption) (*SetDispatchTaskResponse, error)
|
||||
// Playbook dispatch ledger (eda-playbook-engine.md gap 2): one durable
|
||||
// at-most-once record per (playbook, version, event, action) for the
|
||||
// side-effecting positions. Administrative, like RecordDispatch: a
|
||||
@@ -1169,6 +1173,16 @@ func (c *stateStoreServiceClient) ListBindings(ctx context.Context, in *ListBind
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *stateStoreServiceClient) ListDispatches(ctx context.Context, in *ListDispatchesRequest, opts ...grpc.CallOption) (*ListDispatchesResponse, error) {
|
||||
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
|
||||
out := new(ListDispatchesResponse)
|
||||
err := c.cc.Invoke(ctx, StateStoreService_ListDispatches_FullMethodName, in, out, cOpts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *stateStoreServiceClient) UpdateBinding(ctx context.Context, in *UpdateBindingRequest, opts ...grpc.CallOption) (*UpdateBindingResponse, error) {
|
||||
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
|
||||
out := new(UpdateBindingResponse)
|
||||
@@ -1269,6 +1283,16 @@ func (c *stateStoreServiceClient) RecordDispatch(ctx context.Context, in *Record
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *stateStoreServiceClient) SetDispatchTask(ctx context.Context, in *SetDispatchTaskRequest, opts ...grpc.CallOption) (*SetDispatchTaskResponse, error) {
|
||||
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
|
||||
out := new(SetDispatchTaskResponse)
|
||||
err := c.cc.Invoke(ctx, StateStoreService_SetDispatchTask_FullMethodName, in, out, cOpts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *stateStoreServiceClient) RecordPlaybookDispatch(ctx context.Context, in *RecordPlaybookDispatchRequest, opts ...grpc.CallOption) (*RecordPlaybookDispatchResponse, error) {
|
||||
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
|
||||
out := new(RecordPlaybookDispatchResponse)
|
||||
@@ -1572,6 +1596,7 @@ type StateStoreServiceServer interface {
|
||||
InsertBinding(context.Context, *InsertBindingRequest) (*InsertBindingResponse, error)
|
||||
GetBinding(context.Context, *GetBindingRequest) (*GetBindingResponse, error)
|
||||
ListBindings(context.Context, *ListBindingsRequest) (*ListBindingsResponse, error)
|
||||
ListDispatches(context.Context, *ListDispatchesRequest) (*ListDispatchesResponse, error)
|
||||
UpdateBinding(context.Context, *UpdateBindingRequest) (*UpdateBindingResponse, error)
|
||||
DeleteBinding(context.Context, *DeleteBindingRequest) (*DeleteBindingResponse, error)
|
||||
ApproveBinding(context.Context, *ApproveBindingRequest) (*ApproveBindingResponse, error)
|
||||
@@ -1585,6 +1610,7 @@ type StateStoreServiceServer interface {
|
||||
// Dispatch
|
||||
ArmedBindingsForSource(context.Context, *ArmedBindingsForSourceRequest) (*ArmedBindingsForSourceResponse, error)
|
||||
RecordDispatch(context.Context, *RecordDispatchRequest) (*RecordDispatchResponse, error)
|
||||
SetDispatchTask(context.Context, *SetDispatchTaskRequest) (*SetDispatchTaskResponse, error)
|
||||
// Playbook dispatch ledger (eda-playbook-engine.md gap 2): one durable
|
||||
// at-most-once record per (playbook, version, event, action) for the
|
||||
// side-effecting positions. Administrative, like RecordDispatch: a
|
||||
@@ -1867,6 +1893,9 @@ func (UnimplementedStateStoreServiceServer) GetBinding(context.Context, *GetBind
|
||||
func (UnimplementedStateStoreServiceServer) ListBindings(context.Context, *ListBindingsRequest) (*ListBindingsResponse, error) {
|
||||
return nil, status.Error(codes.Unimplemented, "method ListBindings not implemented")
|
||||
}
|
||||
func (UnimplementedStateStoreServiceServer) ListDispatches(context.Context, *ListDispatchesRequest) (*ListDispatchesResponse, error) {
|
||||
return nil, status.Error(codes.Unimplemented, "method ListDispatches not implemented")
|
||||
}
|
||||
func (UnimplementedStateStoreServiceServer) UpdateBinding(context.Context, *UpdateBindingRequest) (*UpdateBindingResponse, error) {
|
||||
return nil, status.Error(codes.Unimplemented, "method UpdateBinding not implemented")
|
||||
}
|
||||
@@ -1897,6 +1926,9 @@ func (UnimplementedStateStoreServiceServer) ArmedBindingsForSource(context.Conte
|
||||
func (UnimplementedStateStoreServiceServer) RecordDispatch(context.Context, *RecordDispatchRequest) (*RecordDispatchResponse, error) {
|
||||
return nil, status.Error(codes.Unimplemented, "method RecordDispatch not implemented")
|
||||
}
|
||||
func (UnimplementedStateStoreServiceServer) SetDispatchTask(context.Context, *SetDispatchTaskRequest) (*SetDispatchTaskResponse, error) {
|
||||
return nil, status.Error(codes.Unimplemented, "method SetDispatchTask not implemented")
|
||||
}
|
||||
func (UnimplementedStateStoreServiceServer) RecordPlaybookDispatch(context.Context, *RecordPlaybookDispatchRequest) (*RecordPlaybookDispatchResponse, error) {
|
||||
return nil, status.Error(codes.Unimplemented, "method RecordPlaybookDispatch not implemented")
|
||||
}
|
||||
@@ -3401,6 +3433,24 @@ func _StateStoreService_ListBindings_Handler(srv interface{}, ctx context.Contex
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
func _StateStoreService_ListDispatches_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(ListDispatchesRequest)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(StateStoreServiceServer).ListDispatches(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: StateStoreService_ListDispatches_FullMethodName,
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(StateStoreServiceServer).ListDispatches(ctx, req.(*ListDispatchesRequest))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
func _StateStoreService_UpdateBinding_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(UpdateBindingRequest)
|
||||
if err := dec(in); err != nil {
|
||||
@@ -3581,6 +3631,24 @@ func _StateStoreService_RecordDispatch_Handler(srv interface{}, ctx context.Cont
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
func _StateStoreService_SetDispatchTask_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(SetDispatchTaskRequest)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(StateStoreServiceServer).SetDispatchTask(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: StateStoreService_SetDispatchTask_FullMethodName,
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(StateStoreServiceServer).SetDispatchTask(ctx, req.(*SetDispatchTaskRequest))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
func _StateStoreService_RecordPlaybookDispatch_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(RecordPlaybookDispatchRequest)
|
||||
if err := dec(in); err != nil {
|
||||
@@ -4131,6 +4199,10 @@ var StateStoreService_ServiceDesc = grpc.ServiceDesc{
|
||||
MethodName: "ListBindings",
|
||||
Handler: _StateStoreService_ListBindings_Handler,
|
||||
},
|
||||
{
|
||||
MethodName: "ListDispatches",
|
||||
Handler: _StateStoreService_ListDispatches_Handler,
|
||||
},
|
||||
{
|
||||
MethodName: "UpdateBinding",
|
||||
Handler: _StateStoreService_UpdateBinding_Handler,
|
||||
@@ -4171,6 +4243,10 @@ var StateStoreService_ServiceDesc = grpc.ServiceDesc{
|
||||
MethodName: "RecordDispatch",
|
||||
Handler: _StateStoreService_RecordDispatch_Handler,
|
||||
},
|
||||
{
|
||||
MethodName: "SetDispatchTask",
|
||||
Handler: _StateStoreService_SetDispatchTask_Handler,
|
||||
},
|
||||
{
|
||||
MethodName: "RecordPlaybookDispatch",
|
||||
Handler: _StateStoreService_RecordPlaybookDispatch_Handler,
|
||||
|
||||
@@ -701,6 +701,9 @@ func (d *Daemon) claimAndEnqueue(ctx context.Context, b binding.Binding, c store
|
||||
d.Log.Warn("binding dispatch: enqueue", "binding", b.ID, "capture", c.ID, "error", err)
|
||||
return
|
||||
}
|
||||
if err := d.BindingDispatcher.SetDispatchTask(ctx, b.ID, c.ID, task.ID); err != nil {
|
||||
d.Log.Warn("binding dispatch: record task", "binding", b.ID, "capture", c.ID, "task", task.ID, "error", err)
|
||||
}
|
||||
if c.Unsigned {
|
||||
d.markUnsignedStart(ctx, task.ID, b, c)
|
||||
}
|
||||
|
||||
@@ -228,6 +228,8 @@ type BindingStore interface {
|
||||
InsertBinding(ctx context.Context, b binding.Binding) (string, error)
|
||||
GetBinding(ctx context.Context, id string) (*binding.Binding, error)
|
||||
ListBindings(ctx context.Context) ([]binding.Binding, error)
|
||||
// ListDispatches returns the newest ledger rows matching filter.
|
||||
ListDispatches(ctx context.Context, filter DispatchFilter) ([]Dispatch, error)
|
||||
UpdateBinding(ctx context.Context, b binding.Binding) error
|
||||
DeleteBinding(ctx context.Context, id string) error
|
||||
ApproveBinding(ctx context.Context, id string) error
|
||||
@@ -244,6 +246,25 @@ type SourceStore interface {
|
||||
SetSourceSecret(ctx context.Context, path, secret string) error
|
||||
}
|
||||
|
||||
// Dispatch is one binding/capture ledger row: the task it started, or the
|
||||
// reason it started none.
|
||||
type Dispatch struct {
|
||||
BindingID string `json:"binding_id"`
|
||||
BindingVersion int64 `json:"binding_version"`
|
||||
CaptureID string `json:"capture_id"`
|
||||
TaskID int64 `json:"task_id,omitempty"`
|
||||
Reason string `json:"reason,omitempty"`
|
||||
DispatchedAt time.Time `json:"dispatched_at"`
|
||||
}
|
||||
|
||||
// DispatchFilter narrows ListDispatches; empty fields match any row.
|
||||
type DispatchFilter struct {
|
||||
BindingID string
|
||||
CaptureID string
|
||||
TaskID int64
|
||||
Limit int
|
||||
}
|
||||
|
||||
// BindingDispatcher looks up armed bindings, lists undispatched captures and
|
||||
// records dispatches.
|
||||
type BindingDispatcher interface {
|
||||
@@ -262,6 +283,8 @@ type BindingDispatcher interface {
|
||||
reason string,
|
||||
) error
|
||||
ListUndispatchedCaptures(ctx context.Context, sources []string, limit int) ([]CapturedEvent, error)
|
||||
// SetDispatchTask records the task a claimed dispatch started.
|
||||
SetDispatchTask(ctx context.Context, bindingID, captureID string, taskID int64) error
|
||||
}
|
||||
|
||||
// BindingTaskCreator is the single-method consumer-facing surface for
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -573,6 +573,26 @@ func (c *Client) ListBindings(ctx context.Context) ([]binding.Binding, error) {
|
||||
return mapValues(r.Bindings, bindingValue), nil
|
||||
}
|
||||
|
||||
func (c *Client) ListDispatches(ctx context.Context, filter storecontract.DispatchFilter) ([]storecontract.Dispatch, error) {
|
||||
r, err := c.client.ListDispatches(ctx, &pb.ListDispatchesRequest{
|
||||
BindingId: filter.BindingID, CaptureId: filter.CaptureID, TaskId: filter.TaskID, Limit: int32(filter.Limit), //nolint:gosec // the store clamps it
|
||||
})
|
||||
if err != nil {
|
||||
return nil, unmapError(err)
|
||||
}
|
||||
return mapValues(r.Dispatches, func(d *pb.Dispatch) storecontract.Dispatch {
|
||||
return storecontract.Dispatch{
|
||||
BindingID: d.BindingId, BindingVersion: d.BindingVersion, CaptureID: d.CaptureId,
|
||||
TaskID: d.TaskId, Reason: d.Reason, DispatchedAt: timeValue(d.DispatchedAt),
|
||||
}
|
||||
}), nil
|
||||
}
|
||||
|
||||
func (c *Client) SetDispatchTask(ctx context.Context, bindingID, captureID string, taskID int64) error {
|
||||
_, err := c.client.SetDispatchTask(ctx, &pb.SetDispatchTaskRequest{BindingId: bindingID, CaptureId: captureID, TaskId: taskID})
|
||||
return unmapError(err)
|
||||
}
|
||||
|
||||
func (c *Client) UpdateBinding(ctx context.Context, b binding.Binding) error {
|
||||
_, err := c.client.UpdateBinding(ctx, &pb.UpdateBindingRequest{Binding: bindingProto(b)})
|
||||
return unmapError(err)
|
||||
|
||||
@@ -948,6 +948,35 @@ func (s *server) RecordDispatch(ctx context.Context, r *pb.RecordDispatchRequest
|
||||
return &pb.RecordDispatchResponse{}, nil
|
||||
}
|
||||
|
||||
func (s *server) SetDispatchTask(ctx context.Context, r *pb.SetDispatchTaskRequest) (*pb.SetDispatchTaskResponse, error) {
|
||||
if s.deps.BindingDispatcher == nil {
|
||||
return nil, errBindingDispatchUnavailable
|
||||
}
|
||||
if err := s.deps.BindingDispatcher.SetDispatchTask(ctx, r.BindingId, r.CaptureId, r.TaskId); err != nil {
|
||||
return nil, s.logErr("SetDispatchTask", err)
|
||||
}
|
||||
return &pb.SetDispatchTaskResponse{}, nil
|
||||
}
|
||||
|
||||
func (s *server) ListDispatches(ctx context.Context, r *pb.ListDispatchesRequest) (*pb.ListDispatchesResponse, error) {
|
||||
bs, err := s.binding()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
dispatches, err := bs.ListDispatches(ctx, storecontract.DispatchFilter{
|
||||
BindingID: r.BindingId, CaptureID: r.CaptureId, TaskID: r.TaskId, Limit: int(r.Limit),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, s.logErr("ListDispatches", err)
|
||||
}
|
||||
return &pb.ListDispatchesResponse{Dispatches: mapValues(dispatches, func(d storecontract.Dispatch) *pb.Dispatch {
|
||||
return &pb.Dispatch{
|
||||
BindingId: d.BindingID, BindingVersion: d.BindingVersion, CaptureId: d.CaptureID,
|
||||
TaskId: d.TaskID, Reason: d.Reason, DispatchedAt: timestamp(d.DispatchedAt),
|
||||
}
|
||||
})}, nil
|
||||
}
|
||||
|
||||
// Playbook dispatch
|
||||
|
||||
func (s *server) RecordPlaybookDispatch(ctx context.Context, r *pb.RecordPlaybookDispatchRequest) (*pb.RecordPlaybookDispatchResponse, error) {
|
||||
|
||||
@@ -128,6 +128,30 @@ func (s *Server) handleBindingGet(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(w, b)
|
||||
}
|
||||
|
||||
// handleDispatches serves the newest dispatch ledger rows for the resource the
|
||||
// path names, linking bindings, captures and tasks to each other.
|
||||
func (s *Server) handleDispatches(filterFor func(id string) (storecontract.DispatchFilter, bool)) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if s.Bindings == nil {
|
||||
http.Error(w, "bindings not configured", http.StatusServiceUnavailable)
|
||||
return
|
||||
}
|
||||
filter, ok := filterFor(r.PathValue("id"))
|
||||
if !ok {
|
||||
http.Error(w, "invalid id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
filter.Limit = 50
|
||||
dispatches, err := s.Bindings.ListDispatches(r.Context(), filter)
|
||||
if err != nil {
|
||||
s.Log.Error("list dispatches", "err", err)
|
||||
http.Error(w, "list dispatches failed", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
writeJSON(w, dispatches)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) handleBindingUpdate(w http.ResponseWriter, r *http.Request) {
|
||||
if !s.authorizeTaskMutation(w, r) {
|
||||
return
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"io/fs"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@@ -282,6 +283,16 @@ func (s *Server) registerMappingAndBindingRoutes(mux *http.ServeMux) {
|
||||
mux.HandleFunc("PATCH /api/bindings/{id}", s.handleBindingUpdate)
|
||||
mux.HandleFunc("DELETE /api/bindings/{id}", s.handleBindingDelete)
|
||||
mux.HandleFunc("POST /api/bindings/{id}/approve", s.handleBindingApprove)
|
||||
mux.HandleFunc("GET /api/bindings/{id}/dispatches", s.handleDispatches(func(id string) (storecontract.DispatchFilter, bool) {
|
||||
return storecontract.DispatchFilter{BindingID: id}, id != ""
|
||||
}))
|
||||
mux.HandleFunc("GET /api/captures/{id}/dispatches", s.handleDispatches(func(id string) (storecontract.DispatchFilter, bool) {
|
||||
return storecontract.DispatchFilter{CaptureID: id}, id != ""
|
||||
}))
|
||||
mux.HandleFunc("GET /api/tasks/{id}/dispatches", s.handleDispatches(func(id string) (storecontract.DispatchFilter, bool) {
|
||||
taskID, err := strconv.ParseInt(id, 10, 64)
|
||||
return storecontract.DispatchFilter{TaskID: taskID}, err == nil && taskID > 0
|
||||
}))
|
||||
mux.HandleFunc("GET /api/sources", s.handleSourcesList)
|
||||
mux.HandleFunc("POST /api/sources", s.handleSourceCreate)
|
||||
mux.HandleFunc("POST /api/sources/{path}/signing", s.handleSourceSigning)
|
||||
|
||||
Reference in new issue
Block a user