|
47 | 47 | using var host = builder.Build();
|
48 | 48 | host.Start();
|
49 | 49 |
|
50 |
| -using var daprClient = new DaprClientBuilder().Build(); |
| 50 | +DaprClient daprClient; |
| 51 | +string apiToken = Environment.GetEnvironmentVariable("DAPR_API_TOKEN"); |
| 52 | +if (!string.IsNullOrEmpty(apiToken)) |
| 53 | +{ |
| 54 | + daprClient = new DaprClientBuilder().UseDaprApiToken(apiToken).Build(); |
| 55 | +} |
| 56 | +else |
| 57 | +{ |
| 58 | + daprClient = new DaprClientBuilder().Build(); |
| 59 | +} |
51 | 60 |
|
52 | 61 | // Wait for the sidecar to become available
|
53 | 62 | while (!await daprClient.CheckHealthAsync())
|
|
70 | 79 | await RestockInventory(daprClient, baseInventory);
|
71 | 80 |
|
72 | 81 | // Start the input loop
|
73 |
| -while (true) |
| 82 | +using (daprClient) |
74 | 83 | {
|
75 |
| - // Get the name of the item to order and make sure we have inventory |
76 |
| - string items = string.Join(", ", baseInventory.Select(i => i.Name)); |
77 |
| - Console.WriteLine($"Enter the name of one of the following items to order [{items}]."); |
78 |
| - Console.WriteLine("To restock items, type 'restock'."); |
79 |
| - string itemName = Console.ReadLine()?.Trim(); |
80 |
| - if (string.IsNullOrEmpty(itemName)) |
81 |
| - { |
82 |
| - continue; |
83 |
| - } |
84 |
| - else if (string.Equals("restock", itemName, StringComparison.OrdinalIgnoreCase)) |
| 84 | + while (true) |
85 | 85 | {
|
86 |
| - await RestockInventory(daprClient, baseInventory); |
87 |
| - continue; |
88 |
| - } |
| 86 | + // Get the name of the item to order and make sure we have inventory |
| 87 | + string items = string.Join(", ", baseInventory.Select(i => i.Name)); |
| 88 | + Console.WriteLine($"Enter the name of one of the following items to order [{items}]."); |
| 89 | + Console.WriteLine("To restock items, type 'restock'."); |
| 90 | + string itemName = Console.ReadLine()?.Trim(); |
| 91 | + if (string.IsNullOrEmpty(itemName)) |
| 92 | + { |
| 93 | + continue; |
| 94 | + } |
| 95 | + else if (string.Equals("restock", itemName, StringComparison.OrdinalIgnoreCase)) |
| 96 | + { |
| 97 | + await RestockInventory(daprClient, baseInventory); |
| 98 | + continue; |
| 99 | + } |
89 | 100 |
|
90 |
| - InventoryItem item = baseInventory.FirstOrDefault(item => string.Equals(item.Name, itemName, StringComparison.OrdinalIgnoreCase)); |
91 |
| - if (item == null) |
92 |
| - { |
93 |
| - Console.ForegroundColor = ConsoleColor.Yellow; |
94 |
| - Console.WriteLine($"We don't have {itemName}!"); |
95 |
| - Console.ResetColor(); |
96 |
| - continue; |
97 |
| - } |
| 101 | + InventoryItem item = baseInventory.FirstOrDefault(item => string.Equals(item.Name, itemName, StringComparison.OrdinalIgnoreCase)); |
| 102 | + if (item == null) |
| 103 | + { |
| 104 | + Console.ForegroundColor = ConsoleColor.Yellow; |
| 105 | + Console.WriteLine($"We don't have {itemName}!"); |
| 106 | + Console.ResetColor(); |
| 107 | + continue; |
| 108 | + } |
98 | 109 |
|
99 |
| - Console.WriteLine($"How many {itemName} would you like to purchase?"); |
100 |
| - string amountStr = Console.ReadLine().Trim(); |
101 |
| - if (!int.TryParse(amountStr, out int amount) || amount <= 0) |
102 |
| - { |
103 |
| - Console.ForegroundColor = ConsoleColor.Yellow; |
104 |
| - Console.WriteLine($"Invalid input. Assuming you meant to type '1'."); |
105 |
| - Console.ResetColor(); |
106 |
| - amount = 1; |
107 |
| - } |
| 110 | + Console.WriteLine($"How many {itemName} would you like to purchase?"); |
| 111 | + string amountStr = Console.ReadLine().Trim(); |
| 112 | + if (!int.TryParse(amountStr, out int amount) || amount <= 0) |
| 113 | + { |
| 114 | + Console.ForegroundColor = ConsoleColor.Yellow; |
| 115 | + Console.WriteLine($"Invalid input. Assuming you meant to type '1'."); |
| 116 | + Console.ResetColor(); |
| 117 | + amount = 1; |
| 118 | + } |
108 | 119 |
|
109 |
| - // Construct the order with a unique order ID |
110 |
| - string orderId = $"{itemName.ToLowerInvariant()}-{Guid.NewGuid().ToString()[..8]}"; |
111 |
| - double totalCost = amount * item.PerItemCost; |
112 |
| - var orderInfo = new OrderPayload(itemName.ToLowerInvariant(), totalCost, amount); |
| 120 | + // Construct the order with a unique order ID |
| 121 | + string orderId = $"{itemName.ToLowerInvariant()}-{Guid.NewGuid().ToString()[..8]}"; |
| 122 | + double totalCost = amount * item.PerItemCost; |
| 123 | + var orderInfo = new OrderPayload(itemName.ToLowerInvariant(), totalCost, amount); |
113 | 124 |
|
114 |
| - // Start the workflow using the order ID as the workflow ID |
115 |
| - Console.WriteLine($"Starting order workflow '{orderId}' purchasing {amount} {itemName}"); |
116 |
| - await daprClient.StartWorkflowAsync( |
117 |
| - workflowComponent: DaprWorkflowComponent, |
118 |
| - workflowName: nameof(OrderProcessingWorkflow), |
119 |
| - input: orderInfo, |
120 |
| - instanceId: orderId); |
| 125 | + // Start the workflow using the order ID as the workflow ID |
| 126 | + Console.WriteLine($"Starting order workflow '{orderId}' purchasing {amount} {itemName}"); |
| 127 | + await daprClient.StartWorkflowAsync( |
| 128 | + workflowComponent: DaprWorkflowComponent, |
| 129 | + workflowName: nameof(OrderProcessingWorkflow), |
| 130 | + input: orderInfo, |
| 131 | + instanceId: orderId); |
121 | 132 |
|
122 |
| - // Wait for the workflow to start and confirm the input |
123 |
| - GetWorkflowResponse state = await daprClient.WaitForWorkflowStartAsync( |
124 |
| - instanceId: orderId, |
125 |
| - workflowComponent: DaprWorkflowComponent); |
| 133 | + // Wait for the workflow to start and confirm the input |
| 134 | + GetWorkflowResponse state = await daprClient.WaitForWorkflowStartAsync( |
| 135 | + instanceId: orderId, |
| 136 | + workflowComponent: DaprWorkflowComponent); |
126 | 137 |
|
127 |
| - Console.WriteLine($"{state.WorkflowName} (ID = {orderId}) started successfully with {state.ReadInputAs<OrderPayload>()}"); |
| 138 | + Console.WriteLine($"{state.WorkflowName} (ID = {orderId}) started successfully with {state.ReadInputAs<OrderPayload>()}"); |
128 | 139 |
|
129 |
| - // Wait for the workflow to complete |
130 |
| - while (true) |
131 |
| - { |
132 |
| - using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5)); |
133 |
| - try |
| 140 | + // Wait for the workflow to complete |
| 141 | + while (true) |
134 | 142 | {
|
135 |
| - state = await daprClient.WaitForWorkflowCompletionAsync( |
136 |
| - instanceId: orderId, |
137 |
| - workflowComponent: DaprWorkflowComponent, |
138 |
| - cancellationToken: cts.Token); |
139 |
| - break; |
140 |
| - } |
141 |
| - catch (OperationCanceledException) |
142 |
| - { |
143 |
| - // Check to see if the workflow is blocked waiting for an approval |
144 |
| - state = await daprClient.GetWorkflowAsync( |
145 |
| - instanceId: orderId, |
146 |
| - workflowComponent: DaprWorkflowComponent); |
147 |
| - if (state.Properties.TryGetValue("dapr.workflow.custom_status", out string customStatus) && |
148 |
| - customStatus.Contains("Waiting for approval")) |
| 143 | + using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5)); |
| 144 | + try |
149 | 145 | {
|
150 |
| - Console.WriteLine($"{state.WorkflowName} (ID = {orderId}) requires approval. Approve? [Y/N]"); |
151 |
| - string approval = Console.ReadLine(); |
152 |
| - ApprovalResult approvalResult = ApprovalResult.Unspecified; |
153 |
| - if (string.Equals(approval, "Y", StringComparison.OrdinalIgnoreCase)) |
154 |
| - { |
155 |
| - Console.WriteLine("Approving order..."); |
156 |
| - approvalResult = ApprovalResult.Approved; |
157 |
| - } |
158 |
| - else if (string.Equals(approval, "N", StringComparison.OrdinalIgnoreCase)) |
159 |
| - { |
160 |
| - Console.WriteLine("Rejecting order..."); |
161 |
| - approvalResult = ApprovalResult.Rejected; |
162 |
| - } |
163 |
| - |
164 |
| - if (approvalResult != ApprovalResult.Unspecified) |
| 146 | + state = await daprClient.WaitForWorkflowCompletionAsync( |
| 147 | + instanceId: orderId, |
| 148 | + workflowComponent: DaprWorkflowComponent, |
| 149 | + cancellationToken: cts.Token); |
| 150 | + break; |
| 151 | + } |
| 152 | + catch (OperationCanceledException) |
| 153 | + { |
| 154 | + // Check to see if the workflow is blocked waiting for an approval |
| 155 | + state = await daprClient.GetWorkflowAsync( |
| 156 | + instanceId: orderId, |
| 157 | + workflowComponent: DaprWorkflowComponent); |
| 158 | + if (state.Properties.TryGetValue("dapr.workflow.custom_status", out string customStatus) && |
| 159 | + customStatus.Contains("Waiting for approval")) |
165 | 160 | {
|
166 |
| - // Raise the workflow event to the workflow |
167 |
| - await daprClient.RaiseWorkflowEventAsync( |
168 |
| - instanceId: orderId, |
169 |
| - workflowComponent: DaprWorkflowComponent, |
170 |
| - eventName: "ManagerApproval", |
171 |
| - eventData: approvalResult); |
| 161 | + Console.WriteLine($"{state.WorkflowName} (ID = {orderId}) requires approval. Approve? [Y/N]"); |
| 162 | + string approval = Console.ReadLine(); |
| 163 | + ApprovalResult approvalResult = ApprovalResult.Unspecified; |
| 164 | + if (string.Equals(approval, "Y", StringComparison.OrdinalIgnoreCase)) |
| 165 | + { |
| 166 | + Console.WriteLine("Approving order..."); |
| 167 | + approvalResult = ApprovalResult.Approved; |
| 168 | + } |
| 169 | + else if (string.Equals(approval, "N", StringComparison.OrdinalIgnoreCase)) |
| 170 | + { |
| 171 | + Console.WriteLine("Rejecting order..."); |
| 172 | + approvalResult = ApprovalResult.Rejected; |
| 173 | + } |
| 174 | + |
| 175 | + if (approvalResult != ApprovalResult.Unspecified) |
| 176 | + { |
| 177 | + // Raise the workflow event to the workflow |
| 178 | + await daprClient.RaiseWorkflowEventAsync( |
| 179 | + instanceId: orderId, |
| 180 | + workflowComponent: DaprWorkflowComponent, |
| 181 | + eventName: "ManagerApproval", |
| 182 | + eventData: approvalResult); |
| 183 | + } |
| 184 | + |
| 185 | + // otherwise, keep waiting |
172 | 186 | }
|
173 |
| - |
174 |
| - // otherwise, keep waiting |
175 | 187 | }
|
176 | 188 | }
|
177 |
| - } |
178 | 189 |
|
179 |
| - if (state.RuntimeStatus == WorkflowRuntimeStatus.Completed) |
180 |
| - { |
181 |
| - OrderResult result = state.ReadOutputAs<OrderResult>(); |
182 |
| - if (result.Processed) |
| 190 | + if (state.RuntimeStatus == WorkflowRuntimeStatus.Completed) |
183 | 191 | {
|
184 |
| - Console.ForegroundColor = ConsoleColor.Green; |
185 |
| - Console.WriteLine($"Order workflow is {state.RuntimeStatus} and the order was processed successfully ({result})."); |
186 |
| - Console.ResetColor(); |
| 192 | + OrderResult result = state.ReadOutputAs<OrderResult>(); |
| 193 | + if (result.Processed) |
| 194 | + { |
| 195 | + Console.ForegroundColor = ConsoleColor.Green; |
| 196 | + Console.WriteLine($"Order workflow is {state.RuntimeStatus} and the order was processed successfully ({result})."); |
| 197 | + Console.ResetColor(); |
| 198 | + } |
| 199 | + else |
| 200 | + { |
| 201 | + Console.WriteLine($"Order workflow is {state.RuntimeStatus} but the order was not processed."); |
| 202 | + } |
187 | 203 | }
|
188 |
| - else |
| 204 | + else if (state.RuntimeStatus == WorkflowRuntimeStatus.Failed) |
189 | 205 | {
|
190 |
| - Console.WriteLine($"Order workflow is {state.RuntimeStatus} but the order was not processed."); |
| 206 | + Console.ForegroundColor = ConsoleColor.Red; |
| 207 | + Console.WriteLine($"The workflow failed - {state.FailureDetails}"); |
| 208 | + Console.ResetColor(); |
191 | 209 | }
|
192 |
| - } |
193 |
| - else if (state.RuntimeStatus == WorkflowRuntimeStatus.Failed) |
194 |
| - { |
195 |
| - Console.ForegroundColor = ConsoleColor.Red; |
196 |
| - Console.WriteLine($"The workflow failed - {state.FailureDetails}"); |
197 |
| - Console.ResetColor(); |
198 |
| - } |
199 | 210 |
|
200 |
| - Console.WriteLine(); |
| 211 | + Console.WriteLine(); |
| 212 | + } |
201 | 213 | }
|
202 |
| - |
203 | 214 | static async Task RestockInventory(DaprClient daprClient, List<InventoryItem> inventory)
|
204 | 215 | {
|
205 | 216 | Console.WriteLine("*** Restocking inventory...");
|
|
0 commit comments