@reventlessdev/reventless-aws 3.0.0-alpha.264 → 3.0.0-alpha.266

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 (27) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/package.json +7 -7
  3. package/src/Platform.res +66 -45
  4. package/src/Platform.res.mjs +31 -35
  5. package/src/adapter/Geocoder/Geocoder_AwsLocation_Resolver.res +227 -0
  6. package/src/adapter/Geocoder/Geocoder_AwsLocation_Resolver.res.mjs +127 -0
  7. package/src/adapter/Geocoder/Geocoder_AwsLocation_Resolver_Ops.res +55 -0
  8. package/src/adapter/Geocoder/Geocoder_AwsLocation_Resolver_Ops.res.mjs +48 -0
  9. package/src/adapter/Runtime/AutomationSliceEntryPoint.mjs +21 -3
  10. package/src/adapter/Runtime/AutomationSliceEntryPoint_Ops.res +219 -27
  11. package/src/adapter/Runtime/AutomationSliceEntryPoint_Ops.res.mjs +121 -7
  12. package/src/adapter/Runtime/AutomationSliceRuntime_Builder_Single.res +118 -0
  13. package/src/adapter/Runtime/AutomationSliceRuntime_Builder_Single.res.mjs +61 -0
  14. package/src/components/OutboundTranslationSlice_Builder.res +15 -0
  15. package/src/components/OutboundTranslationSlice_Builder.res.mjs +11 -1
  16. package/src/plugin/runtime/PluginRuntime_Builder.res +26 -0
  17. package/src/plugin/runtime/PluginRuntime_Builder.res.mjs +26 -0
  18. package/tests/AutomationSliceEntryPoint_OpsTest.res +44 -3
  19. package/tests/AutomationSliceEntryPoint_OpsTest.res.mjs +65 -1
  20. package/tests/Geocoder_AwsLocation_Resolver_OpsTest.res +53 -0
  21. package/tests/Geocoder_AwsLocation_Resolver_OpsTest.res.mjs +55 -0
  22. package/src/adapter/Geocoder/Geocoder_AwsLocation.res +0 -166
  23. package/src/adapter/Geocoder/Geocoder_AwsLocation.res.mjs +0 -105
  24. package/src/adapter/Geocoder/Geocoder_AwsLocation_Ops.res +0 -144
  25. package/src/adapter/Geocoder/Geocoder_AwsLocation_Ops.res.mjs +0 -137
  26. package/tests/Geocoder_AwsLocation_OpsTest.res +0 -78
  27. package/tests/Geocoder_AwsLocation_OpsTest.res.mjs +0 -71
@@ -4,14 +4,20 @@ import * as S from "sury/src/S.res.mjs";
4
4
  import * as Stream from "@reventlessdev/rescript-effect/src/Stream.res.mjs";
5
5
  import * as Stdlib_JSON from "@rescript/runtime/lib/es6/Stdlib_JSON.js";
6
6
  import * as Stdlib_Array from "@rescript/runtime/lib/es6/Stdlib_Array.js";
7
+ import * as Stdlib_JsExn from "@rescript/runtime/lib/es6/Stdlib_JsExn.js";
7
8
  import * as Stdlib_Option from "@rescript/runtime/lib/es6/Stdlib_Option.js";
8
9
  import * as Effect from "effect/Effect";
9
10
  import * as Stream$1 from "effect/Stream";
10
11
  import * as Primitive_option from "@rescript/runtime/lib/es6/Primitive_option.js";
11
12
  import * as DcbDecode$Reventless from "@reventlessdev/reventless-spec/src/components/DcbDecode.res.mjs";
13
+ import * as Primitive_exceptions from "@rescript/runtime/lib/es6/Primitive_exceptions.js";
12
14
  import * as Message$ReventlessCore from "@reventlessdev/reventless-core/src/Message.res.mjs";
15
+ import * as EffectLogger$ReventlessCore from "@reventlessdev/reventless-core/src/util/EffectLogger.res.mjs";
16
+ import * as DynamoDb_Error$ReventlessAws from "../../errors/DynamoDb_Error.res.mjs";
17
+ import * as Util_DynamoDb_Runtime$ReventlessAws from "../../util/Util_DynamoDb_Runtime.res.mjs";
13
18
  import * as AutomationSlice_Callback$ReventlessCore from "@reventlessdev/reventless-core/src/components/AutomationSlice/AutomationSlice_Callback.res.mjs";
14
19
  import * as StreamRoutedEntryPoint_Ops$ReventlessAws from "./StreamRoutedEntryPoint_Ops.res.mjs";
20
+ import * as Geocoder_AwsLocation_Backend$ReventlessAws from "../Geocoder/Geocoder_AwsLocation_Backend.res.mjs";
15
21
  import * as CommandTopicChannel_SQS_Runtime$ReventlessAws from "../CommandTopic/CommandTopicChannel_SQS_Runtime.res.mjs";
16
22
  import * as QueryDbStorage_DynamoDb_Runtime$ReventlessAws from "../QueryDb/QueryDbStorage_DynamoDb_Runtime.res.mjs";
