fix(scheduling): fire a schedule's capture into its owning organisation

This commit is contained in:
sam committed 2026-10-27 10:03:29 +11:00
1 parent b705019eb8
commit 334bb8cc40
18 files changed
+77 -21

No files matched your search

@@ -165,6 +165,7 @@ func (s *EDA) InsertCapture(ctx context.Context, c storecontract.CapturedEvent,
Delivery: delivery,
DefaultOrg: string(org.DefaultOrgID),
DefaultWorkspace: string(org.DefaultWorkspaceID),
ScheduleOrg: c.OrgID,
}); err != nil {
return "", fmt.Errorf("edastore: insert capture: %w", err)
}
@@ -522,7 +522,8 @@ const insertCapture = `-- name: InsertCapture :exec
INSERT INTO captures (id, source, remote_addr, content_type, headers, body, authenticated, received_at, unsigned, event_type, delivery, org_id, workspace_id)
SELECT $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11,
COALESCE(s.org_id, $12::text), COALESCE(s.workspace_id, $13::text)
CASE WHEN $2 = 'schedule' AND $12::text <> '' THEN $12::text ELSE COALESCE(s.org_id, $13::text) END,
COALESCE(s.workspace_id, $14::text)
FROM (SELECT 1) intake LEFT JOIN sources s ON s.path = $2
`
@@ -538,6 +539,7 @@ type InsertCaptureParams struct {
Unsigned bool
EventType string
Delivery string
ScheduleOrg string
DefaultOrg string
DefaultWorkspace string
}
@@ -556,6 +558,7 @@ func (q *Queries) InsertCapture(ctx context.Context, arg InsertCaptureParams) er
arg.Unsigned,
arg.EventType,
arg.Delivery,
arg.ScheduleOrg,
arg.DefaultOrg,
arg.DefaultWorkspace,
)
@@ -469,7 +469,7 @@ const startRequestedRuns = `-- name: StartRequestedRuns :many
UPDATE schedule_runs r SET result = 'running', started_at = $1
FROM schedules s
WHERE r.result = 'pending' AND s.id = r.schedule_id
RETURNING r.id, r.schedule_id, r.scheduled_for, s.name, s.built_in
RETURNING r.id, r.schedule_id, r.scheduled_for, s.name, s.built_in, s.owner_id
`
type StartRequestedRunsRow struct {
@@ -478,6 +478,7 @@ type StartRequestedRunsRow struct {
ScheduledFor time.Time
Name string
BuiltIn bool
OwnerID string
}
func (q *Queries) StartRequestedRuns(ctx context.Context, startedAt pgtype.Timestamptz) ([]StartRequestedRunsRow, error) {
@@ -495,6 +496,7 @@ func (q *Queries) StartRequestedRuns(ctx context.Context, startedAt pgtype.Times
&i.ScheduledFor,
&i.Name,
&i.BuiltIn,
&i.OwnerID,
); err != nil {
return nil, err
}
@@ -3,7 +3,8 @@
-- name: InsertCapture :exec
INSERT INTO captures (id, source, remote_addr, content_type, headers, body, authenticated, received_at, unsigned, event_type, delivery, org_id, workspace_id)
SELECT $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11,
COALESCE(s.org_id, @default_org::text), COALESCE(s.workspace_id, @default_workspace::text)
CASE WHEN $2 = 'schedule' AND @schedule_org::text <> '' THEN @schedule_org::text ELSE COALESCE(s.org_id, @default_org::text) END,
COALESCE(s.workspace_id, @default_workspace::text)
FROM (SELECT 1) intake LEFT JOIN sources s ON s.path = $2;
-- name: ListCaptures :many
@@ -38,7 +38,7 @@ WHERE result = 'running' AND started_at < $2;
UPDATE schedule_runs r SET result = 'running', started_at = $1
FROM schedules s
WHERE r.result = 'pending' AND s.id = r.schedule_id
RETURNING r.id, r.schedule_id, r.scheduled_for, s.name, s.built_in;
RETURNING r.id, r.schedule_id, r.scheduled_for, s.name, s.built_in, s.owner_id;
-- name: LockDueSchedules :many
SELECT * FROM schedules
+12 -2
View File
@@ -10,6 +10,7 @@ import (
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgtype"
"github.com/samcharles93/archie-core/internal/domain/org"
"github.com/samcharles93/archie-core/internal/domain/scheduling"
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/postgresdb"
)
@@ -223,7 +224,7 @@ func (s *Store) ClaimDue(ctx context.Context, now time.Time, stale time.Duration
for _, r := range requested {
claims = append(claims, scheduling.Claim{
ID: r.ID, ScheduleID: r.ScheduleID, ScheduledFor: r.ScheduledFor, Started: now, Result: scheduling.RunRunning,
Name: r.Name, BuiltIn: r.BuiltIn,
Name: r.Name, BuiltIn: r.BuiltIn, Org: scheduleOrg(r.OwnerID),
})
}
rezoned, err := q.LockStaleZoneCronSchedules(ctx, loc.String())
@@ -250,7 +251,7 @@ func (s *Store) ClaimDue(ctx context.Context, now time.Time, stale time.Duration
case err != nil:
return err
default:
claims = append(claims, scheduling.Claim{Run: runFromRow(run), Name: schedule.Name, BuiltIn: schedule.BuiltIn})
claims = append(claims, scheduling.Claim{Run: runFromRow(run), Name: schedule.Name, BuiltIn: schedule.BuiltIn, Org: scheduleOrg(schedule.Owner.ID)})
}
if err := advance(ctx, q, schedule, now, loc); err != nil {
return err
@@ -264,6 +265,15 @@ func (s *Store) ClaimDue(ctx context.Context, now time.Time, stale time.Duration
return claims, nil
}
// scheduleOrg is the organisation a firing belongs to: the owner's, or the
// system org for an instance schedule, which has no owner id.
func scheduleOrg(ownerID string) string {
if ownerID == "" {
return string(org.DefaultOrgID)
}
return ownerID
}
// advance moves a schedule's next run to its first slot after now.
func advance(ctx context.Context, q *postgresdb.Queries, schedule scheduling.Schedule, now time.Time, loc *time.Location) error {
next, err := nextRun(schedule.Timing, now, loc)