@reventlessdev/reventless-core 3.0.0-alpha.295 → 3.0.0-alpha.297

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (29) hide show
  1. package/CHANGELOG.md +33 -0
  2. package/package.json +6 -6
  3. package/src/admin/Platform_ComponentDefinitionsApi.res +3 -1
  4. package/src/admin/Platform_ComponentDefinitionsApi.res.mjs +5 -1
  5. package/src/components/Api/GraphQL_FragmentGenerator.res +15 -0
  6. package/src/components/Api/GraphQL_FragmentGenerator.res.mjs +13 -0
  7. package/src/components/AutomationSlice/AutomationSlice.res +2 -0
  8. package/src/components/AutomationSlice/AutomationSlice_Builder.res +126 -9
  9. package/src/components/AutomationSlice/AutomationSlice_Builder.res.mjs +90 -10
  10. package/src/components/AutomationSlice/AutomationSlice_Callback.res +122 -1
  11. package/src/components/AutomationSlice/AutomationSlice_Callback.res.mjs +101 -2
  12. package/src/components/AutomationSlice/AutomationSlice_Route.res +3 -1
  13. package/src/components/AutomationSlice/AutomationSlice_Route.res.mjs +2 -1
  14. package/src/components/AutomationSlice/AutomationSlice_Schedules.res +149 -0
  15. package/src/components/AutomationSlice/AutomationSlice_Schedules.res.mjs +193 -0
  16. package/src/components/Dcb/Dcb_Builder.res +35 -4
  17. package/src/components/Dcb/Dcb_Builder.res.mjs +33 -5
  18. package/src/components/InboundTranslationSlice/InboundTranslationSlice_Builder.res +1 -1
  19. package/src/components/InboundTranslationSlice/InboundTranslationSlice_Builder.res.mjs +3 -1
  20. package/src/components/OutboundTranslationSlice/OutboundTranslationSlice_Builder.res +1 -1
  21. package/src/components/OutboundTranslationSlice/OutboundTranslationSlice_Builder.res.mjs +3 -1
  22. package/src/plugin/component/Plugin_Builder.res +10 -3
  23. package/src/plugin/component/Plugin_Builder.res.mjs +8 -2
  24. package/src/plugin/component/Plugin_Structure.res +7 -0
  25. package/src/plugin/component/Plugin_Structure.res.mjs +5 -2
  26. package/tests/admin/AdminApiSchemaDriftTest.res +1 -0
  27. package/tests/admin/AdminApiSchemaDriftTest.res.mjs +4 -1
  28. package/tests/automationslice/AutomationSliceSchedulesTest.res +143 -0
  29. package/tests/automationslice/AutomationSliceSchedulesTest.res.mjs +247 -0
