Skip to content

Commit 959f206

Browse files
authored
feat(sdk): use DurablePromise in runInChildContext, step and waitForCallback #293 (#295)
- use DurablePromise in step - use DurablePromise in runInChildContext - use DurablePromise in waitForCallback - make tests less vebose - add test for asserting runInChildContext works with data near and above limit. - add more description to DurablePromise
1 parent 912cf4c commit 959f206

File tree

32 files changed

+913
-331
lines changed

32 files changed

+913
-331
lines changed

packages/aws-durable-execution-sdk-js-examples/src/examples/block-example/block-example.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
withDurableExecution,
44
} from "@aws/durable-execution-sdk-js";
55
import { ExampleConfig } from "../../types";
6+
import { log } from "../../utils/logger";
67

78
export const config: ExampleConfig = {
89
name: "Block Example",
@@ -11,19 +12,19 @@ export const config: ExampleConfig = {
1112

1213
export const handler = withDurableExecution(
1314
async (event: any, context: DurableContext) => {
14-
console.log("Handler started");
15+
log("Handler started");
1516

1617
// Example of using runInChildContext with a child context
1718
const result = await context.runInChildContext(
1819
"parent_block",
1920
async (childContext: DurableContext) => {
20-
console.log("Inside parent block");
21+
log("Inside parent block");
2122

2223
// Use the child context for nested operations
2324
const nestedResult = await childContext.step(
2425
"nested_step",
2526
async () => {
26-
console.log("Inside nested step");
27+
log("Inside nested step");
2728
return "nested step result";
2829
},
2930
);
@@ -32,7 +33,7 @@ export const handler = withDurableExecution(
3233
const nestedBlockResult = await childContext.runInChildContext(
3334
"nested_block",
3435
async (grandchildContext: DurableContext) => {
35-
console.log("Inside nested block");
36+
log("Inside nested block");
3637

3738
// Use the grandchild context for further nested operations
3839
await grandchildContext.wait({ seconds: 1 });
@@ -48,7 +49,7 @@ export const handler = withDurableExecution(
4849
},
4950
);
5051

51-
console.log("Block completed with result:", result);
52+
log("Block completed with result:", result);
5253
return result;
5354
},
5455
);

packages/aws-durable-execution-sdk-js-examples/src/examples/comprehensive-operations/comprehensive-operations.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
withDurableExecution,
44
} from "@aws/durable-execution-sdk-js";
55
import { ExampleConfig } from "../../types";
6+
import { log } from "../../utils/logger";
67

78
export const config: ExampleConfig = {
89
name: "Comprehensive Operations",
@@ -23,11 +24,11 @@ interface ComprehensiveExampleInput {
2324
*/
2425
export const handler = withDurableExecution(
2526
async (event: ComprehensiveExampleInput, context: DurableContext) => {
26-
console.log("Starting comprehensive operations example with event:", event);
27+
log("Starting comprehensive operations example with event:", event);
2728

2829
// Step 1: ctx.step - Simple step that returns a result
2930
const step1Result = await context.step("step1", async () => {
30-
console.log("Executing step1");
31+
log("Executing step1");
3132
return "Step 1 completed successfully";
3233
});
3334

packages/aws-durable-execution-sdk-js-examples/src/examples/concurrent/wait/concurrent-wait.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
withDurableExecution,
44
} from "@aws/durable-execution-sdk-js";
55
import { ExampleConfig } from "../../../types";
6+
import { log } from "../../../utils/logger";
67

78
export const config: ExampleConfig = {
89
name: "Concurrent Wait",
@@ -11,13 +12,13 @@ export const config: ExampleConfig = {
1112

1213
export const handler = withDurableExecution(
1314
async (event: any, context: DurableContext) => {
14-
console.log("Before waits");
15+
log("Before waits");
1516
await Promise.all([
1617
context.wait("wait-1-second", { seconds: 1 }),
1718
context.wait("wait-5-seconds", { seconds: 5 }),
1819
context.wait("wait-10-seconds", { seconds: 10 }),
1920
]);
20-
console.log("After waits");
21+
log("After waits");
2122
return "Completed waits";
2223
},
2324
);

packages/aws-durable-execution-sdk-js-examples/src/examples/context-validation/parent-context-in-child/parent-context-in-child.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
withDurableExecution,
44
} from "@aws/durable-execution-sdk-js";
55
import { ExampleConfig } from "../../../types";
6+
import { log } from "../../../utils/logger";
67

78
export const config: ExampleConfig = {
89
name: "Parent Context in Child Error",
@@ -12,13 +13,13 @@ export const config: ExampleConfig = {
1213

1314
export const handler = withDurableExecution(
1415
async (event: any, context: DurableContext) => {
15-
console.log("Starting parent context operations");
16+
log("Starting parent context operations");
1617

1718
// This will fail because we're using the parent context inside the child
1819
const result = await context.runInChildContext(
1920
"child-context",
2021
async (childContext: DurableContext) => {
21-
console.log("Inside child context");
22+
log("Inside child context");
2223

2324
// ❌ WRONG: Using parent context instead of childContext
2425
// This should throw: "Context usage error in 'step': You are using a parent or sibling context..."

packages/aws-durable-execution-sdk-js-examples/src/examples/context-validation/parent-context-in-step/parent-context-in-step.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
withDurableExecution,
44
} from "@aws/durable-execution-sdk-js";
55
import { ExampleConfig } from "../../../types";
6+
import { log } from "../../../utils/logger";
67

78
export const config: ExampleConfig = {
89
name: "Parent Context in Step Error",
@@ -12,13 +13,13 @@ export const config: ExampleConfig = {
1213

1314
export const handler = withDurableExecution(
1415
async (event: any, context: DurableContext) => {
15-
console.log("Starting parent context operations");
16+
log("Starting parent context operations");
1617

1718
// This will fail because we're using the parent context inside a step within the child
1819
const result = await context.runInChildContext(
1920
"child-context",
2021
async (childContext: DurableContext) => {
21-
console.log("Inside child context");
22+
log("Inside child context");
2223

2324
// This step should work fine
2425
const step1 = await childContext.step("correct-step", async () => {

packages/aws-durable-execution-sdk-js-examples/src/examples/context-validation/parent-context-in-wait-condition/parent-context-in-wait-condition.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
withDurableExecution,
44
} from "@aws/durable-execution-sdk-js";
55
import { ExampleConfig } from "../../../types";
6+
import { log } from "../../../utils/logger";
67

78
export const config: ExampleConfig = {
89
name: "Parent Context in WaitForCondition Error",
@@ -12,12 +13,12 @@ export const config: ExampleConfig = {
1213

1314
export const handler = withDurableExecution(
1415
async (event: any, context: DurableContext) => {
15-
console.log("Starting parent context operations");
16+
log("Starting parent context operations");
1617

1718
const result = await context.runInChildContext(
1819
"child-context",
1920
async (childContext: DurableContext) => {
20-
console.log("Inside child context");
21+
log("Inside child context");
2122

2223
// ❌ WRONG: Using parent context inside waitForCondition check function
2324
const wrongWait = await childContext.waitForCondition(

packages/aws-durable-execution-sdk-js-examples/src/examples/create-callback/simple/create-callback.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
withDurableExecution,
44
} from "@aws/durable-execution-sdk-js";
55
import { ExampleConfig } from "../../../types";
6+
import { log } from "../../../utils/logger";
67

78
export const config: ExampleConfig = {
89
name: "Create Callback",
@@ -14,11 +15,9 @@ export const handler = withDurableExecution(
1415
const [callbackPromise, callbackId] =
1516
await context.createCallback<string>();
1617

17-
console.log(`Created callback with ID: ${callbackId}`);
18-
1918
// In a real scenario, you would send the callbackId to an external system
2019
// For this example, we'll just log it
21-
console.log("Send this callbackId to external system:", callbackId);
20+
log("Send this callbackId to external system:", callbackId);
2221

2322
// The promise would be resolved by calling SendDurableExecutionCallbackSuccess
2423
// with the callbackId from an external system

packages/aws-durable-execution-sdk-js-examples/src/examples/hello-world/hello-world.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
withDurableExecution,
44
} from "@aws/durable-execution-sdk-js";
55
import { ExampleConfig } from "../../types";
6+
import { log } from "../../utils/logger";
67

78
export const config: ExampleConfig = {
89
name: "Hello World",
@@ -11,7 +12,7 @@ export const config: ExampleConfig = {
1112

1213
export const handler = withDurableExecution(
1314
async (event: any, context: DurableContext) => {
14-
console.log("Hello world from a durable function!");
15+
log("Hello world from a durable function!");
1516
return "Hello World!";
1617
},
1718
);

packages/aws-durable-execution-sdk-js-examples/src/examples/map-completion-config-issue/map-completion-config-issue.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
withDurableExecution,
44
} from "@aws/durable-execution-sdk-js";
55
import { ExampleConfig } from "../../types";
6+
import { log } from "../../utils/logger";
67

78
export const config: ExampleConfig = {
89
name: "Map Completion Config Issue",
@@ -27,8 +28,8 @@ export const handler = withDurableExecution(
2728
toleratedFailurePercentage: 50,
2829
};
2930

30-
console.log("Starting map with config:", JSON.stringify(completionConfig));
31-
console.log(
31+
log("Starting map with config:", JSON.stringify(completionConfig));
32+
log(
3233
"Items pattern:",
3334
items.map((i) => (i.shouldFail ? "FAIL" : "SUCCESS")).join(", "),
3435
);
@@ -37,7 +38,7 @@ export const handler = withDurableExecution(
3738
"completion-config-items",
3839
items,
3940
async (ctx: DurableContext, item: (typeof items)[0], index: number) => {
40-
console.log(
41+
log(
4142
`Processing item ${item.id} (index ${index}), shouldFail: ${item.shouldFail}`,
4243
);
4344

@@ -74,13 +75,13 @@ export const handler = withDurableExecution(
7475
},
7576
);
7677

77-
console.log("Map completed with results:");
78-
console.log(`Total items processed: ${results.totalCount}`);
79-
console.log(`Successful items: ${results.successCount}`);
80-
console.log(`Failed items: ${results.failureCount}`);
81-
console.log(`Has failures: ${results.hasFailure}`);
82-
console.log(`Batch status: ${results.status}`);
83-
console.log(`Completion reason: ${results.completionReason}`);
78+
log("Map completed with results:");
79+
log(`Total items processed: ${results.totalCount}`);
80+
log(`Successful items: ${results.successCount}`);
81+
log(`Failed items: ${results.failureCount}`);
82+
log(`Has failures: ${results.hasFailure}`);
83+
log(`Batch status: ${results.status}`);
84+
log(`Completion reason: ${results.completionReason}`);
8485

8586
return {
8687
totalItems: results.totalCount,

packages/aws-durable-execution-sdk-js-examples/src/examples/parallel/min-successful-with-callback/parallel-min-successful-callback.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
withDurableExecution,
44
} from "@aws/durable-execution-sdk-js";
55
import { ExampleConfig } from "../../../types";
6+
import { log } from "../../../utils/logger";
67

78
export const config: ExampleConfig = {
89
name: "Parallel minSuccessful with Callbacks",
@@ -18,19 +19,21 @@ export const handler = withDurableExecution(
1819
async (childContext) => {
1920
const [callbackPromise, callbackId] =
2021
await childContext.createCallback<string>("branch-1-callback");
21-
console.log(`Branch 1 callback ID: ${callbackId}`);
22+
log(`Branch 1 callback ID: ${callbackId}`);
2223
return await callbackPromise;
2324
},
2425
async (childContext) => {
2526
const [callbackPromise, callbackId] =
2627
await childContext.createCallback<string>("branch-2-callback");
27-
console.log(`Branch 2 callback ID: ${callbackId}`);
28+
log(`Branch 2 callback ID: ${callbackId}`);
29+
2830
return await callbackPromise;
2931
},
3032
async (childContext) => {
3133
const [callbackPromise, callbackId] =
3234
await childContext.createCallback<string>("branch-3-callback");
33-
console.log(`Branch 3 callback ID: ${callbackId}`);
35+
log(`Branch 3 callback ID: ${callbackId}`);
36+
3437
return await callbackPromise;
3538
},
3639
],

0 commit comments

Comments
 (0)