fix(daemon): record terminal non-dispatch outcomes so a capture is evaluated once
This commit is contained in:
1 parent
e302103e78
commit
79f005bd28
12 files changed
+127
-16
No files matched your search
@@ -0,0 +1,65 @@
|
||||
package postgres_test
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/binding"
|
||||
"github.com/samcharles93/archie-core/internal/domain/eventtype"
|
||||
"github.com/samcharles93/archie-core/internal/domain/mapping"
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore"
|
||||
)
|
||||
|
||||
// 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.
|
||||
func TestEvaluatedCaptureIsNotReoffered(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)
|
||||
}
|
||||
bindingID, 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.ApproveBinding(ctx, bindingID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
captureID, err := eda.InsertCapture(ctx, storecontract.CapturedEvent{
|
||||
Source: "forge", Body: "{}", Authenticated: true, ReceivedAt: time.Now(),
|
||||
}, time.Hour, 100)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
assertUndispatched(t, eda, 1)
|
||||
if err := eda.RecordDispatch(ctx, bindingID, 1, captureID, 0, "filtered"); err != nil {
|
||||
t.Fatalf("record filtered evaluation: %v", err)
|
||||
}
|
||||
assertUndispatched(t, eda, 0)
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
func assertUndispatched(t *testing.T, eda *postgres.EDA, want int) {
|
||||
t.Helper()
|
||||
got, err := eda.ListUndispatchedCaptures(t.Context(), []string{"forge"}, 10)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(got) != want {
|
||||
t.Fatalf("undispatched captures = %d, want %d", len(got), want)
|
||||
}
|
||||
}
|
||||
@@ -485,13 +485,15 @@ func (s *EDA) ApproveBinding(ctx context.Context, id string) error {
|
||||
|
||||
// RecordDispatch writes one at-most-once binding dispatch. binding_version is
|
||||
// stored but is not part of the unique key, so a version bump does not permit
|
||||
// re-dispatching the same (binding, capture).
|
||||
func (s *EDA) RecordDispatch(ctx context.Context, bindingID string, bindingVersion int64, captureID string, taskID int64) error {
|
||||
// re-dispatching the same (binding, capture). A non-empty reason records an
|
||||
// evaluation that ended without a task.
|
||||
func (s *EDA) RecordDispatch(ctx context.Context, bindingID string, bindingVersion int64, captureID string, taskID int64, reason string) error {
|
||||
err := s.q.InsertBindingDispatch(ctx, postgresdb.InsertBindingDispatchParams{
|
||||
Binding: bindingID,
|
||||
BindingVersion: bindingVersion,
|
||||
Capture: captureID,
|
||||
TaskID: taskID,
|
||||
Reason: reason,
|
||||
})
|
||||
return ledgerWrite(err, "edastore: record dispatch")
|
||||
}
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
-- binding_dispatches.reason says why an evaluated capture did not start a
|
||||
-- task; empty means it dispatched. Recording terminal non-dispatch outcomes
|
||||
-- keeps the capture from being re-evaluated every cycle.
|
||||
|
||||
-- +goose Up
|
||||
|
||||
ALTER TABLE binding_dispatches ADD COLUMN reason text NOT NULL DEFAULT '';
|
||||
|
||||
-- +goose Down
|
||||
|
||||
ALTER TABLE binding_dispatches DROP COLUMN reason;
|
||||
@@ -395,8 +395,8 @@ func (q *Queries) InsertBinding(ctx context.Context, arg InsertBindingParams) er
|
||||
}
|
||||
|
||||
const insertBindingDispatch = `-- name: InsertBindingDispatch :exec
|
||||
INSERT INTO binding_dispatches (binding, binding_version, capture, task_id)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
INSERT INTO binding_dispatches (binding, binding_version, capture, task_id, reason)
|
||||
VALUES ($1, $2, $3, $4, $5)
|
||||
`
|
||||
|
||||
type InsertBindingDispatchParams struct {
|
||||
@@ -404,6 +404,7 @@ type InsertBindingDispatchParams struct {
|
||||
BindingVersion int64
|
||||
Capture string
|
||||
TaskID int64
|
||||
Reason string
|
||||
}
|
||||
|
||||
func (q *Queries) InsertBindingDispatch(ctx context.Context, arg InsertBindingDispatchParams) error {
|
||||
@@ -412,6 +413,7 @@ func (q *Queries) InsertBindingDispatch(ctx context.Context, arg InsertBindingDi
|
||||
arg.BindingVersion,
|
||||
arg.Capture,
|
||||
arg.TaskID,
|
||||
arg.Reason,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -78,6 +78,7 @@ type BindingDispatch struct {
|
||||
Capture string
|
||||
TaskID int64
|
||||
DispatchedAt time.Time
|
||||
Reason string
|
||||
}
|
||||
|
||||
type Capture struct {
|
||||
|
||||
@@ -88,8 +88,8 @@ UPDATE bindings SET status = 'armed', updated_at = now() WHERE id = $1;
|
||||
DELETE FROM bindings WHERE id = $1;
|
||||
|
||||
-- name: InsertBindingDispatch :exec
|
||||
INSERT INTO binding_dispatches (binding, binding_version, capture, task_id)
|
||||
VALUES ($1, $2, $3, $4);
|
||||
INSERT INTO binding_dispatches (binding, binding_version, capture, task_id, reason)
|
||||
VALUES ($1, $2, $3, $4, $5);
|
||||
|
||||
-- name: InsertPlaybookDispatch :exec
|
||||
INSERT INTO playbook_dispatches (playbook_id, playbook_version, event_id, action_id)
|
||||
|
||||
Reference in new issue
Block a user