package/CHANGELOG.md CHANGED
@@ -3,6 +3,39 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ # 3.0.0-alpha.297 (2026-10-16)
7
+
8
+ * fix(auth)!: only operators read the framework's own stores ([a2fd202](https://github.com/ReventlessDev/reventless-core/commit/a2fd2022b6876dae855b185df7f8263353fc971d))
9
+
10
+ ### BREAKING CHANGES
11
+
12
+ * a caller outside the elevated groups is refused the
13
+ framework's to-do, schedule and audit stores. On AWS, a roles-gated
14
+ view's …ByIds, …Refs, …Items and …By<Index> fields refuse callers
15
+ outside its roles.
16
+
17
+
18
+
19
+ # 3.0.0-alpha.296 (2026-10-16)
20
+
21
+ ### Bug Fixes
22
+
23
+ * **core:** a core test names the schedules module directly ([ac459e5](https://github.com/ReventlessDev/reventless-core/commit/ac459e5ece6479086b86f86019ed1176b4471c9b))
24
+ * feat!(automation): schedules set by events ([53bc436](https://github.com/ReventlessDev/reventless-core/commit/53bc4363dffce81ef36475728bf0dae704716021))
25
+ * feat!(automation): declared schedules tick an automation ([420acd2](https://github.com/ReventlessDev/reventless-core/commit/420acd2233aadf700141a31d9d3e0fdc8074bef7))
26
+ ### Features
27
+
28
+ * **examples:** a marketing digest at the time each customer chooses ([3521b10](https://github.com/ReventlessDev/reventless-core/commit/3521b10c4f3ff64f3e4561e94e051892cdfe5d5f))
29
+
30
+ ### BREAKING CHANGES
31
+
32
+ * a hand-written AutomationSlice.Mapping.Make the PPX does
33
+ not see must define `let reschedule = _ => []`.
34
+ * a hand-written AutomationSlice spec the PPX does not see must
35
+ declare `let schedule = None`.
36
+
37
+
38
+
6
39
  # 3.0.0-alpha.295 (2026-10-15)
7
40
 
8
41
  * feat!: an automation's process takes ~now, and collect the event's recorded time ([8c8f3f5](https://github.com/ReventlessDev/reventless-core/commit/8c8f3f570474b1658b605b726077debc7d6a1dc7))
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reventlessdev/reventless-core",
3
- "version": "3.0.0-alpha.295",
3
+ "version": "3.0.0-alpha.297",
4
4
  "description": "Core package for Reventless framework",
5
5
  "license": "Apache-2.0",
6
6
  "jest": {
@@ -28,22 +28,22 @@
28
28
  "dependencies": {
29
29
  "sury": "11.0.0",
30
30
  "uuid": "^13.0.0",
31
+ "@reventlessdev/rescript-jest": "1.0.0-alpha.12",
31
32
  "@reventlessdev/rescript-effect": "1.0.0-alpha.0",
32
33
  "@reventlessdev/rescript-hash-object": "1.2.0-alpha.15",
33
34
  "@reventlessdev/rescript-fast-csv": "2.0.0-alpha.22",
34
- "@reventlessdev/rescript-jest": "1.0.0-alpha.12",
35
+ "@reventlessdev/rescript-node": "2.0.0-alpha.22",
35
36
  "@reventlessdev/rescript-pulumi-pulumi": "2.3.0-alpha.23",
36
37
  "@reventlessdev/rescript-ssh2": "2.0.0-alpha.23",
37
- "@reventlessdev/rescript-node": "2.0.0-alpha.22",
38
38
  "@reventlessdev/rescript-uuid": "2.0.0-alpha.1",
39
- "@reventlessdev/reventless-infra": "3.0.0-alpha.192",
40
39
  "@reventlessdev/reventless-interop": "3.0.0-alpha.40",
41
- "@reventlessdev/reventless-spec": "3.0.0-alpha.164"
40
+ "@reventlessdev/reventless-infra": "3.0.0-alpha.194",
41
+ "@reventlessdev/reventless-spec": "3.0.0-alpha.166"
42
42
  },
43
43
  "devDependencies": {
44
44
  "rescript": "12.3.1",
45
45
  "sury-ppx": "11.0.0",
46
- "@reventlessdev/reventless-ppx": "1.0.0-alpha.100"
46
+ "@reventlessdev/reventless-ppx": "1.0.0-alpha.102"
47
47
  },
48
48
  "peerDependencies": {
49
49
  "rescript": "^12.3.1"
@@ -30,7 +30,7 @@ let sdlTypes: array<string> = [
30
30
  // def may decline to state them, and a state whose key cannot be resolved has no
31
31
  // `idField` to report.
32
32
  `type Platform_ReadSideDef {\n name: String!\n queryField: String!\n schema: String!\n consumedEventTypes: [String!]!\n linkedWriteSide: [String!]!\n labelField: String!\n searchableFields: [String!]!\n labelFieldSource: String\n lifecycleField: String\n visibility: String\n chapter: String\n singleQueryField: String\n idField: String\n idFieldSource: String\n requiredAccess: [String!]\n ownerField: String\n retiredField: String\n retiredValues: [String!]\n namedWhenRetired: Boolean!\n}`,
33
- `type Platform_AutomationSliceDef {\n name: String!\n consumedEventTypes: [String!]!\n producedCommandTypes: [String!]!\n targetName: String\n chapter: String\n}`,
33
+ `type Platform_AutomationSliceDef {\n name: String!\n consumedEventTypes: [String!]!\n producedCommandTypes: [String!]!\n targetName: String\n chapter: String\n schedule: String\n}`,
34
34
  `type Platform_OutboundTranslationSliceDef {\n name: String!\n consumedEventTypes: [String!]!\n inboundCommandTypes: [String!]!\n targetName: String\n externalSystem: String\n chapter: String\n consumedSources: [String!]\n}`,
35
35
  `type Platform_InboundTranslationSliceDef {\n name: String!\n commandTypes: [String!]!\n targetName: String\n externalSystem: String\n chapter: String\n}`,
36
36
  // The subscriber's half of the port's translation table — see `handledEventDef`.
@@ -170,6 +170,8 @@ let encodeAutomationSliceDef = (a: automationSliceDef): JSON.t =>
170
170
  // from the structure rather than from the deploy-time inspector sync.
171
171
  ("targetName", JSON.Encode.string(a.targetName)),
172
172
  ("chapter", a.chapter->Option.mapOr(JSON.Encode.null, JSON.Encode.string)),
173
+ // The declared time, as its JSON text; null for an automation started by events only.
174
+ ("schedule", a.schedule->Option.mapOr(JSON.Encode.null, JSON.Encode.string)),
173
175
  ])->JSON.Encode.object
174
176
 
175
177
  let encodeOutboundTranslationSliceDef = (o: outboundTranslationSliceDef): JSON.t =>
@@ -11,7 +11,7 @@ let sdlTypes = [
11
11
  `type Platform_ErrorDef {\n name: String!\n schema: String!\n references: [Platform_FieldReference!]!\n}`,
12
12
  `type Platform_WriteSideDef {\n name: String!\n commands: [Platform_CommandDef!]!\n linkedViews: [String!]!\n consistencyRead: String\n producedEventTypes: [String!]!\n consumedEventTypes: [String!]!\n events: [Platform_EventDef!]!\n errors: [Platform_ErrorDef!]!\n chapter: String\n}`,
13
13
  `type Platform_ReadSideDef {\n name: String!\n queryField: String!\n schema: String!\n consumedEventTypes: [String!]!\n linkedWriteSide: [String!]!\n labelField: String!\n searchableFields: [String!]!\n labelFieldSource: String\n lifecycleField: String\n visibility: String\n chapter: String\n singleQueryField: String\n idField: String\n idFieldSource: String\n requiredAccess: [String!]\n ownerField: String\n retiredField: String\n retiredValues: [String!]\n namedWhenRetired: Boolean!\n}`,
14
- `type Platform_AutomationSliceDef {\n name: String!\n consumedEventTypes: [String!]!\n producedCommandTypes: [String!]!\n targetName: String\n chapter: String\n}`,
14
+ `type Platform_AutomationSliceDef {\n name: String!\n consumedEventTypes: [String!]!\n producedCommandTypes: [String!]!\n targetName: String\n chapter: String\n schedule: String\n}`,
15
15
  `type Platform_OutboundTranslationSliceDef {\n name: String!\n consumedEventTypes: [String!]!\n inboundCommandTypes: [String!]!\n targetName: String\n externalSystem: String\n chapter: String\n consumedSources: [String!]\n}`,
16
16
  `type Platform_InboundTranslationSliceDef {\n name: String!\n commandTypes: [String!]!\n targetName: String\n externalSystem: String\n chapter: String\n}`,
17
17
  `type Platform_HandledEventDef {\n name: String!\n toCommandTypes: [String!]!\n}`,
@@ -278,6 +278,10 @@ function encodeAutomationSliceDef(a) {
278
278
  [
279
279
  "chapter",
280
280
  Stdlib_Option.mapOr(a.chapter, null, prim => prim)
281
+ ],
282
+ [
283
+ "schedule",
284
+ Stdlib_Option.mapOr(a.schedule, null, prim => prim)
281
285
  ]
282
286
  ]);
283
287
  }
@@ -777,6 +777,21 @@ let mutationArgTypes = (~fieldName: string, variantSchema: S.t<unknown>): option
777
777
  out
778
778
  })
779
779
 
780
+ /** Every query field `generate` emits for an entry, under the same conditions.
781
+ A platform gating an entry gates each of them: a door left out is open. */
782
+ let queryFieldNamesOf = (entry: querySchemaEntry): array<string> => {
783
+ let byIds = entry.includeIdParam->Option.getOr(true) && entry.subIdField === None
784
+ let indexFields =
785
+ entry.indexQueries
786
+ ->Option.getOr([])
787
+ ->Array.filter(ic => !Reventless.ReadModel.isDerivedIndex(ic))
788
+ ->Array.map(ic => indexQueryFieldName(~singleFieldName=entry.singleFieldName, ~index=ic.index))
789
+ [entry.singleFieldName, entry.listFieldName]
790
+ ->Array.concat(byIds ? [entry.listFieldName ++ "ByIds", entry.listFieldName ++ "Refs"] : [])
791
+ ->Array.concat(entry.subIdField->Option.isSome ? [entry.singleFieldName ++ "Items"] : [])
792
+ ->Array.concat(indexFields)
793
+ }
794
+
780
795
  // ── Main generate function ─────────────────────────────────────────────────
781
796
 
782
797
  let generate = (
@@ -529,6 +529,18 @@ function mutationArgTypes(fieldName, variantSchema) {
529
529
  });
530
530
  }
531
531
 
532
+ function queryFieldNamesOf(entry) {
533
+ let byIds = Stdlib_Option.getOr(entry.includeIdParam, true) && entry.subIdField === undefined;
534
+ let indexFields = Stdlib_Option.getOr(entry.indexQueries, []).filter(ic => !ReadModel$Reventless.isDerivedIndex(ic)).map(ic => indexQueryFieldName(entry.singleFieldName, ic.index));
535
+ return [
536
+ entry.singleFieldName,
537
+ entry.listFieldName
538
+ ].concat(byIds ? [
539
+ entry.listFieldName + "ByIds",
540
+ entry.listFieldName + "Refs"
541
+ ] : []).concat(Stdlib_Option.isSome(entry.subIdField) ? [entry.singleFieldName + "Items"] : []).concat(indexFields);
542
+ }
543
+
532
544
  function generate(mutationEntries, queryEntries) {
533
545
  let types = [];
534
546
  let mutations = [];
@@ -729,6 +741,7 @@ export {
729
741
  deriveIndexQueryField,
730
742
  deriveMutationFieldFromObject,
731
743
  mutationArgTypes,
744
+ queryFieldNamesOf,
732
745
  generate,
733
746
  }
734
747
  /* log Not a pure module */
@@ -26,6 +26,8 @@ module type T = {
26
26
  module Automation: Reventless.AutomationSlice.Automation with module Spec := Spec
27
27
  type component = Component.t<t, outputs, operations>
28
28
  let queryDbName: string
29
+ /** The schedules store's name, for a slice that maps the `Clock`. */
30
+ let schedulesDbName: option<string>
29
31
  /** Names of all sources this slice consumes (deduplicated). Used by
30
32
  `Plugin_Builder` for the source-name fail-fast assembly check. */
31
33
  let sourceNames: array<string>
@@ -22,6 +22,10 @@ module Make = (
22
22
  let api: unit => QueryDbStorage.api
23
23
  let apiRole: unit => QueryDbStorage.role
24
24
  },
25
+ // The platform's schedules of a slice, for the runtime this builder's operations run on.
26
+ Schedules: {
27
+ let platformFor: (~slice: string) => ReventlessInfra.AutomationSlice.platformSchedules
28
+ },
25
29
  ) => {
26
30
  let finish = EventCollectorRuntimeBuilder.finish
27
31
  module Make = (
@@ -39,12 +43,16 @@ module Make = (
39
43
  // Names of all sources this slice subscribes to (deduplicated). Multiple
40
44
  // mappings can share a source name (e.g., two mappings reading the same
41
45
  // Aggregate's events for different reasons).
42
- let sourceNames =
46
+ let mappedSources =
43
47
  Automation.mappings
44
48
  ->Array.map((module(M: Automation.Mapping)) => M.sourceName)
45
49
  ->Belt.Set.String.fromArray
46
50
  ->Belt.Set.String.toArray
47
51
 
52
+ // The event-topic sources; `Clock` is delivered by the slice's schedules, not a topic.
53
+ let sourceNames = mappedSources->Array.filter(name => name != Reventless.Clock.name)
54
+ let readsClock = mappedSources->Array.includes(Reventless.Clock.name)
55
+
48
56
  // QueryDb for TODO list — stores todoRow keyed by string ID
49
57
  module TodoQueryDbSpec = {
50
58
  module Id = Reventless.Id.String
@@ -55,11 +63,60 @@ module Make = (
55
63
  let config = Reventless.ReadModel.config()
56
64
  let subIdConfig: option<Reventless.ReadModel.subIdConfig<state>> = None
57
65
  type role = Reventless.Role.name
58
- let authorization: Reventless.Authorization.rule<role> = AllowAuthenticated
66
+ let authorization: Reventless.Authorization.rule<role> = Reventless.OwnerScope.operators()
59
67
  let visibility: Reventless.Visibility.t = Public
60
68
  }
61
69
 
62
70
  module SpecificQueryDb = QueryDb_Builder.Make(TodoQueryDbSpec, QueryDbStorage, QueryDbResolvers)
71
+
72
+ // The schedules store: a row per key, made only for a slice that maps the Clock.
73
+ let schedulesStoreName = Spec.name ++ "Schedule"
74
+ let schedulesDbName = readsClock ? Some(schedulesStoreName) : None
75
+ module SchedulesQueryDbSpec = {
76
+ module Id = Reventless.Id.String
77
+ let name = schedulesStoreName
78
+ let moduleUrl: string = %raw(`import.meta.url`)
79
+ type state = AutomationSlice_Schedules.row
80
+ let stateSchema = AutomationSlice_Schedules.rowSchema
81
+ let config = Reventless.ReadModel.config()
82
+ let subIdConfig: option<Reventless.ReadModel.subIdConfig<state>> = None
83
+ type role = Reventless.Role.name
84
+ let authorization: Reventless.Authorization.rule<role> = Reventless.OwnerScope.operators()
85
+ let visibility: Reventless.Visibility.t = Public
86
+ }
87
+ module SchedulesQueryDb = QueryDb_Builder.Make(
88
+ SchedulesQueryDbSpec,
89
+ QueryDbStorage,
90
+ QueryDbResolvers,
91
+ )
92
+
93
+ // Written whole, and a key the rows no longer hold is deleted.
94
+ let syncSchedules = async (
95
+ ops: option<SchedulesQueryDb.operations>,
96
+ ~stored: ref<array<string>>,
97
+ ) =>
98
+ switch ops {
99
+ | Some(ops) =>
100
+ let keys = Callback.schedules->Dict.keysToArray
101
+ for i in 0 to keys->Array.length - 1 {
102
+ let key = keys->Array.getUnsafe(i)
103
+ let _ = await ops.save(
104
+ key->Reventless.Id.String.makeFromString,
105
+ Callback.schedules->Dict.getUnsafe(key),
106
+ QueryDb.Overwrite,
107
+ None,
108
+ )
109
+ }
110
+ let gone = stored.contents->Array.filter(key => !(keys->Array.includes(key)))
111
+ for i in 0 to gone->Array.length - 1 {
112
+ let _ = await ops.delete(
113
+ gone->Array.getUnsafe(i)->Reventless.Id.String.makeFromString,
114
+ None,
115
+ )
116
+ }
117
+ stored := keys
118
+ | None => ()
119
+ }
63
120
  module SpecificEventCollector = EventCollector_Builder.Make(
64
121
  RuntimeEnvironment,
65
122
  EventCollectorChannel,
@@ -116,6 +173,32 @@ module Make = (
116
173
  }
117
174
  )
118
175
 
176
+ // A `Clock` mapping receives ticks only from a declared schedule, or from one an
177
+ // event sets through another mapping's `reschedule`.
178
+ if readsClock && Spec.schedule->Option.isNone && sourceNames->Array.length == 0 {
179
+ JsError.throwWithMessage(
180
+ `AutomationSlice "${Spec.name}" maps the Clock source but declares no schedule ` ++
181
+ `and reads no event that could set one, so no tick will ever reach it. ` ++ `Declare \`let schedule\` on its spec.`,
182
+ )
183
+ }
184
+
185
+ let schedulesDb = readsClock
186
+ ? Some(
187
+ SchedulesQueryDb.make(
188
+ ~api=Api.api(),
189
+ ~apiRole=Api.apiRole(),
190
+ ~owner={kind: ComponentType.AutomationSlice, name: Spec.name},
191
+ ~opts,
192
+ ),
193
+ )
194
+ : None
195
+ let schedulesOps: Pulumi.Output.t<option<SchedulesQueryDb.operations>> = switch schedulesDb {
196
+ | Some(db) => db->Component.operations->Pulumi.Output.apply(ops => Some(ops))
197
+ | None => Pulumi.Output.make(None)
198
+ }
199
+ let platform = Schedules.platformFor(~slice=Spec.name)
200
+ let storedSchedules = ref([])
201
+
119
202
  // Filter to only the topics this slice consumes from.
120
203
  let sourceSet = sourceNames->Belt.Set.String.fromArray
121
204
  let eventTopics = allEventTopics->EventTopic.filter(sourceSet)
@@ -127,9 +210,9 @@ module Make = (
127
210
  // first event arrival (which silently dropped Phase 2 in the previous
128
211
  // shape).
129
212
  let eventCollector =
130
- (queryDb->Component.operations, publishJsons)
131
- ->Pulumi.Output.all2
132
- ->Pulumi.Output.apply(((queryDbOps, publishJsonsFn)) => {
213
+ (queryDb->Component.operations, publishJsons, schedulesOps)
214
+ ->Pulumi.Output.all3
215
+ ->Pulumi.Output.apply(((queryDbOps, publishJsonsFn, schedulesOps)) => {
133
216
  let ec = SpecificEventCollector.make(
134
217
  ~name=Spec.name,
135
218
  ~eventTopics,
@@ -148,6 +231,13 @@ module Make = (
148
231
  // Sync the post-phase-1 TODO state so consumers awaiting the
149
232
  // originating publishEvent observe Pending rows immediately.
150
233
  await syncToQueryDb(queryDbOps)
234
+ // The schedule rows are stored first, then the platform follows them.
235
+ switch Callback.takeScheduleChanges() {
236
+ | [] => ()
237
+ | changes =>
238
+ await syncSchedules(schedulesOps, ~stored=storedSchedules)
239
+ await AutomationSlice_Schedules.tell(platform, changes)
240
+ }
151
241
  // Phase 2 must NOT be awaited here. Phase 2 publishes commands
152
242
  // whose downstream events get fanned out by the bus to all
153
243
  // subscribers of this same topic — including this very fiber.
@@ -194,15 +284,41 @@ module Make = (
194
284
  eventCollector->Pulumi.Output.flatMap(ec => ec->Component.operations),
195
285
  publishJsons,
196
286
  queryDb->Component.operations,
287
+ schedulesOps,
197
288
  )
198
- ->Pulumi.Output.all3
199
- ->Pulumi.Output.apply(((ecOps, publishJsonsFn, queryDbOps)) => {
289
+ ->Pulumi.Output.all4
290
+ ->Pulumi.Output.apply(((ecOps, publishJsonsFn, queryDbOps, schedulesOps)) => {
291
+ let processPending = async () => {
292
+ await Callback.phase2(publishJsonsFn)
293
+ await syncToQueryDb(queryDbOps)
294
+ }
295
+ let reconcileSchedules = async () =>
296
+ if readsClock {
297
+ await AutomationSlice_Schedules.reconcile(
298
+ Callback.schedules,
299
+ platform,
300
+ ~declaredTime=Spec.schedule,
301
+ ~now=Date.now(),
302
+ )
303
+ await syncSchedules(schedulesOps, ~stored=storedSchedules)
304
+ }
200
305
  let ops: AutomationSlice.operations = {
201
306
  enqueueEvent: ecOps.enqueueEvent,
202
307
  // As the AWS sweep does: the rows phase 2 moved are written back.
203
- processPending: async () => {
204
- await Callback.phase2(publishJsonsFn)
308
+ processPending,
309
+ tick: async (~key, ~at) => {
310
+ Callback.phase1([AutomationSlice_Callback.tickEvent(~key, ~at)], context)
205
311
  await syncToQueryDb(queryDbOps)
312
+ // A once-only schedule's row went when it fired, and a Clock mapping
313
+ // may change schedules too.
314
+ await syncSchedules(schedulesOps, ~stored=storedSchedules)
315
+ await AutomationSlice_Schedules.tell(platform, Callback.takeScheduleChanges())
316
+ await processPending()
317
+ },
318
+ reconcileSchedules,
319
+ rebuildSchedules: async events => {
320
+ Callback.rebuildSchedules(events)
321
+ await reconcileSchedules()
206
322
  },
207
323
  }
208
324
  ops
@@ -217,6 +333,7 @@ module Make = (
217
333
  let outputs: AutomationSlice.outputs = {
218
334
  resources: aggregatedResources,
219
335
  queryDb: queryDb->Component.outputs,
336
+ schedulesDb: ?(schedulesDb->Option.map(db => db->Component.outputs)),
220
337
  }
221
338
  self->Component.setOutputs(outputs)
222
339
  }
@@ -11,8 +11,11 @@ import * as Pulumi from "@pulumi/pulumi";
11
11
  import * as Belt_SetString from "@rescript/runtime/lib/es6/Belt_SetString.js";
12
12
  import * as Stdlib_JsError from "@rescript/runtime/lib/es6/Stdlib_JsError.js";
13
13
  import * as Stdlib_Promise from "@rescript/runtime/lib/es6/Stdlib_Promise.js";
14
+ import * as Clock$Reventless from "@reventlessdev/reventless-spec/src/components/Clock.res.mjs";
15
+ import * as Primitive_option from "@rescript/runtime/lib/es6/Primitive_option.js";
14
16
  import * as Primitive_string from "@rescript/runtime/lib/es6/Primitive_string.js";
15
17
  import * as ReadModel$Reventless from "@reventlessdev/reventless-spec/src/components/ReadModel.res.mjs";
18
+ import * as OwnerScope$Reventless from "@reventlessdev/reventless-spec/src/types/OwnerScope.res.mjs";
16
19
  import * as Component$ReventlessCore from "../Component.res.mjs";
17
20
  import * as EventTopic$ReventlessCore from "../EventTopic/EventTopic.res.mjs";
18
21
  import * as EffectLogger$ReventlessCore from "../../util/EffectLogger.res.mjs";
@@ -22,15 +25,19 @@ import * as AutomationSlice$ReventlessCore from "./AutomationSlice.res.mjs";
22
25
  import * as QueryDb_Builder$ReventlessCore from "../QueryDb/QueryDb_Builder.res.mjs";
23
26
  import * as EventCollector_Builder$ReventlessCore from "../EventCollector/EventCollector_Builder.res.mjs";
24
27
  import * as AutomationSlice_Callback$ReventlessCore from "./AutomationSlice_Callback.res.mjs";
28
+ import * as AutomationSlice_Schedules$ReventlessCore from "./AutomationSlice_Schedules.res.mjs";
25
29
 
26
30
  function Make(RuntimeEnvironment) {
27
- return QueryDbStorage => (QueryDbResolvers => (EventCollectorChannel => (EventCollectorRuntimeBuilder => (Api => {
31
+ return QueryDbStorage => (QueryDbResolvers => (EventCollectorChannel => (EventCollectorRuntimeBuilder => (Api => (Schedules => {
28
32
  let Make = Spec => (Automation => {
29
33
  let Callback = AutomationSlice_Callback$ReventlessCore.Make(Spec)(Automation);
30
34
  let queryDbName = Spec.name + "Todo";
31
- let sourceNames = Belt_SetString.toArray(Belt_SetString.fromArray(Automation.mappings.map(M => M.sourceName)));
35
+ let mappedSources = Belt_SetString.toArray(Belt_SetString.fromArray(Automation.mappings.map(M => M.sourceName)));
36
+ let sourceNames = mappedSources.filter(name => name !== Clock$Reventless.name);
37
+ let readsClock = mappedSources.includes(Clock$Reventless.name);
32
38
  let moduleUrl = import.meta.url;
33
39
  let config = ReadModel$Reventless.config(undefined, undefined, undefined);
40
+ let authorization = OwnerScope$Reventless.operators();
34
41
  let SpecificQueryDb = QueryDb_Builder$ReventlessCore.Make({
35
42
  Id: Id$Reventless.$$String,
36
43
  name: queryDbName,
@@ -38,9 +45,39 @@ function Make(RuntimeEnvironment) {
38
45
  stateSchema: AutomationSlice_Callback$ReventlessCore.todoRowSchema,
39
46
  config: config,
40
47
  subIdConfig: undefined,
41
- authorization: "AllowAuthenticated",
48
+ authorization: authorization,
42
49
  visibility: "Public"
43
50
  })(QueryDbStorage)(QueryDbResolvers);
51
+ let schedulesStoreName = Spec.name + "Schedule";
52
+ let schedulesDbName = readsClock ? schedulesStoreName : undefined;
53
+ let moduleUrl$1 = import.meta.url;
54
+ let config$1 = ReadModel$Reventless.config(undefined, undefined, undefined);
55
+ let authorization$1 = OwnerScope$Reventless.operators();
56
+ let SchedulesQueryDb = QueryDb_Builder$ReventlessCore.Make({
57
+ Id: Id$Reventless.$$String,
58
+ name: schedulesStoreName,
59
+ moduleUrl: moduleUrl$1,
60
+ stateSchema: AutomationSlice_Schedules$ReventlessCore.rowSchema,
61
+ config: config$1,
62
+ subIdConfig: undefined,
63
+ authorization: authorization$1,
64
+ visibility: "Public"
65
+ })(QueryDbStorage)(QueryDbResolvers);
66
+ let syncSchedules = async (ops, stored) => {
67
+ if (ops === undefined) {
68
+ return;
69
+ }
70
+ let keys = Object.keys(Callback.schedules);
71
+ for (let i = 0, i_finish = keys.length; i < i_finish; ++i) {
72
+ let key = keys[i];
73
+ await ops.save(Id$Reventless.$$String.makeFromString(key), Callback.schedules[key], "Overwrite", undefined);
74
+ }
75
+ let gone = stored.contents.filter(key => !keys.includes(key));
76
+ for (let i$1 = 0, i_finish$1 = gone.length; i$1 < i_finish$1; ++i$1) {
77
+ await ops.delete(Id$Reventless.$$String.makeFromString(gone[i$1]), undefined);
78
+ }
79
+ stored.contents = keys;
80
+ };
44
81
  let SpecificEventCollector = EventCollector_Builder$ReventlessCore.Make(RuntimeEnvironment)(EventCollectorChannel);
45
82
  let syncToQueryDb = async queryDbOps => {
46
83
  let items = Object.entries(Callback.todoItems);
@@ -67,12 +104,26 @@ function Make(RuntimeEnvironment) {
67
104
  let availableNames = Object.keys(allEventTopics).toSorted(Primitive_string.compare).join(", ");
68
105
  Stdlib_JsError.throwWithMessage(`AutomationSlice "` + Spec.name + `" has a Mapping with sourceName "` + sourceName + `", but no EventTopic with that key exists in allEventTopics. ` + (`Available source names: [` + availableNames + `]. `) + `Check Mapping.Make's first arg matches an Aggregate Spec.name or a DCB source name (typically "<pluginName>DcbEventLog").`);
69
106
  });
107
+ if (readsClock && Stdlib_Option.isNone(Spec.schedule) && sourceNames.length === 0) {
108
+ Stdlib_JsError.throwWithMessage(`AutomationSlice "` + Spec.name + `" maps the Clock source but declares no schedule and reads no event that could set one, so no tick will ever reach it. Declare \`let schedule\` on its spec.`);
109
+ }
110
+ let schedulesDb = readsClock ? Primitive_option.some(SchedulesQueryDb.make(Api.api(), Api.apiRole(), undefined, {
111
+ kind: "AutomationSlice",
112
+ name: Spec.name
113
+ }, opts)) : undefined;
114
+ let schedulesOps = schedulesDb !== undefined ? Component$ReventlessCore.operations(Primitive_option.valFromOption(schedulesDb)).apply(ops => ops) : Pulumi.output(undefined);
115
+ let platform = Schedules.platformFor(Spec.name);
116
+ let storedSchedules = {
117
+ contents: []
118
+ };
70
119
  let sourceSet = Belt_SetString.fromArray(sourceNames);
71
120
  let eventTopics = EventTopic$ReventlessCore.filter(allEventTopics, sourceSet);
72
121
  let eventCollector = Pulumi.all([
73
122
  Component$ReventlessCore.operations(queryDb),
74
- publishJsons
123
+ publishJsons,
124
+ schedulesOps
75
125
  ]).apply(param => {
126
+ let schedulesOps = param[2];
76
127
  let publishJsonsFn = param[1];
77
128
  let queryDbOps = param[0];
78
129
  let ec = SpecificEventCollector.make(Spec.name, eventTopics, {
@@ -82,6 +133,11 @@ function Make(RuntimeEnvironment) {
82
133
  let jsonEventsHandler = stream => Effect.flatMap(Stream.runCollect(stream), jsons => Effect.promise(async () => {
83
134
  Callback.phase1(jsons, context);
84
135
  await syncToQueryDb(queryDbOps);
136
+ let changes = Callback.takeScheduleChanges();
137
+ if (changes.length !== 0) {
138
+ await syncSchedules(schedulesOps, storedSchedules);
139
+ await AutomationSlice_Schedules$ReventlessCore.tell(platform, changes);
140
+ }
85
141
  Stdlib_Promise.$$catch(Callback.phase2(publishJsonsFn).then(() => syncToQueryDb(queryDbOps)), exn => {
86
142
  let errMsg = Stdlib_Option.getOr(Stdlib_Option.flatMap(Stdlib_JsExn.fromException(exn), Stdlib_JsExn.message), "unknown");
87
143
  Effect.runSync(EffectLogger$ReventlessCore.logError(`AutomationSlice(` + Spec.name + `)`, undefined, `detached phase 2 error: ` + errMsg));
@@ -96,23 +152,46 @@ function Make(RuntimeEnvironment) {
96
152
  Component$ReventlessCore.setOperations(extra, Pulumi.all([
97
153
  Output$Pulumi.flatMap(eventCollector, Component$ReventlessCore.operations),
98
154
  publishJsons,
99
- Component$ReventlessCore.operations(queryDb)
155
+ Component$ReventlessCore.operations(queryDb),
156
+ schedulesOps
100
157
  ]).apply(param => {
158
+ let schedulesOps = param[3];
101
159
  let queryDbOps = param[2];
102
160
  let publishJsonsFn = param[1];
161
+ let processPending = async () => {
162
+ await Callback.phase2(publishJsonsFn);
163
+ return await syncToQueryDb(queryDbOps);
164
+ };
165
+ let reconcileSchedules = async () => {
166
+ if (readsClock) {
167
+ await AutomationSlice_Schedules$ReventlessCore.reconcile(Callback.schedules, platform, Spec.schedule, Date.now());
168
+ return await syncSchedules(schedulesOps, storedSchedules);
169
+ }
170
+ };
103
171
  return {
104
172
  enqueueEvent: param[0].enqueueEvent,
105
- processPending: async () => {
106
- await Callback.phase2(publishJsonsFn);
107
- return await syncToQueryDb(queryDbOps);
173
+ processPending: processPending,
174
+ tick: async (key, at) => {
175
+ Callback.phase1([AutomationSlice_Callback$ReventlessCore.tickEvent(key, at)], context);
176
+ await syncToQueryDb(queryDbOps);
177
+ await syncSchedules(schedulesOps, storedSchedules);
178
+ await AutomationSlice_Schedules$ReventlessCore.tell(platform, Callback.takeScheduleChanges());
179
+ return await processPending();
180
+ },
181
+ reconcileSchedules: reconcileSchedules,
182
+ rebuildSchedules: async events => {
183
+ Callback.rebuildSchedules(events);
184
+ return await reconcileSchedules();
108
185
  }
109
186
  };
110
187
  }));
111
188
  let aggregatedResources = Object.values(eventTopics).flatMap(t => t.resources);
112
189
  let outputs_queryDb = Component$ReventlessCore.outputs(queryDb);
190
+ let outputs_schedulesDb = Stdlib_Option.map(schedulesDb, Component$ReventlessCore.outputs);
113
191
  let outputs = {
114
192
  resources: aggregatedResources,
115
- queryDb: outputs_queryDb
193
+ queryDb: outputs_queryDb,
194
+ schedulesDb: outputs_schedulesDb
116
195
  };
117
196
  return Component$ReventlessCore.setOutputs(extra, outputs);
118
197
  }, opts);
@@ -120,6 +199,7 @@ function Make(RuntimeEnvironment) {
120
199
  Spec: Spec,
121
200
  Automation: Automation,
122
201
  queryDbName: queryDbName,
202
+ schedulesDbName: schedulesDbName,
123
203
  sourceNames: sourceNames,
124
204
  make: make
125
205
  };
@@ -128,7 +208,7 @@ function Make(RuntimeEnvironment) {
128
208
  finish: EventCollectorRuntimeBuilder.finish,
129
209
  Make: Make
130
210
  };
131
- }))));
211
+ })))));
132
212
  }
133
213
 
134
214
  export {