-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathactivity-sequence.ts
More file actions
89 lines (75 loc) · 2.73 KB
/
activity-sequence.ts
File metadata and controls
89 lines (75 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import {
TaskHubGrpcClient,
TaskHubGrpcWorker,
ActivityContext,
OrchestrationContext,
TOrchestrator,
// Logger types for custom logging
// ConsoleLogger (default) - logs to console
// NoOpLogger - silent mode, useful for testing
// You can also implement your own Logger interface
ConsoleLogger,
} from "@microsoft/durabletask-js";
// Wrap the entire code in an immediately-invoked async function
(async () => {
// Update the gRPC client and worker to use a local address and port
const grpcServerAddress = "localhost:4001";
// Optional: Create a custom logger (defaults to ConsoleLogger if not provided)
// You can implement your own Logger interface to integrate with Winston, Pino, etc.
const logger = new ConsoleLogger();
// Pass the logger as the 6th parameter (after metadataGenerator)
// Parameters: hostAddress, options, useTLS, credentials, metadataGenerator, logger
const taskHubClient: TaskHubGrpcClient = new TaskHubGrpcClient(
grpcServerAddress,
undefined,
undefined,
undefined,
undefined,
logger,
);
const taskHubWorker: TaskHubGrpcWorker = new TaskHubGrpcWorker(
grpcServerAddress,
undefined,
undefined,
undefined,
undefined,
logger,
);
const hello = async (_: ActivityContext, name: string) => {
return `Hello ${name}!`;
};
const sequence: TOrchestrator = async function* (ctx: OrchestrationContext): any {
const cities: string[] = [];
const result1 = yield ctx.callActivity(hello, "Tokyo");
cities.push(result1);
const result2 = yield ctx.callActivity(hello, "Seattle"); // Correct the spelling of "Seattle"
cities.push(result2);
const result3 = yield ctx.callActivity(hello, "London");
cities.push(result3);
return cities;
};
taskHubWorker.addOrchestrator(sequence);
taskHubWorker.addActivity(hello);
// Wrap the worker startup in a try-catch block to handle any errors during startup
try {
await taskHubWorker.start();
logger.info("Worker started successfully");
} catch (error) {
logger.error("Error starting worker:", error);
}
// Schedule a new orchestration
try {
const id = await taskHubClient.scheduleNewOrchestration(sequence);
logger.info(`Orchestration scheduled with ID: ${id}`);
// Wait for orchestration completion
const state = await taskHubClient.waitForOrchestrationCompletion(id, undefined, 30);
logger.info(`Orchestration completed! Result: ${state?.serializedOutput}`);
} catch (error) {
logger.error("Error scheduling or waiting for orchestration:", error);
}
// stop worker and client
await taskHubWorker.stop();
await taskHubClient.stop();
})();