17
23
  import * as OutboundTranslationSlice_Callback$ReventlessCore from "@reventlessdev/reventless-core/src/components/OutboundTranslationSlice/OutboundTranslationSlice_Callback.res.mjs";
@@ -87,8 +93,71 @@ function makeSyncTodoItems(queryDbTableName, todoItems, rowSchema) {
87
93
  };
88
94
  }
89
95
 
90
- function makeAutomationJsonEventsHandler(context, callback, publishJsons, syncTodoItems) {
96
+ function makeLoadTodoItems(queryDbTableName, todoItems, rowSchema, comp) {
97
+ let loaded = {
98
+ contents: false
99
+ };
100
+ return async () => {
101
+ if (loaded.contents) {
102
+ return;
103
+ }
104
+ loaded.contents = true;
105
+ let params_ConsistentRead = true;
106
+ let params_ExpressionAttributeNames = Object.fromEntries([[
107
+ "#s",
108
+ "status"
109
+ ]]);
110
+ let params_ExpressionAttributeValues = Object.fromEntries([
111
+ [
112
+ ":pending",
113
+ "Pending"
114
+ ],
115
+ [
116
+ ":failed",
117
+ "Failed"
118
+ ]
119
+ ]);
120
+ let params_FilterExpression = "#s = :pending OR #s = :failed";
121
+ let params = {
122
+ TableName: queryDbTableName,
123
+ ConsistentRead: params_ConsistentRead,
124
+ ExpressionAttributeNames: params_ExpressionAttributeNames,
125
+ ExpressionAttributeValues: params_ExpressionAttributeValues,
126
+ FilterExpression: params_FilterExpression
127
+ };
128
+ let restored = await Effect.runPromise(Effect.catchAll(Effect.map(Stream.runCollect(Util_DynamoDb_Runtime$ReventlessAws.scanStream(params)), items => Stdlib_Array.reduce(items, 0, (count, item) => {
129
+ let json = JSON.parse(Stdlib_Option.getOr(JSON.stringify(item), ""));
130
+ let id = Stdlib_Option.flatMap(Stdlib_Option.flatMap(Stdlib_JSON.Decode.object(json), d => d["id"]), Stdlib_JSON.Decode.string);
131
+ if (id === undefined) {
132
+ return count;
133
+ }
134
+ if (!Stdlib_Option.isNone(todoItems[id])) {
135
+ return count;
136
+ }
137
+ let row;
138
+ try {
139
+ row = S.parseJsonOrThrow(json, rowSchema);
140
+ } catch (exn) {
141
+ return count;
142
+ }
143
+ todoItems[id] = row;
144
+ return count + 1 | 0;
145
+ })), err => Effect.map(EffectLogger$ReventlessCore.logError(comp, undefined, `restore: couldn't read pending TODO rows from ` + queryDbTableName + `: ` + DynamoDb_Error$ReventlessAws.message(err)), () => 0)));
146
+ if (restored > 0) {
147
+ return Effect.runSync(EffectLogger$ReventlessCore.logInfo(comp, undefined, `restore: reloaded ` + restored.toString() + ` unfinished TODO row(s) from ` + queryDbTableName));
148
+ }
149
+ };
150
+ }
151
+
152
+ function capabilities() {
153
+ return {
154
+ geocode: text => Geocoder_AwsLocation_Backend$ReventlessAws.search(Stdlib_Option.getOr(process.env["PLACE_INDEX_NAME"], ""), text, undefined)
155
+ };
156
+ }
157
+
158
+ function makeAutomationJsonEventsHandler(context, callback, publishJsons, syncTodoItems, loadTodoItems) {
91
159
  return stream => Effect.flatMap(Stream.runCollect(stream), jsons => Effect.promise(async () => {
160
+ await loadTodoItems();
92
161
  callback.phase1(jsons, context);
93
162
  await syncTodoItems();
94
163
  await callback.phase2(publishJsons);
@@ -96,7 +165,7 @@ function makeAutomationJsonEventsHandler(context, callback, publishJsons, syncTo
96
165
  }));
97
166
  }
98
167
 
99
- function makeOutboundJsonEventsHandler(consumedEventSchema, callback, publishJsons, syncTodoItems) {
168
+ function makeOutboundJsonEventsHandler(consumedEventSchema, callback, publishJsons, syncTodoItems, loadTodoItems) {
100
169
  let decoder = DcbDecode$Reventless.makeDecoder(consumedEventSchema);
101
170
  return stream => Effect.flatMap(Stream.runCollect(Stream$1.flatMap(Stream$1.mapEffect(stream, json => Effect.sync(() => {
102
171
  let envelope = Stdlib_JSON.Decode.object(json);
@@ -113,27 +182,69 @@ function makeOutboundJsonEventsHandler(consumedEventSchema, callback, publishJso
113
182
  return [];
114
183
  }
115
184
  })), events => Stream$1.fromIterable(events))), events => Effect.promise(async () => {
185
+ await loadTodoItems();
116
186
  callback.phase1(events);
117
187
  await syncTodoItems();
118
- await callback.phase2(publishJsons);
188
+ await callback.phase2(publishJsons, {
189
+ geocode: text => Geocoder_AwsLocation_Backend$ReventlessAws.search(Stdlib_Option.getOr(process.env["PLACE_INDEX_NAME"], ""), text, undefined)
190
+ });
119
191
  return await syncTodoItems();
120
192
  }));
121
193
  }
122
194
 
123
195
  function makeAutomationRegisteredHandler(entry, sliceName, callback) {
196
+ let comp = `AutomationSlice(` + sliceName + `)`;
197
+ let publishJsons = makePublishJsons(entry.dcbQueueUrl, Stdlib_Option.getOr(entry.commandQueueIsFifo, false));
198
+ let syncTodoItems = makeSyncTodoItems(entry.queryDbTableName, callback.todoItems, AutomationSlice_Callback$ReventlessCore.todoRowSchema);
199
+ let loadTodoItems = makeLoadTodoItems(entry.queryDbTableName, callback.todoItems, AutomationSlice_Callback$ReventlessCore.todoRowSchema, comp);
124
200
  return {
125
- handler: StreamRoutedEntryPoint_Ops$ReventlessAws.toStreamHandler(makeAutomationJsonEventsHandler(Stdlib_Option.getOr(entry.context, defaultContext(sliceName)), callback, makePublishJsons(entry.dcbQueueUrl, Stdlib_Option.getOr(entry.commandQueueIsFifo, false)), makeSyncTodoItems(entry.queryDbTableName, callback.todoItems, AutomationSlice_Callback$ReventlessCore.todoRowSchema))),
126
- comp: `AutomationSlice(` + sliceName + `)`
201
+ registered: {
202
+ handler: StreamRoutedEntryPoint_Ops$ReventlessAws.toStreamHandler(makeAutomationJsonEventsHandler(Stdlib_Option.getOr(entry.context, defaultContext(sliceName)), callback, publishJsons, syncTodoItems, loadTodoItems)),
203
+ comp: comp
204
+ },
205
+ sweep: async () => {
206
+ await loadTodoItems();
207
+ await callback.phase2(publishJsons);
208
+ return await syncTodoItems();
209
+ },
210
+ comp: comp
127
211
  };
128
212
  }
129
213
 
130
214
  function makeOutboundRegisteredHandler(entry, sliceName, consumedEventSchema, callback) {
215
+ let comp = `OutboundTranslationSlice(` + sliceName + `)`;
216
+ let publishJsons = makePublishJsons(entry.dcbQueueUrl, Stdlib_Option.getOr(entry.commandQueueIsFifo, false));
217
+ let syncTodoItems = makeSyncTodoItems(entry.queryDbTableName, callback.todoItems, OutboundTranslationSlice_Callback$ReventlessCore.todoRowSchema);
218
+ let loadTodoItems = makeLoadTodoItems(entry.queryDbTableName, callback.todoItems, OutboundTranslationSlice_Callback$ReventlessCore.todoRowSchema, comp);
131
219
  return {
132
- handler: StreamRoutedEntryPoint_Ops$ReventlessAws.toStreamHandler(makeOutboundJsonEventsHandler(consumedEventSchema, callback, makePublishJsons(entry.dcbQueueUrl, Stdlib_Option.getOr(entry.commandQueueIsFifo, false)), makeSyncTodoItems(entry.queryDbTableName, callback.todoItems, OutboundTranslationSlice_Callback$ReventlessCore.todoRowSchema))),
133
- comp: `OutboundTranslationSlice(` + sliceName + `)`
220
+ registered: {
221
+ handler: StreamRoutedEntryPoint_Ops$ReventlessAws.toStreamHandler(makeOutboundJsonEventsHandler(consumedEventSchema, callback, publishJsons, syncTodoItems, loadTodoItems)),
222
+ comp: comp
223
+ },
224
+ sweep: async () => {
225
+ await loadTodoItems();
226
+ await callback.phase2(publishJsons, {
227
+ geocode: text => Geocoder_AwsLocation_Backend$ReventlessAws.search(Stdlib_Option.getOr(process.env["PLACE_INDEX_NAME"], ""), text, undefined)
228
+ });
229
+ return await syncTodoItems();
230
+ },
231
+ comp: comp
134
232
  };
135
233
  }
136
234
 
235
+ async function runSweeps(slices) {
236
+ for (let i = 0, i_finish = slices.length; i < i_finish; ++i) {
237
+ let slice = slices[i];
238
+ try {
239
+ await slice.sweep();
240
+ } catch (raw_exn) {
241
+ let exn = Primitive_exceptions.internalToException(raw_exn);
242
+ let msg = Stdlib_Option.getOr(Stdlib_Option.flatMap(Stdlib_JsExn.fromException(exn), Stdlib_JsExn.message), "unknown");
243
+ Effect.runSync(EffectLogger$ReventlessCore.logError(slice.comp, undefined, `sweep failed: ` + msg));
244
+ }
245
+ }
246
+ }
247
+
137
248
  export {
138
249
  strOf,
139
250
  decodeContext,
@@ -143,9 +254,12 @@ export {
143
254
  defaultContext,
144
255
  makePublishJsons,
145
256
  makeSyncTodoItems,
257
+ makeLoadTodoItems,
258
+ capabilities,
146
259
  makeAutomationJsonEventsHandler,
147
260
  makeOutboundJsonEventsHandler,
148
261
  makeAutomationRegisteredHandler,
149
262
  makeOutboundRegisteredHandler,
263
+ runSweeps,
150
264
  }
151
265
  /* S Not a pure module */
@@ -181,6 +181,121 @@ let forEventCollector: ReventlessCore.Runtime.forEventCollector<
181
181
  }
182
182
  }
183
183
 
184
+ // How often the shared automation Lambda is invoked with no records, to work
185
+ // its slices' TODO backlogs.
186
+ //
187
+ // Without it a backlog only moves when the *next event* arrives, so a slice
188
+ // whose traffic stops holds its failed items indefinitely — and D4's "retried to
189
+ // maxRetries, then swept" is what an outbound slice is chosen for over a
190
+ // hand-rolled Task. Five minutes is short enough that a transient outage clears
191
+ // on its own without anyone watching, and long enough that the invocations are
192
+ // noise against the event traffic (~288/day per deployment, most of them finding
193
+ // an empty backlog and exiting).
194
+ let sweepIntervalMinutes = 5
195
+
196
+ // The scheduled trigger: a rule, permission for EventBridge to invoke, and a
197
+ // target carrying the constant payload the entry point's shell branches on. The
198
+ // payload is the whole event — a scheduled invocation has no `records`, which is
199
+ // exactly how the shell tells the two apart.
200
+ let makeSweepSchedule = (~runtime: ReventlessCore.Runtime.environment<runtimeParts>, ~opts) => {
201
+ let name = "AllAutomationSlicesSweep"
202
+ let customOpts = opts->ReventlessCore.Util.Pulumi.ComponentResourceOptions.toCustomResourceOptions
203
+
204
+ let rule = {
205
+ open PulumiAws.Cloudwatch
206
+ EventRule.make(
207
+ ~name=Pulumi.Pulumi.getStackName() ++ ("-" ++ name),
208
+ ~args={
209
+ description: "Work the automation/outbound slices' TODO backlogs"->Pulumi.Input.make,
210
+ scheduleExpression: EventRule.ScheduleExpression.every(sweepIntervalMinutes->Minutes),
211
+ tags: AWS.Tags.make(
212
+ ~name=Pulumi.Pulumi.getStackName() ++ ("-" ++ name),
213
+ ~kind=ReventlessCore.ComponentType.AutomationSlice,
214
+ ~role=Scheduler,
215
+ ~component=name,
216
+ ),
217
+ },
218
+ ~opts=customOpts,
219
+ )
220
+ }
221
+
222
+ // The permission and target need the Lambda's resolved arn/name, so they stay
223
+ // inside an apply — the same shape the plugin heartbeat's runner uses.
224
+ let _permissionAndTarget =
225
+ (
226
+ runtime.parts.lambda->Pulumi.Output.flatMap(l => l.arn),
227
+ runtime.parts.lambda->Pulumi.Output.flatMap(l => l.name),
228
+ )
229
+ ->Pulumi.Output.all2
230
+ ->Pulumi.Output.apply(((lambdaArn, lambdaName)) => {
231
+ let _permission = PulumiAws.Lambda.Permission.make(
232
+ ~name,
233
+ ~args={
234
+ action: "lambda:InvokeFunction",
235
+ function: lambdaName->Pulumi.Input.make,
236
+ principal: AWS.CloudwatchEventRule.principal,
237
+ },
238
+ ~opts=customOpts,
239
+ )
240
+ let _target = {
241
+ open PulumiAws.Cloudwatch
242
+ EventTarget.make(
243
+ ~name,
244
+ ~args={
245
+ rule: EventTarget.Rule.ofEventRule(rule),
246
+ arn: lambdaArn->Pulumi.Input.make,
247
+ input: `{"reventlessSweep":true}`->Pulumi.Input.make,
248
+ },
249
+ ~opts=customOpts,
250
+ )
251
+ }
252
+ })
253
+ }
254
+
255
+ // Least-privilege `geo:SearchPlaceIndexForText` on the platform's place index,
256
+ // for the slices that call the geocoder through the injected capability.
257
+ //
258
+ // Only attached when the platform actually provisioned an index — that bit is a
259
+ // plain bool and therefore known synchronously, while the index *name* is an
260
+ // Output. Without the split this would either grant on a nonsense ARN or need
261
+ // `option<Pulumi.Output.t<_>>`, which corrupts the Output proxy.
262
+ //
263
+ // The RolePolicy is created at top level with an Output-valued document, not
264
+ // inside an `apply`. A resource created in an apply callback does not reliably
265
+ // register with the engine — the same defect that intermittently left the
266
+ // heartbeat Lambda without its SQS grant.
267
+ let makeGeocoderGrant = (~runtime: ReventlessCore.Runtime.environment<runtimeParts>, ~opts) =>
268
+ if PluginRuntime_Builder.geocoderProvisioned() {
269
+ let customOpts =
270
+ opts->ReventlessCore.Util.Pulumi.ComponentResourceOptions.toCustomResourceOptions
271
+ let policyJson =
272
+ PluginRuntime_Builder.geocoderPlaceIndex()->Pulumi.Output.apply(idx => {
273
+ open PulumiAws.PolicyDocument
274
+ PulumiAws.PolicyDocument.make(
275
+ ~id="AllAutomationSlicesGeocode",
276
+ ~statements=[
277
+ {
278
+ sid: "AllowGeocode",
279
+ effect: Allow,
280
+ actions: Action("geo:SearchPlaceIndexForText"),
281
+ // Account/region wildcarded, matching the Function URL handler's
282
+ // own policy: the index name is what identifies it, and the stack
283
+ // has no other account to reach.
284
+ resources: Resource(`arn:aws:geo:*:*:place-index/${idx}`),
285
+ },
286
+ ],
287
+ )->PulumiAws.PolicyDocument.toJsonString
288
+ })
289
+ let _ = PulumiAws.IAM.RolePolicy.make(
290
+ ~name="AllAutomationSlicesGeocode",
291
+ ~args={
292
+ policy: policyJson->Pulumi.Output.asInput,
293
+ role: runtime.parts.lambdaRole.id->Pulumi.Output.asInput,
294
+ },
295
+ ~opts=customOpts,
296
+ )
297
+ }
298
+
184
299
  let finished = ref(false)
185
300
 
186
301
  // Legacy finalizer, kept only to satisfy `EventCollectorRuntime_Builder.T`.
@@ -501,6 +616,9 @@ let finishWithDcbEventLog = (dcbEventLog: ReventlessCore.DcbEventLog.component)
501
616
  ~runtime,
502
617
  ~opts,
503
618
  )
619
+
620
+ makeSweepSchedule(~runtime, ~opts)
621
+ makeGeocoderGrant(~runtime, ~opts)
504
622
  | None =>
505
623
  log.warn(
506
624
  ~comp="AutomationSliceRuntime_Builder_Single",
@@ -1,13 +1,22 @@
1
1
  // Generated by ReScript, PLEASE EDIT WITH CARE
2
2
 
3
+ import * as Aws from "@pulumi/aws";
3
4
  import * as Stdlib_Dict from "@rescript/runtime/lib/es6/Stdlib_Dict.js";
4
5
  import * as Stdlib_Array from "@rescript/runtime/lib/es6/Stdlib_Array.js";
6
+ import * as Output$Pulumi from "@reventlessdev/rescript-pulumi-pulumi/src/Output.res.mjs";
5
7
  import * as Stdlib_Option from "@rescript/runtime/lib/es6/Stdlib_Option.js";
6
8
  import * as Pulumi from "@pulumi/pulumi";
7
9
  import * as Stdlib_JsError from "@rescript/runtime/lib/es6/Stdlib_JsError.js";
10
+ import * as Primitive_option from "@rescript/runtime/lib/es6/Primitive_option.js";
11
+ import * as AWS$ReventlessAws from "../AWS.res.mjs";
8
12
  import * as Logger$ReventlessCore from "@reventlessdev/reventless-core/src/util/Logger.res.mjs";
13
+ import * as AWS_Tags$ReventlessAws from "../AWS_Tags.res.mjs";
9
14
  import * as Component$ReventlessCore from "@reventlessdev/reventless-core/src/components/Component.res.mjs";
15
+ import * as PolicyDocument$PulumiAws from "@reventlessdev/rescript-pulumi-aws/src/IAM/PolicyDocument.res.mjs";
10
16
  import * as Util_Bundle$ReventlessAws from "../../util/Util_Bundle.res.mjs";
17
+ import * as Util_Pulumi$ReventlessCore from "@reventlessdev/reventless-core/src/util/Util_Pulumi.res.mjs";
18
+ import * as Cloudwatch_EventRule$PulumiAws from "@reventlessdev/rescript-pulumi-aws/src/Cloudwatch/Cloudwatch_EventRule.res.mjs";
19
+ import * as Cloudwatch_EventTarget$PulumiAws from "@reventlessdev/rescript-pulumi-aws/src/Cloudwatch/Cloudwatch_EventTarget.res.mjs";
11
20
  import * as CommandTopicRegistry$ReventlessAws from "../CommandTopic/CommandTopicRegistry.res.mjs";
12
21
  import * as PluginRuntime_Builder$ReventlessAws from "../../plugin/runtime/PluginRuntime_Builder.res.mjs";
13
22
  import * as RuntimeEnvironment_Lambda$ReventlessAws from "./RuntimeEnvironment_Lambda.res.mjs";
@@ -96,6 +105,51 @@ function forEventCollector(param, eventTopics, resources, memorySizeOpt, timeout
96
105
  log.info("AutomationSliceRuntime_Builder_Single", undefined, `registered ` + eventCollectorName + ` for ` + parentName);
97
106
  }
98
107
 
108
+ function makeSweepSchedule(runtime, opts) {
109
+ let name = "AllAutomationSlicesSweep";
110
+ let customOpts = Util_Pulumi$ReventlessCore.ComponentResourceOptions.toCustomResourceOptions(opts);
111
+ let rule = new (Aws.cloudwatch.EventRule)(Pulumi.getStack() + ("-" + name), {
112
+ description: "Work the automation/outbound slices' TODO backlogs",
113
+ scheduleExpression: Primitive_option.some(Cloudwatch_EventRule$PulumiAws.ScheduleExpression.every({
114
+ TAG: "Minutes",
115
+ _0: 5
116
+ })),
117
+ tags: AWS_Tags$ReventlessAws.make(Pulumi.getStack() + ("-" + name), "AutomationSlice", "Scheduler", undefined, name, undefined, undefined, undefined)
118
+ }, customOpts);
119
+ Pulumi.all([
120
+ Output$Pulumi.flatMap(runtime.parts.lambda, l => l.arn),
121
+ Output$Pulumi.flatMap(runtime.parts.lambda, l => l.name)
122
+ ]).apply(param => {
123
+ new (Aws.lambda.Permission)(name, {
124
+ action: "lambda:InvokeFunction",
125
+ function: param[1],
126
+ principal: AWS$ReventlessAws.CloudwatchEventRule.principal
127
+ }, customOpts);
128
+ new (Aws.cloudwatch.EventTarget)(name, {
129
+ rule: Cloudwatch_EventTarget$PulumiAws.Rule.ofEventRule(rule),
130
+ arn: param[0],
131
+ input: `{"reventlessSweep":true}`
132
+ }, customOpts);
133
+ });
134
+ }
135
+
136
+ function makeGeocoderGrant(runtime, opts) {
137
+ if (!PluginRuntime_Builder$ReventlessAws.geocoderProvisioned()) {
138
+ return;
139
+ }
140
+ let customOpts = Util_Pulumi$ReventlessCore.ComponentResourceOptions.toCustomResourceOptions(opts);
141
+ let policyJson = PluginRuntime_Builder$ReventlessAws.geocoderPlaceIndex().apply(idx => PolicyDocument$PulumiAws.toJsonString(PolicyDocument$PulumiAws.make(undefined, "AllAutomationSlicesGeocode", [{
142
+ Sid: "AllowGeocode",
143
+ Effect: "Allow",
144
+ Action: "geo:SearchPlaceIndexForText",
145
+ Resource: `arn:aws:geo:*:*:place-index/` + idx
146
+ }])));
147
+ new (Aws.iam.RolePolicy)("AllAutomationSlicesGeocode", {
148
+ policy: policyJson,
149
+ role: runtime.parts.lambdaRole.id
150
+ }, customOpts);
151
+ }
152
+
99
153
  let finished = {
100
154
  contents: false
101
155
  };
@@ -271,6 +325,8 @@ function finishWithDcbEventLog(dcbEventLog) {
271
325
  eventTopics: eventTopics,
272
326
  resources: allQueryDbResources
273
327
  }], runtime, opts);
