refactor: the single binary uses encoding/json/v2 and jsontext (#1977)
This commit is contained in:
13 files changed
+103
-99
No files matched your search
@@ -4,7 +4,7 @@ package health
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
json "encoding/json/v2"
|
||||
"net/http"
|
||||
"sort"
|
||||
"sync"
|
||||
@@ -55,7 +55,7 @@ func (r *Registry) ready(w http.ResponseWriter, req *http.Request) {
|
||||
if len(failures) > 0 {
|
||||
w.WriteHeader(http.StatusServiceUnavailable)
|
||||
}
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{"ready": len(failures) == 0, "failures": failures})
|
||||
_ = json.MarshalWrite(w, map[string]any{"ready": len(failures) == 0, "failures": failures})
|
||||
}
|
||||
|
||||
func (r *Registry) names() []string {
|
||||
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
"crypto/sha256"
|
||||
"crypto/subtle"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
json "encoding/json/v2"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
@@ -21,7 +21,7 @@ const maxBody = 1 << 20
|
||||
func Write(w http.ResponseWriter, status int, v any) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(status)
|
||||
_ = json.NewEncoder(w).Encode(v)
|
||||
_ = json.MarshalWrite(w, v)
|
||||
}
|
||||
|
||||
// Error writes {"error": msg} with status.
|
||||
@@ -31,9 +31,7 @@ func Error(w http.ResponseWriter, status int, msg string) {
|
||||
|
||||
// Read decodes a bounded request body into v, refusing unknown fields.
|
||||
func Read(r *http.Request, v any) error {
|
||||
dec := json.NewDecoder(io.LimitReader(r.Body, maxBody))
|
||||
dec.DisallowUnknownFields()
|
||||
if err := dec.Decode(v); err != nil {
|
||||
if err := json.UnmarshalRead(io.LimitReader(r.Body, maxBody), v, json.RejectUnknownMembers(true)); err != nil {
|
||||
return fmt.Errorf("read request body: %w", err)
|
||||
}
|
||||
return nil
|
||||
|
||||
@@ -4,7 +4,8 @@ import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"encoding/json/jsontext"
|
||||
json "encoding/json/v2"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
@@ -65,7 +66,7 @@ type Resolution struct {
|
||||
type Event struct {
|
||||
Type string
|
||||
SourceEventID string
|
||||
Fields json.RawMessage
|
||||
Fields jsontext.Value
|
||||
LinkKey string
|
||||
OccurredAt time.Time
|
||||
}
|
||||
@@ -73,7 +74,7 @@ type Event struct {
|
||||
// Invocation is one judged action to run.
|
||||
type Invocation struct {
|
||||
Action string
|
||||
Input json.RawMessage
|
||||
Input jsontext.Value
|
||||
IdempotencyKey string
|
||||
}
|
||||
|
||||
@@ -166,7 +167,7 @@ func (c *Connector) Parse(ctx context.Context, body []byte, headers map[string]s
|
||||
}
|
||||
|
||||
// Resolve returns the target and effect of one concrete action.
|
||||
func (c *Connector) Resolve(ctx context.Context, name string, input json.RawMessage) (Resolution, error) {
|
||||
func (c *Connector) Resolve(ctx context.Context, name string, input jsontext.Value) (Resolution, error) {
|
||||
if _, err := c.action(name, input); err != nil {
|
||||
return Resolution{}, err
|
||||
}
|
||||
@@ -210,7 +211,7 @@ func (c *Connector) Invoke[T any](ctx context.Context, inv Invocation) (T, error
|
||||
|
||||
// Lookup answers a read-only question and decodes the answer into T. found is
|
||||
// false when the connector found nothing, which callers report as a finding.
|
||||
func (c *Connector) Lookup[T any](ctx context.Context, name string, input json.RawMessage) (T, bool, error) {
|
||||
func (c *Connector) Lookup[T any](ctx context.Context, name string, input jsontext.Value) (T, bool, error) {
|
||||
var zero T
|
||||
schema, ok := c.lookups[name]
|
||||
if !ok {
|
||||
@@ -230,7 +231,7 @@ func (c *Connector) Lookup[T any](ctx context.Context, name string, input json.R
|
||||
return out, err == nil, err
|
||||
}
|
||||
|
||||
func (c *Connector) action(name string, input json.RawMessage) (action, error) {
|
||||
func (c *Connector) action(name string, input jsontext.Value) (action, error) {
|
||||
a, ok := c.actions[name]
|
||||
if !ok {
|
||||
return action{}, fmt.Errorf("connector %q: action %q: %w", c.name, name, ErrUnknownName)
|
||||
@@ -243,7 +244,7 @@ func (c *Connector) action(name string, input json.RawMessage) (action, error) {
|
||||
|
||||
// Digest identifies one concrete action: its name and its exact input. The
|
||||
// connector SDK computes the same digest over what it ran.
|
||||
func Digest(action string, input json.RawMessage) string {
|
||||
func Digest(action string, input jsontext.Value) string {
|
||||
h := sha256.New()
|
||||
h.Write([]byte(action))
|
||||
h.Write([]byte{0})
|
||||
@@ -270,9 +271,9 @@ func compile(raw []byte) (*jsonschema.Resolved, error) {
|
||||
return resolved, nil
|
||||
}
|
||||
|
||||
func validate(schema *jsonschema.Resolved, raw json.RawMessage) error {
|
||||
func validate(schema *jsonschema.Resolved, raw jsontext.Value) error {
|
||||
if len(raw) == 0 {
|
||||
raw = json.RawMessage(`{}`)
|
||||
raw = jsontext.Value(`{}`)
|
||||
}
|
||||
var instance any
|
||||
if err := json.Unmarshal(raw, &instance); err != nil {
|
||||
|
||||
@@ -2,7 +2,7 @@ package connectors
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"encoding/json/jsontext"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
@@ -53,11 +53,11 @@ func (f *fakeClient) Invoke(_ context.Context, req *pb.InvokeRequest, _ ...grpc.
|
||||
// judged action is caught by its digest, and an unconfirmed outcome is
|
||||
// reported as unknown rather than success.
|
||||
func TestInvokeRunsOnlyTheJudgedAction(t *testing.T) {
|
||||
good := json.RawMessage(`{"path":"/tmp/runner"}`)
|
||||
good := jsontext.Value(`{"path":"/tmp/runner"}`)
|
||||
tests := []struct {
|
||||
name string
|
||||
client *fakeClient
|
||||
input json.RawMessage
|
||||
input jsontext.Value
|
||||
wantErr error
|
||||
wantAnyErr bool
|
||||
wantInvoked int
|
||||
@@ -67,7 +67,7 @@ func TestInvokeRunsOnlyTheJudgedAction(t *testing.T) {
|
||||
{name: "external effects without idempotency are refused before running", client: &fakeClient{}, input: good, wantErr: ErrNotIdempotent},
|
||||
{name: "read-only actions need no idempotency", client: &fakeClient{readOnly: true}, input: good, wantInvoked: 1},
|
||||
{name: "an unconfirmed outcome is unknown", client: &fakeClient{idempotent: true, unknown: true}, input: good, wantErr: ErrOutcomeUnknown, wantInvoked: 1},
|
||||
{name: "input breaking the schema never reaches the connector", client: &fakeClient{idempotent: true}, input: json.RawMessage(`{"path":7}`), wantAnyErr: true, wantInvoked: 0},
|
||||
{name: "input breaking the schema never reaches the connector", client: &fakeClient{idempotent: true}, input: jsontext.Value(`{"path":7}`), wantAnyErr: true, wantInvoked: 0},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
|
||||
@@ -5,7 +5,8 @@ package store
|
||||
import (
|
||||
"context"
|
||||
"database/sql/driver"
|
||||
"encoding/json"
|
||||
"encoding/json/jsontext"
|
||||
json "encoding/json/v2"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
@@ -15,13 +16,13 @@ import (
|
||||
// Accepted is one connector an operator accepted, with the authority it
|
||||
// declared.
|
||||
type Accepted struct {
|
||||
OrgID string `db:"org_id"`
|
||||
Name string `db:"name"`
|
||||
Path string `db:"path"`
|
||||
SHA256 string `db:"sha256"`
|
||||
Egress Strings `db:"egress"`
|
||||
Env Strings `db:"env"`
|
||||
Settings json.RawMessage `db:"settings"`
|
||||
OrgID string `db:"org_id"`
|
||||
Name string `db:"name"`
|
||||
Path string `db:"path"`
|
||||
SHA256 string `db:"sha256"`
|
||||
Egress Strings `db:"egress"`
|
||||
Env Strings `db:"env"`
|
||||
Settings jsontext.Value `db:"settings"`
|
||||
}
|
||||
|
||||
// Store is the connectors module's Postgres store.
|
||||
|
||||
@@ -2,7 +2,7 @@ package connectors
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
json "encoding/json/v2"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
|
||||
+13
-12
@@ -8,7 +8,8 @@ import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"encoding/json/jsontext"
|
||||
json "encoding/json/v2"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
@@ -174,12 +175,12 @@ func (m *Module) dispatch(tx *sqlx.Tx, e store.Event) (bool, error) {
|
||||
return matched, nil
|
||||
}
|
||||
|
||||
func runInput(e store.Event) json.RawMessage {
|
||||
func runInput(e store.Event) jsontext.Value {
|
||||
input, err := json.Marshal(map[string]any{"event": map[string]any{
|
||||
"id": e.ID, "source": e.Source, "type": e.Type, "fields": e.Fields, "occurred_at": e.OccurredAt.Time,
|
||||
}})
|
||||
if err != nil {
|
||||
return json.RawMessage("null")
|
||||
return jsontext.Value("null")
|
||||
}
|
||||
return input
|
||||
}
|
||||
@@ -268,15 +269,15 @@ func (m *Module) recent(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
// eventView is an event as the API returns it.
|
||||
type eventView struct {
|
||||
ID uuid.UUID `json:"id"`
|
||||
Source string `json:"source"`
|
||||
Type string `json:"type"`
|
||||
Fields json.RawMessage `json:"fields"`
|
||||
LinkedTo *uuid.UUID `json:"linked_to,omitempty"`
|
||||
OccurredAt *time.Time `json:"occurred_at,omitempty"`
|
||||
ReceivedAt time.Time `json:"received_at"`
|
||||
Matched bool `json:"matched"`
|
||||
DispatchedAt *time.Time `json:"dispatched_at,omitempty"`
|
||||
ID uuid.UUID `json:"id"`
|
||||
Source string `json:"source"`
|
||||
Type string `json:"type"`
|
||||
Fields jsontext.Value `json:"fields"`
|
||||
LinkedTo *uuid.UUID `json:"linked_to,omitempty"`
|
||||
OccurredAt *time.Time `json:"occurred_at,omitempty"`
|
||||
ReceivedAt time.Time `json:"received_at"`
|
||||
Matched bool `json:"matched"`
|
||||
DispatchedAt *time.Time `json:"dispatched_at,omitempty"`
|
||||
}
|
||||
|
||||
func viewOf(e store.Event) eventView {
|
||||
|
||||
@@ -2,14 +2,15 @@
|
||||
package match
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"encoding/json/jsontext"
|
||||
json "encoding/json/v2"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Fields reports whether every filter entry equals the event field at its
|
||||
// dotted path ("labels.alertname"). An empty filter matches every event.
|
||||
func Fields(fields json.RawMessage, filter map[string]string) bool {
|
||||
func Fields(fields jsontext.Value, filter map[string]string) bool {
|
||||
if len(filter) == 0 {
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ package store
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"encoding/json/jsontext"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
@@ -28,30 +28,30 @@ type Source struct {
|
||||
|
||||
// Event is one stored event.
|
||||
type Event struct {
|
||||
ID uuid.UUID `db:"id"`
|
||||
OrgID string `db:"org_id"`
|
||||
Source string `db:"source"`
|
||||
Type string `db:"type"`
|
||||
SourceEventID string `db:"source_event_id"`
|
||||
LinkKey sql.NullString `db:"link_key"`
|
||||
LinkedTo *uuid.UUID `db:"linked_to"`
|
||||
Fields json.RawMessage `db:"fields"`
|
||||
OccurredAt sql.NullTime `db:"occurred_at"`
|
||||
ReceivedAt time.Time `db:"received_at"`
|
||||
Matched bool `db:"matched"`
|
||||
DispatchedAt sql.NullTime `db:"dispatched_at"`
|
||||
ID uuid.UUID `db:"id"`
|
||||
OrgID string `db:"org_id"`
|
||||
Source string `db:"source"`
|
||||
Type string `db:"type"`
|
||||
SourceEventID string `db:"source_event_id"`
|
||||
LinkKey sql.NullString `db:"link_key"`
|
||||
LinkedTo *uuid.UUID `db:"linked_to"`
|
||||
Fields jsontext.Value `db:"fields"`
|
||||
OccurredAt sql.NullTime `db:"occurred_at"`
|
||||
ReceivedAt time.Time `db:"received_at"`
|
||||
Matched bool `db:"matched"`
|
||||
DispatchedAt sql.NullTime `db:"dispatched_at"`
|
||||
}
|
||||
|
||||
// Trigger starts a workflow for events of one type from one source whose
|
||||
// fields match its filter.
|
||||
type Trigger struct {
|
||||
OrgID string `db:"org_id"`
|
||||
Name string `db:"name"`
|
||||
Source string `db:"source"`
|
||||
EventType string `db:"event_type"`
|
||||
Filter json.RawMessage `db:"filter"`
|
||||
Workflow string `db:"workflow"`
|
||||
Enabled bool `db:"enabled"`
|
||||
OrgID string `db:"org_id"`
|
||||
Name string `db:"name"`
|
||||
Source string `db:"source"`
|
||||
EventType string `db:"event_type"`
|
||||
Filter jsontext.Value `db:"filter"`
|
||||
Workflow string `db:"workflow"`
|
||||
Enabled bool `db:"enabled"`
|
||||
}
|
||||
|
||||
// Store is the events module's Postgres store.
|
||||
|
||||
@@ -3,7 +3,8 @@ package runs
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"encoding/json/jsontext"
|
||||
json "encoding/json/v2"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
@@ -100,14 +101,14 @@ func (rc *Context) Sleep(d time.Duration) error {
|
||||
|
||||
// Wait returns the payload signalled to this run under key, suspending the run
|
||||
// durably until it arrives or timeout passes from the first time it waited.
|
||||
func (rc *Context) Wait(key string, timeout time.Duration) (json.RawMessage, error) {
|
||||
func (rc *Context) Wait(key string, timeout time.Duration) (jsontext.Value, error) {
|
||||
deadline, err := rc.Step("wait-start:"+key, func(context.Context, string) (time.Time, error) {
|
||||
return time.Now().Add(timeout).UTC(), nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return rc.Step("wait:"+key, func(ctx context.Context, _ string) (json.RawMessage, error) {
|
||||
return rc.Step("wait:"+key, func(ctx context.Context, _ string) (jsontext.Value, error) {
|
||||
payload, ok, err := rc.work.Signalled(ctx, rc.run.ID, key)
|
||||
switch {
|
||||
case err != nil:
|
||||
|
||||
@@ -5,7 +5,7 @@ package store
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"encoding/json/jsontext"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
@@ -41,21 +41,21 @@ var (
|
||||
|
||||
// Run is a claimed run.
|
||||
type Run struct {
|
||||
ID uuid.UUID `db:"id"`
|
||||
OrgID string `db:"org_id"`
|
||||
Workflow string `db:"workflow"`
|
||||
Input json.RawMessage `db:"input"`
|
||||
Attempts int `db:"attempts"`
|
||||
ID uuid.UUID `db:"id"`
|
||||
OrgID string `db:"org_id"`
|
||||
Workflow string `db:"workflow"`
|
||||
Input jsontext.Value `db:"input"`
|
||||
Attempts int `db:"attempts"`
|
||||
}
|
||||
|
||||
// Step is one recorded step of a run.
|
||||
type Step struct {
|
||||
RunID uuid.UUID `db:"run_id"`
|
||||
Seq int `db:"seq"`
|
||||
Name string `db:"name"`
|
||||
Outcome string `db:"outcome"`
|
||||
Output json.RawMessage `db:"output"`
|
||||
Error sql.NullString `db:"error"`
|
||||
RunID uuid.UUID `db:"run_id"`
|
||||
Seq int `db:"seq"`
|
||||
Name string `db:"name"`
|
||||
Outcome string `db:"outcome"`
|
||||
Output jsontext.Value `db:"output"`
|
||||
Error sql.NullString `db:"error"`
|
||||
}
|
||||
|
||||
// Store is the runs module's Postgres store.
|
||||
@@ -70,7 +70,7 @@ func New(db *sqlx.DB) *Store {
|
||||
|
||||
// Start inserts a queued run. A run with the same organisation and start key
|
||||
// is not inserted again; its id is returned instead.
|
||||
func (s *Store) Start(ctx context.Context, orgID, workflow, startKey string, input json.RawMessage) (uuid.UUID, error) {
|
||||
func (s *Store) Start(ctx context.Context, orgID, workflow, startKey string, input jsontext.Value) (uuid.UUID, error) {
|
||||
key := sql.NullString{String: startKey, Valid: startKey != ""}
|
||||
var id uuid.UUID
|
||||
err := s.db.GetContext(ctx, &id, `
|
||||
@@ -175,7 +175,7 @@ func (s *Store) Suspend(ctx context.Context, id, token uuid.UUID, wakeAt *time.T
|
||||
}
|
||||
|
||||
// Finish records a terminal state while the token holds the lease.
|
||||
func (s *Store) Finish(ctx context.Context, id, token uuid.UUID, state string, result json.RawMessage, errText string) error {
|
||||
func (s *Store) Finish(ctx context.Context, id, token uuid.UUID, state string, result jsontext.Value, errText string) error {
|
||||
res, err := s.db.ExecContext(ctx, `
|
||||
UPDATE runs.runs
|
||||
SET state = $3, result = $4, error = NULLIF($5, ''), finished_at = now(), updated_at = now(),
|
||||
@@ -191,7 +191,7 @@ func (s *Store) Finish(ctx context.Context, id, token uuid.UUID, state string, r
|
||||
}
|
||||
|
||||
// Signal stores a signal for a run and queues the run if it is waiting for it.
|
||||
func (s *Store) Signal(ctx context.Context, id uuid.UUID, key string, payload json.RawMessage) error {
|
||||
func (s *Store) Signal(ctx context.Context, id uuid.UUID, key string, payload jsontext.Value) error {
|
||||
tx, err := s.db.BeginTxx(ctx, nil)
|
||||
if err != nil {
|
||||
return fmt.Errorf("signal run: %w", err)
|
||||
@@ -214,8 +214,8 @@ func (s *Store) Signal(ctx context.Context, id uuid.UUID, key string, payload js
|
||||
}
|
||||
|
||||
// Signalled returns the payload of a run's signal, if one arrived.
|
||||
func (s *Store) Signalled(ctx context.Context, id uuid.UUID, key string) (json.RawMessage, bool, error) {
|
||||
var payload json.RawMessage
|
||||
func (s *Store) Signalled(ctx context.Context, id uuid.UUID, key string) (jsontext.Value, bool, error) {
|
||||
var payload jsontext.Value
|
||||
err := s.db.GetContext(ctx, &payload, `SELECT payload FROM runs.signals WHERE run_id = $1 AND key = $2`, id, key)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, false, nil
|
||||
@@ -270,7 +270,7 @@ func affected(res sql.Result) int64 {
|
||||
return n
|
||||
}
|
||||
|
||||
func nullJSON(v json.RawMessage) any {
|
||||
func nullJSON(v jsontext.Value) any {
|
||||
if len(v) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ package runs
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"encoding/json/jsontext"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
@@ -22,7 +22,7 @@ import (
|
||||
// Workflow is the code a run executes. It must choose its next step only from
|
||||
// its input and the results of earlier steps; clock reads, randomness and
|
||||
// lookups belong inside steps.
|
||||
type Workflow func(rc *Context, input json.RawMessage) (json.RawMessage, error)
|
||||
type Workflow func(rc *Context, input jsontext.Value) (jsontext.Value, error)
|
||||
|
||||
// StartRequest names the workflow to run and the organisation it runs for.
|
||||
// A non-empty Key makes the start idempotent within the organisation.
|
||||
@@ -30,7 +30,7 @@ type StartRequest struct {
|
||||
OrgID string
|
||||
Workflow string
|
||||
Key string
|
||||
Input json.RawMessage
|
||||
Input jsontext.Value
|
||||
}
|
||||
|
||||
// Options tune the engine. Zero values take the defaults.
|
||||
@@ -48,8 +48,8 @@ type workerStore interface {
|
||||
Steps(ctx context.Context, id uuid.UUID) ([]store.Step, error)
|
||||
RecordStep(ctx context.Context, token uuid.UUID, step store.Step) error
|
||||
Suspend(ctx context.Context, id, token uuid.UUID, wakeAt *time.Time, waitingFor string) error
|
||||
Finish(ctx context.Context, id, token uuid.UUID, state string, result json.RawMessage, errText string) error
|
||||
Signalled(ctx context.Context, id uuid.UUID, key string) (json.RawMessage, bool, error)
|
||||
Finish(ctx context.Context, id, token uuid.UUID, state string, result jsontext.Value, errText string) error
|
||||
Signalled(ctx context.Context, id uuid.UUID, key string) (jsontext.Value, bool, error)
|
||||
CancelRequested(ctx context.Context, id uuid.UUID) (bool, error)
|
||||
}
|
||||
|
||||
@@ -96,13 +96,13 @@ func (e *Engine) Register(name string, wf Workflow) {
|
||||
func (e *Engine) Start(ctx context.Context, req StartRequest) (uuid.UUID, error) {
|
||||
input := req.Input
|
||||
if len(input) == 0 {
|
||||
input = json.RawMessage("null")
|
||||
input = jsontext.Value("null")
|
||||
}
|
||||
return e.db.Start(ctx, req.OrgID, req.Workflow, req.Key, input)
|
||||
}
|
||||
|
||||
// Signal delivers a payload to a run waiting on key, now or later.
|
||||
func (e *Engine) Signal(ctx context.Context, id uuid.UUID, key string, payload json.RawMessage) error {
|
||||
func (e *Engine) Signal(ctx context.Context, id uuid.UUID, key string, payload jsontext.Value) error {
|
||||
return e.db.Signal(ctx, id, key, payload)
|
||||
}
|
||||
|
||||
@@ -178,7 +178,7 @@ func (e *Engine) execute(ctx context.Context, run store.Run, token uuid.UUID) {
|
||||
|
||||
// invoke runs the workflow, turning a panic in workflow or step code into a
|
||||
// failure of this run rather than of the process.
|
||||
func (e *Engine) invoke(ctx context.Context, run store.Run, token uuid.UUID) (result json.RawMessage, err error) {
|
||||
func (e *Engine) invoke(ctx context.Context, run store.Run, token uuid.UUID) (result jsontext.Value, err error) {
|
||||
defer func() {
|
||||
if p := recover(); p != nil {
|
||||
err = fmt.Errorf("panic: %v", p)
|
||||
|
||||
@@ -2,7 +2,8 @@ package runs
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"encoding/json/jsontext"
|
||||
json "encoding/json/v2"
|
||||
"log/slog"
|
||||
"sync"
|
||||
"testing"
|
||||
@@ -24,7 +25,7 @@ type fakeWork struct {
|
||||
recordedElsewhere *store.Step
|
||||
steps []store.Step
|
||||
finished []string
|
||||
results []json.RawMessage
|
||||
results []jsontext.Value
|
||||
}
|
||||
|
||||
func (f *fakeWork) Claim(context.Context, string, uuid.UUID, time.Duration) (store.Run, bool, error) {
|
||||
@@ -59,7 +60,7 @@ func (f *fakeWork) Suspend(context.Context, uuid.UUID, uuid.UUID, *time.Time, st
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *fakeWork) Finish(_ context.Context, _, _ uuid.UUID, state string, result json.RawMessage, _ string) error {
|
||||
func (f *fakeWork) Finish(_ context.Context, _, _ uuid.UUID, state string, result jsontext.Value, _ string) error {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
if f.leaseLost {
|
||||
@@ -70,7 +71,7 @@ func (f *fakeWork) Finish(_ context.Context, _, _ uuid.UUID, state string, resul
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *fakeWork) Signalled(context.Context, uuid.UUID, string) (json.RawMessage, bool, error) {
|
||||
func (f *fakeWork) Signalled(context.Context, uuid.UUID, string) (jsontext.Value, bool, error) {
|
||||
return nil, false, nil
|
||||
}
|
||||
|
||||
@@ -83,7 +84,7 @@ func (f *fakeWork) CancelRequested(context.Context, uuid.UUID) (bool, error) {
|
||||
// step (which could repeat an effect), and a step another holder already
|
||||
// recorded wins over this worker's own result.
|
||||
func TestLeaseFencesStepWrites(t *testing.T) {
|
||||
theirs := json.RawMessage(`"theirs"`)
|
||||
theirs := jsontext.Value(`"theirs"`)
|
||||
tests := []struct {
|
||||
name string
|
||||
work *fakeWork
|
||||
@@ -122,7 +123,7 @@ func TestLeaseFencesStepWrites(t *testing.T) {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
engine := newEngine(nil, tt.work, slog.New(slog.DiscardHandler), Options{})
|
||||
effects := 0
|
||||
engine.Register("probe", func(rc *Context, _ json.RawMessage) (json.RawMessage, error) {
|
||||
engine.Register("probe", func(rc *Context, _ jsontext.Value) (jsontext.Value, error) {
|
||||
out, err := rc.Step("probe", func(context.Context, string) (string, error) {
|
||||
return "mine", nil
|
||||
})
|
||||
@@ -137,7 +138,7 @@ func TestLeaseFencesStepWrites(t *testing.T) {
|
||||
}
|
||||
return json.Marshal(out)
|
||||
})
|
||||
run := store.Run{ID: uuid.New(), OrgID: "org", Workflow: "probe", Input: json.RawMessage(`null`)}
|
||||
run := store.Run{ID: uuid.New(), OrgID: "org", Workflow: "probe", Input: jsontext.Value(`null`)}
|
||||
engine.execute(t.Context(), run, uuid.New())
|
||||
|
||||
if got := len(tt.work.steps); got != tt.wantSteps {
|
||||
|
||||
Reference in new issue
Block a user