diff --git a/internal/app/archied/scheduling.go b/internal/app/archied/scheduling.go index 163bf1ba5..da8910fed 100644 --- a/internal/app/archied/scheduling.go +++ b/internal/app/archied/scheduling.go @@ -129,7 +129,7 @@ func (s *scheduleSource) Due(ctx context.Context, now time.Time) ([]scheduling.J } jobs = append(jobs, scheduling.Job{ ID: c.ScheduleID, RunID: c.ID, Pool: pool, Detail: c.Name, - ScheduledFor: c.ScheduledFor, BuiltIn: c.BuiltIn, + ScheduledFor: c.ScheduledFor, BuiltIn: c.BuiltIn, Org: c.Org, }) } return jobs, nil @@ -192,7 +192,7 @@ type scheduleFirer struct { func (f scheduleFirer) Run(ctx context.Context, job scheduling.Job) error { captureID, err := f.firer.Fire(ctx, source.ScheduleFired{ - ScheduleID: job.ID, Name: job.Detail, ScheduledFor: job.ScheduledFor, FiredAt: time.Now(), + ScheduleID: job.ID, Name: job.Detail, ScheduledFor: job.ScheduledFor, FiredAt: time.Now(), Org: job.Org, }) if err != nil { return err diff --git a/internal/contracts/state/v1/capture.pb.go b/internal/contracts/state/v1/capture.pb.go index 1c0400679..0e10e4c9d 100644 --- a/internal/contracts/state/v1/capture.pb.go +++ b/internal/contracts/state/v1/capture.pb.go @@ -41,7 +41,10 @@ type CapturedEvent struct { // delivery names the sender's delivery; a retry of one delivery shares it. // Intake sets it from the source's delivery header; empty takes the // capture ID. - Delivery string `protobuf:"bytes,11,opt,name=delivery,proto3" json:"delivery,omitempty"` + Delivery string `protobuf:"bytes,11,opt,name=delivery,proto3" json:"delivery,omitempty"` + // org_id names the organisation of a schedule-fired capture. Other + // captures take the org of their source and ignore it. + OrgId string `protobuf:"bytes,12,opt,name=org_id,json=orgId,proto3" json:"org_id,omitempty"` unknownFields protoimpl.UnknownFields sizeCache protoimpl.SizeCache } @@ -153,6 +156,13 @@ func (x *CapturedEvent) GetDelivery() string { return "" } +func (x *CapturedEvent) GetOrgId() string { + if x != nil { + return x.OrgId + } + return "" +} + type RecordCaptureRefusalRequest struct { state protoimpl.MessageState `protogen:"open.v1"` Source string `protobuf:"bytes,1,opt,name=source,proto3" json:"source,omitempty"` @@ -701,7 +711,7 @@ var File_state_v1_capture_proto protoreflect.FileDescriptor const file_state_v1_capture_proto_rawDesc = "" + "\n" + - "\x16state/v1/capture.proto\x12\bstate.v1\x1a\x1fgoogle/protobuf/timestamp.proto\"\xe3\x02\n" + + "\x16state/v1/capture.proto\x12\bstate.v1\x1a\x1fgoogle/protobuf/timestamp.proto\"\xfa\x02\n" + "\rCapturedEvent\x12\x0e\n" + "\x02id\x18\x01 \x01(\tR\x02id\x12;\n" + "\vreceived_at\x18\x02 \x01(\v2\x1a.google.protobuf.TimestampR\n" + @@ -717,7 +727,8 @@ const file_state_v1_capture_proto_rawDesc = "" + "event_type\x18\t \x01(\tR\teventType\x12\x1a\n" + "\bunsigned\x18\n" + " \x01(\bR\bunsigned\x12\x1a\n" + - "\bdelivery\x18\v \x01(\tR\bdelivery\"\x84\x01\n" + + "\bdelivery\x18\v \x01(\tR\bdelivery\x12\x15\n" + + "\x06org_id\x18\f \x01(\tR\x05orgId\"\x84\x01\n" + "\x1bRecordCaptureRefusalRequest\x12\x16\n" + "\x06source\x18\x01 \x01(\tR\x06source\x12\x12\n" + "\x04addr\x18\x02 \x01(\tR\x04addr\x129\n" + diff --git a/internal/contracts/state/v1/schedule.pb.go b/internal/contracts/state/v1/schedule.pb.go index 757af8d68..6a12b5130 100644 --- a/internal/contracts/state/v1/schedule.pb.go +++ b/internal/contracts/state/v1/schedule.pb.go @@ -371,10 +371,13 @@ func (x *ScheduleRun) GetCaptureId() string { } type ScheduleClaim struct { - state protoimpl.MessageState `protogen:"open.v1"` - Run *ScheduleRun `protobuf:"bytes,1,opt,name=run,proto3" json:"run,omitempty"` - Name string `protobuf:"bytes,2,opt,name=name,proto3" json:"name,omitempty"` - BuiltIn bool `protobuf:"varint,3,opt,name=built_in,json=builtIn,proto3" json:"built_in,omitempty"` + state protoimpl.MessageState `protogen:"open.v1"` + Run *ScheduleRun `protobuf:"bytes,1,opt,name=run,proto3" json:"run,omitempty"` + Name string `protobuf:"bytes,2,opt,name=name,proto3" json:"name,omitempty"` + BuiltIn bool `protobuf:"varint,3,opt,name=built_in,json=builtIn,proto3" json:"built_in,omitempty"` + // org_id is the organisation the firing belongs to: the owner of an org + // schedule, the system org for an instance schedule. + OrgId string `protobuf:"bytes,4,opt,name=org_id,json=orgId,proto3" json:"org_id,omitempty"` unknownFields protoimpl.UnknownFields sizeCache protoimpl.SizeCache } @@ -430,6 +433,13 @@ func (x *ScheduleClaim) GetBuiltIn() bool { return false } +func (x *ScheduleClaim) GetOrgId() string { + if x != nil { + return x.OrgId + } + return "" +} + type ListSchedulesRequest struct { state protoimpl.MessageState `protogen:"open.v1"` unknownFields protoimpl.UnknownFields @@ -1417,11 +1427,12 @@ const file_state_v1_schedule_proto_rawDesc = "" + "\x06result\x18\x06 \x01(\tR\x06result\x12\x14\n" + "\x05error\x18\a \x01(\tR\x05error\x12\x1d\n" + "\n" + - "capture_id\x18\b \x01(\tR\tcaptureId\"g\n" + + "capture_id\x18\b \x01(\tR\tcaptureId\"~\n" + "\rScheduleClaim\x12'\n" + "\x03run\x18\x01 \x01(\v2\x15.state.v1.ScheduleRunR\x03run\x12\x12\n" + "\x04name\x18\x02 \x01(\tR\x04name\x12\x19\n" + - "\bbuilt_in\x18\x03 \x01(\bR\abuiltIn\"\x16\n" + + "\bbuilt_in\x18\x03 \x01(\bR\abuiltIn\x12\x15\n" + + "\x06org_id\x18\x04 \x01(\tR\x05orgId\"\x16\n" + "\x14ListSchedulesRequest\"I\n" + "\x15ListSchedulesResponse\x120\n" + "\tschedules\x18\x01 \x03(\v2\x12.state.v1.ScheduleR\tschedules\"$\n" + diff --git a/internal/domain/scheduling/contract.go b/internal/domain/scheduling/contract.go index d2de4f783..3b7df16a0 100644 --- a/internal/domain/scheduling/contract.go +++ b/internal/domain/scheduling/contract.go @@ -57,6 +57,8 @@ type Job struct { Detail string ScheduledFor time.Time BuiltIn bool + // Org is the organisation the run belongs to. + Org string } // Validate checks the fields the engine relies on. diff --git a/internal/domain/scheduling/model.go b/internal/domain/scheduling/model.go index 7d7c7725e..f3c37f840 100644 --- a/internal/domain/scheduling/model.go +++ b/internal/domain/scheduling/model.go @@ -110,6 +110,9 @@ type Claim struct { Run Name string BuiltIn bool + // Org is the organisation the firing belongs to: the owner of an org + // schedule, the system org for an instance schedule. + Org string } // Builtin is a schedule defined in code, seeded once and then owned by the diff --git a/internal/domain/source/schedule.go b/internal/domain/source/schedule.go index 539265df6..750bdc39a 100644 --- a/internal/domain/source/schedule.go +++ b/internal/domain/source/schedule.go @@ -25,6 +25,8 @@ type ScheduleFired struct { Name string `json:"name"` ScheduledFor time.Time `json:"scheduled_for"` FiredAt time.Time `json:"fired_at"` + // Org is where the capture lands; it is not part of the payload. + Org string `json:"-"` } // Delivery names the firing of one slot. A retried firing of the slot shares diff --git a/internal/domain/storecontract/storecontract.go b/internal/domain/storecontract/storecontract.go index a7f946647..cda7ccdde 100644 --- a/internal/domain/storecontract/storecontract.go +++ b/internal/domain/storecontract/storecontract.go @@ -392,6 +392,9 @@ type CapturedEvent struct { // it, so a binding dispatches each delivery once. Intake sets it from the // source's delivery header; the store fills the capture ID when empty. Delivery string `json:"delivery"` + // OrgID is honoured only for a schedule-fired capture, whose source is + // reserved and so names no org; every other capture takes its source's. + OrgID string `json:"org_id,omitempty"` } // Dispatchable reports whether a binding may start a task from this event: diff --git a/internal/infrastructure/postgres/edastore.go b/internal/infrastructure/postgres/edastore.go index e50c8ad02..020566f35 100644 --- a/internal/infrastructure/postgres/edastore.go +++ b/internal/infrastructure/postgres/edastore.go @@ -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) } diff --git a/internal/infrastructure/postgres/postgresdb/edastore.sql.go b/internal/infrastructure/postgres/postgresdb/edastore.sql.go index 1741ce664..a1759ca03 100644 --- a/internal/infrastructure/postgres/postgresdb/edastore.sql.go +++ b/internal/infrastructure/postgres/postgresdb/edastore.sql.go @@ -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, ) diff --git a/internal/infrastructure/postgres/postgresdb/schedules.sql.go b/internal/infrastructure/postgres/postgresdb/schedules.sql.go index 26b2ef7b4..e6c942969 100644 --- a/internal/infrastructure/postgres/postgresdb/schedules.sql.go +++ b/internal/infrastructure/postgres/postgresdb/schedules.sql.go @@ -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 } diff --git a/internal/infrastructure/postgres/queries/edastore.sql b/internal/infrastructure/postgres/queries/edastore.sql index 04991441f..211affc46 100644 --- a/internal/infrastructure/postgres/queries/edastore.sql +++ b/internal/infrastructure/postgres/queries/edastore.sql @@ -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 diff --git a/internal/infrastructure/postgres/queries/schedules.sql b/internal/infrastructure/postgres/queries/schedules.sql index 158214c41..4b54ac86d 100644 --- a/internal/infrastructure/postgres/queries/schedules.sql +++ b/internal/infrastructure/postgres/queries/schedules.sql @@ -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 diff --git a/internal/infrastructure/postgres/schedules.go b/internal/infrastructure/postgres/schedules.go index a37e2bfa7..8137cc272 100644 --- a/internal/infrastructure/postgres/schedules.go +++ b/internal/infrastructure/postgres/schedules.go @@ -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) diff --git a/internal/infrastructure/schedulecapture/firer.go b/internal/infrastructure/schedulecapture/firer.go index 374f18891..28506bb10 100644 --- a/internal/infrastructure/schedulecapture/firer.go +++ b/internal/infrastructure/schedulecapture/firer.go @@ -39,6 +39,7 @@ func (f Firer) Fire(ctx context.Context, fired source.ScheduleFired) (string, er Body: string(body), Authenticated: true, Delivery: fired.Delivery(), + OrgID: fired.Org, }, f.Retention, f.MaxEvents) if err != nil { return "", fmt.Errorf("schedulecapture: capture firing of %q: %w", fired.ScheduleID, err) diff --git a/internal/infrastructure/staterpc/schedules.go b/internal/infrastructure/staterpc/schedules.go index 70ed366a7..35deba3cb 100644 --- a/internal/infrastructure/staterpc/schedules.go +++ b/internal/infrastructure/staterpc/schedules.go @@ -141,7 +141,7 @@ func (s *scheduleServer) ClaimDueRuns(ctx context.Context, r *pb.ClaimDueRunsReq return nil, s.logErr("ClaimDueRuns", err) } return &pb.ClaimDueRunsResponse{Claims: mapValues(claims, func(c scheduling.Claim) *pb.ScheduleClaim { - return &pb.ScheduleClaim{Run: scheduleRunProto(c.Run), Name: c.Name, BuiltIn: c.BuiltIn} + return &pb.ScheduleClaim{Run: scheduleRunProto(c.Run), Name: c.Name, BuiltIn: c.BuiltIn, OrgId: c.Org} })}, nil } @@ -300,7 +300,7 @@ func (c *Client) ClaimDueRuns(ctx context.Context, now time.Time, stale time.Dur return nil, scheduleError(err) } return mapValues(r.GetClaims(), func(p *pb.ScheduleClaim) scheduling.Claim { - return scheduling.Claim{Run: scheduleRunValue(p.GetRun()), Name: p.GetName(), BuiltIn: p.GetBuiltIn()} + return scheduling.Claim{Run: scheduleRunValue(p.GetRun()), Name: p.GetName(), BuiltIn: p.GetBuiltIn(), Org: p.GetOrgId()} }), nil } diff --git a/internal/infrastructure/staterpc/values.go b/internal/infrastructure/staterpc/values.go index 5d9f55880..a0b0877f7 100644 --- a/internal/infrastructure/staterpc/values.go +++ b/internal/infrastructure/staterpc/values.go @@ -204,7 +204,7 @@ func capturedEventProto(c storecontract.CapturedEvent) *pb.CapturedEvent { Id: c.ID, ReceivedAt: timestamp(c.ReceivedAt), Source: c.Source, RemoteAddr: c.RemoteAddr, ContentType: c.ContentType, Headers: c.Headers, Body: c.Body, Authenticated: c.Authenticated, EventType: c.EventType, - Unsigned: c.Unsigned, Delivery: c.Delivery, + Unsigned: c.Unsigned, Delivery: c.Delivery, OrgId: c.OrgID, } } @@ -216,7 +216,7 @@ func capturedEventValue(c *pb.CapturedEvent) storecontract.CapturedEvent { ID: c.Id, ReceivedAt: timeValue(c.ReceivedAt), Source: c.Source, RemoteAddr: c.RemoteAddr, ContentType: c.ContentType, Headers: c.Headers, Body: c.Body, Authenticated: c.Authenticated, EventType: c.EventType, - Unsigned: c.Unsigned, Delivery: c.Delivery, + Unsigned: c.Unsigned, Delivery: c.Delivery, OrgID: c.OrgId, } } diff --git a/proto/state/v1/capture.proto b/proto/state/v1/capture.proto index d71a0ca2a..758c1b0fb 100644 --- a/proto/state/v1/capture.proto +++ b/proto/state/v1/capture.proto @@ -36,6 +36,9 @@ message CapturedEvent { // Intake sets it from the source's delivery header; empty takes the // capture ID. string delivery = 11; + // org_id names the organisation of a schedule-fired capture. Other + // captures take the org of their source and ignore it. + string org_id = 12; } message RecordCaptureRefusalRequest { string source = 1; diff --git a/proto/state/v1/schedule.proto b/proto/state/v1/schedule.proto index 0b0f98b30..34e8ab9c1 100644 --- a/proto/state/v1/schedule.proto +++ b/proto/state/v1/schedule.proto @@ -66,6 +66,9 @@ message ScheduleClaim { ScheduleRun run = 1; string name = 2; bool built_in = 3; + // org_id is the organisation the firing belongs to: the owner of an org + // schedule, the system org for an instance schedule. + string org_id = 4; } message ListSchedulesRequest {}