328
+ makeSweepSchedule(runtime, opts);
329
+ makeGeocoderGrant(runtime, opts);
274
330
  } else {
275
331
  log.warn("AutomationSliceRuntime_Builder_Single", undefined, "finishWithDcbEventLog: DCB EventLog has no parent");
276
332
  }
@@ -282,6 +338,8 @@ let EventCollectorChannel;
282
338
 
283
339
  let RuntimeEnvironment;
284
340
 
341
+ let sweepIntervalMinutes = 5;
342
+
285
343
  export {
286
344
  EventCollectorChannel,
287
345
  RuntimeEnvironment,
@@ -296,6 +354,9 @@ export {
296
354
  storedSpecs,
297
355
  grandParent,
298
356
  forEventCollector,
357
+ sweepIntervalMinutes,
358
+ makeSweepSchedule,
359
+ makeGeocoderGrant,
299
360
  finished,
300
361
  finish,
301
362
  finishWithDcbEventLog,
@@ -14,6 +14,20 @@ module EventCollectorRuntimeBuilder = {
14
14
  let finish = Inner.finish
15
15
  }
16
16
 
17
+ // On AWS the core builder's in-process handler is never the thing that runs a
18
+ // translation: `forEventCollector` registers module paths, and the shared
19
+ // `AllAutomationSlices` Lambda rebuilds the callback from them and drives phase 2
20
+ // itself. So the capabilities that matter are the ones
21
+ // `AutomationSliceEntryPoint_Ops.capabilities` builds at runtime, from the
22
+ // Lambda's environment — where the place index name actually is.
23
+ //
24
+ // This deploy-time value therefore has no caller. `none` rather than a real
25
+ // geocoder because inventing one here would suggest a path that does not exist,
26
+ // and because a deploy-time module has no environment to read anyway.
27
+ module DeployTimeCapabilities = {
28
+ let capabilities = () => Reventless.Capabilities.none
29
+ }
30
+
17
31
  module Make = (Api: {
18
32
  let api: unit => Types.AppSync.api
19
33
  let apiRole: unit => Types.AppSync.role
@@ -25,6 +39,7 @@ module Make = (Api: {
25
39
  EventCollectorChannel,
26
40
  EventCollectorRuntimeBuilder,
27
41
  Api,
42
+ DeployTimeCapabilities,
28
43
  )
29
44
 
30
45
  module Make = (
@@ -1,6 +1,7 @@
1
1
  // Generated by ReScript, PLEASE EDIT WITH CARE
2
2
 
3
3
  import * as Belt_SetString from "@rescript/runtime/lib/es6/Belt_SetString.js";
4
+ import * as Capabilities$Reventless from "@reventlessdev/reventless-spec/src/semantic/Capabilities.res.mjs";
4
5
  import * as Component$ReventlessCore from "@reventlessdev/reventless-core/src/components/Component.res.mjs";
5
6
  import * as EventTopic$ReventlessCore from "@reventlessdev/reventless-core/src/components/EventTopic/EventTopic.res.mjs";
6
7
  import * as Util_Bundle$ReventlessAws from "../util/Util_Bundle.res.mjs";
@@ -18,6 +19,14 @@ let EventCollectorRuntimeBuilder = {
18
19
  finish: AutomationSliceRuntime_Builder_Single$ReventlessAws.finish
19
20
  };
20
21
 
22
+ function capabilities() {
23
+ return Capabilities$Reventless.none;
24
+ }
25
+
26
+ let DeployTimeCapabilities = {
27
+ capabilities: capabilities
28
+ };
29
+
21
30
  function Make(Api) {
22
31
  let Inner = OutboundTranslationSlice_Builder$ReventlessCore.Make({
23
32
  make: RuntimeEnvironment_Lambda$ReventlessAws.make,
@@ -42,7 +51,7 @@ function Make(Api) {
42
51
  },
43
52
  forEventCollector: AutomationSliceRuntime_Builder_Single$ReventlessAws.forEventCollector,
44
53
  finish: AutomationSliceRuntime_Builder_Single$ReventlessAws.finish
45
- })(Api);
54
+ })(Api)(DeployTimeCapabilities);
46
55
  let Make$1 = Spec => (Translation => {
47
56
  let InnerMake = Inner.Make(Spec)(Translation);
48
57
  let make = (dcbEventLog, allEventTopicsOpt, publishJsons, runtime, opts) => {
@@ -80,6 +89,7 @@ export {
80
89
  EventCollectorChannel,
81
90
  RuntimeEnvironment,
82
91
  EventCollectorRuntimeBuilder,
92
+ DeployTimeCapabilities,
83
93
  Make,
84
94
  }
85
95
  /* Component-ReventlessCore Not a pure module */
@@ -51,6 +51,32 @@ let registerCapabilityEnv = (name: string, value: Pulumi.Output.t<string>) =>
51
51
  capability, so this returns `[]` rather than throwing. */
52
52
  let capabilityEnv = () => capabilityEnvRef->Dict.toArray
53
53
 
54
+ /**
55
+ The geocoding place index, when the platform provisioned one.
56
+
57
+ Separate from `capabilityEnv` because an env dict cannot express a *grant*: a
58
+ slice Lambda calling Amazon Location needs `geo:SearchPlaceIndexForText` on this
59
+ index, and its role is created later, in the slice runtime's finalizer. So the
60
+ name travels twice — once as a variable the runtime reads, once as the resource
61
+ its role must be allowed to touch.
62
+
63
+ A plain Output with `""` for "not provisioned", plus a separate bool for "was it
64
+ registered at all", rather than `ref<option<Pulumi.Output.t<_>>>`: that shape
65
+ compiles to `Option.getOr`, whose nested-option probe corrupts the Output proxy.
66
+ The bool is an ordinary value and is known synchronously, which is what the
67
+ finalizer needs in order to decide whether to attach a policy at all.
68
+ */
69
+ let geocoderPlaceIndexRef: ref<Pulumi.Output.t<string>> = ref(Pulumi.Output.make(""))
70
+ let geocoderProvisionedRef = ref(false)
71
+
72
+ let registerGeocoderPlaceIndex = (indexName: Pulumi.Output.t<string>) => {
73
+ geocoderPlaceIndexRef := indexName
74
+ geocoderProvisionedRef := true
75
+ }
76
+
77
+ let geocoderPlaceIndex = () => geocoderPlaceIndexRef.contents
78
+ let geocoderProvisioned = () => geocoderProvisionedRef.contents
79
+
54
80
  type sliceModulePaths = {
55
81
  specPath: string,
56
82
  behaviorPath: string,
@@ -44,6 +44,27 @@ function capabilityEnv() {
44
44
  return Object.entries(capabilityEnvRef);
45
45
  }
46
46
 
47
+ let geocoderPlaceIndexRef = {
48
+ contents: Pulumi.output("")
49
+ };
50
+
51
+ let geocoderProvisionedRef = {
52
+ contents: false
53
+ };
54
+
55
+ function registerGeocoderPlaceIndex(indexName) {
56
+ geocoderPlaceIndexRef.contents = indexName;
57
+ geocoderProvisionedRef.contents = true;
58
+ }
59
+
60
+ function geocoderPlaceIndex() {
61
+ return geocoderPlaceIndexRef.contents;
62
+ }
63
+
64
+ function geocoderProvisioned() {
65
+ return geocoderProvisionedRef.contents;
66
+ }
67
+
47
68
  let dcbConfigRef = {
48
69
  contents: {
49
70
  pluginName: "",
@@ -506,6 +527,11 @@ export {
506
527
  capabilityEnvRef,
507
528
  registerCapabilityEnv,
508
529
  capabilityEnv,
530
+ geocoderPlaceIndexRef,
531
+ geocoderProvisionedRef,
532
+ registerGeocoderPlaceIndex,
533
+ geocoderPlaceIndex,
534
+ geocoderProvisioned,
509
535
  dcbConfigRef,
510
536
  registeredSliceModulePaths,
511
537
  registerStateChangeSliceSpec,
@@ -112,6 +112,7 @@ describe("AutomationSliceEntryPoint_Ops.makeAutomationJsonEventsHandler", () =>
112
112
  ~callback,
113
113
  ~publishJsons=noopPublish,
114
114
  ~syncTodoItems=async () => steps->Array.push("sync"),
115
+ ~loadTodoItems=async () => steps->Array.push("restore"),
115
116
  )
116
117
  let envelope = obj([("meta", obj([("service", str("Order"))])), ("event", str("Placed"))])
117
118
  await Stream.fromIterable([envelope, str("bare")])->handler->Effect.runPromise
@@ -120,7 +121,43 @@ describe("AutomationSliceEntryPoint_Ops.makeAutomationJsonEventsHandler", () =>
120
121
  expect(receivedCtx.contents->Option.map(c => c.Reventless.AutomationSlice.sliceName))->toEqual(
121
122
  Some("S"),
122
123
  )
123
- expect(steps)->toEqual(["phase1", "sync", "phase2", "sync"])
124
+ // `restore` first: rehydrating the TODO list after phase 1 would let a stored
125
+ // row overwrite one this batch just collected, and after phase 2 would leave
126
+ // the reloaded backlog unprocessed until another invocation.
127
+ expect(steps)->toEqual(["restore", "phase1", "sync", "phase2", "sync"])
128
+ })
129
+ })
130
+
131
+ describe("AutomationSliceEntryPoint_Ops.runSweeps", () => {
132
+ let dummyRegistered: StreamRoutedEntryPoint_Ops.registeredHandler = {
133
+ handler: (_event, _context) => Effect.succeed(),
134
+ }
135
+ let sliceThat = (~comp, ~run): AutomationSliceEntryPoint_Ops.builtSlice => {
136
+ registered: dummyRegistered,
137
+ sweep: run,
138
+ comp,
139
+ }
140
+
141
+ test("sweeps every slice", async () => {
142
+ let swept = []
143
+ await AutomationSliceEntryPoint_Ops.runSweeps([
144
+ sliceThat(~comp="A", ~run=async () => swept->Array.push("a")),
145
+ sliceThat(~comp="B", ~run=async () => swept->Array.push("b")),
146
+ ])
147
+ expect(swept)->toEqual(["a", "b"])
148
+ })
149
+
150
+ // A sweep exists to recover from a failing external call, so one slice whose
151
+ // geocoder is still down must not cost every other slice on the Lambda its
152
+ // turn — that would be a worse version of the problem being fixed.
153
+ test("a throwing slice does not stop the others", async () => {
154
+ let swept = []
155
+ await AutomationSliceEntryPoint_Ops.runSweeps([
156
+ sliceThat(~comp="A", ~run=async () => swept->Array.push("a")),
157
+ sliceThat(~comp="Boom", ~run=async () => JsError.throwWithMessage("gateway down")),
158
+ sliceThat(~comp="C", ~run=async () => swept->Array.push("c")),
159
+ ])
160
+ expect(swept)->toEqual(["a", "c"])
124
161
  })
125
162
  })
126
163
 
@@ -142,13 +179,14 @@ describe("AutomationSliceEntryPoint_Ops.makeOutboundJsonEventsHandler", () => {
142
179
  received := events
143
180
  steps->Array.push("phase1")
144
181
  },
145
- phase2: async _publish => steps->Array.push("phase2"),
182
+ phase2: async (_publish, ~capabilities as _) => steps->Array.push("phase2"),
146
183
  }
147
184
  let handler = AutomationSliceEntryPoint_Ops.makeOutboundJsonEventsHandler(
148
185
  ~consumedEventSchema=outboundEventSchema,
149
186
  ~callback,
150
187
  ~publishJsons=noopPublish,
151
188
  ~syncTodoItems=async () => steps->Array.push("sync"),
189
+ ~loadTodoItems=async () => steps->Array.push("restore"),
152
190
  )
153
191
  // `id` is the envelope's subject — the entity the event was published for.
154
192
  // An Aggregate's event payload does not repeat it, so this is the only place
@@ -170,6 +208,9 @@ describe("AutomationSliceEntryPoint_Ops.makeOutboundJsonEventsHandler", () => {
170
208
  ("ord-1", OrderPlaced({orderId: "ord-1"})),
171
209
  ("", OrderArchived),
172
210
  ])
173
- expect(steps)->toEqual(["phase1", "sync", "phase2", "sync"])
211
+ // `restore` first: rehydrating the TODO list after phase 1 would let a stored
212
+ // row overwrite one this batch just collected, and after phase 2 would leave
213
+ // the reloaded backlog unprocessed until another invocation.
214
+ expect(steps)->toEqual(["restore", "phase1", "sync", "phase2", "sync"])
174
215
  })
175
216
  })