-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Respect DAPR_GRPC_PORT environment variable if set externally, allow …
…app-port to be suppressed (#68) * #66 : Workflow sample, respect external Environment Variables on startup, allow AppPort to be ignored * #66 : Always initialize DaprOptions to ensure valid fluent builder initalization * #66 : Unit test for HasAppPort, other cleanup * #66 : Additional unit tests
- Loading branch information
Showing
32 changed files
with
804 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
samples/Workflow/ConsoleSample/WorkflowConsoleApp/Activities/NotifyActivity.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using Dapr.Workflow; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace WorkflowConsoleApp.Activities | ||
{ | ||
public class NotifyActivity(ILoggerFactory loggerFactory) : WorkflowActivity<Notification, object> | ||
{ | ||
private readonly ILogger _logger = loggerFactory.CreateLogger<NotifyActivity>(); | ||
|
||
public override Task<object> RunAsync(WorkflowActivityContext context, Notification notification) | ||
{ | ||
_logger.LogInformation(notification.Message); | ||
|
||
return Task.FromResult<object>(null); | ||
} | ||
} | ||
|
||
public record Notification(string Message); | ||
} |
29 changes: 29 additions & 0 deletions
29
samples/Workflow/ConsoleSample/WorkflowConsoleApp/Activities/ProcessPaymentActivity.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using Dapr.Workflow; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace WorkflowConsoleApp.Activities | ||
{ | ||
public class ProcessPaymentActivity(ILoggerFactory loggerFactory) : WorkflowActivity<PaymentRequest, object> | ||
{ | ||
private readonly ILogger _logger = loggerFactory.CreateLogger<ProcessPaymentActivity>(); | ||
|
||
public override async Task<object> RunAsync(WorkflowActivityContext context, PaymentRequest req) | ||
{ | ||
_logger.LogInformation( | ||
"Processing payment: {requestId} for {amount} {item} at ${currency}", | ||
req.RequestId, | ||
req.Amount, | ||
req.ItemName, | ||
req.Currency); | ||
|
||
// Simulate slow processing | ||
await Task.Delay(TimeSpan.FromSeconds(7)); | ||
|
||
_logger.LogInformation( | ||
"Payment for request ID '{requestId}' processed successfully", | ||
req.RequestId); | ||
|
||
return null; | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
samples/Workflow/ConsoleSample/WorkflowConsoleApp/Activities/RequestApprovalActivity.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using Dapr.Workflow; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace WorkflowConsoleApp.Activities | ||
{ | ||
public class RequestApprovalActivity(ILoggerFactory loggerFactory) : WorkflowActivity<OrderPayload, object> | ||
{ | ||
private readonly ILogger _logger = loggerFactory.CreateLogger<RequestApprovalActivity>(); | ||
|
||
public override Task<object> RunAsync(WorkflowActivityContext context, OrderPayload input) | ||
{ | ||
var orderId = context.InstanceId; | ||
_logger.LogInformation("Requesting approval for order {orderId}", orderId); | ||
|
||
return Task.FromResult<object>(null); | ||
} | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
samples/Workflow/ConsoleSample/WorkflowConsoleApp/Activities/ReserveInventoryActivity.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using Dapr.Client; | ||
using Dapr.Workflow; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace WorkflowConsoleApp.Activities | ||
{ | ||
public class ReserveInventoryActivity(ILoggerFactory loggerFactory, DaprClient client) : WorkflowActivity<InventoryRequest, InventoryResult> | ||
{ | ||
private const string StoreName = "statestore"; | ||
|
||
private readonly ILogger _logger = loggerFactory.CreateLogger<ReserveInventoryActivity>(); | ||
|
||
public override async Task<InventoryResult> RunAsync(WorkflowActivityContext context, InventoryRequest req) | ||
{ | ||
_logger.LogInformation( | ||
"Reserving inventory for order '{requestId}' of {quantity} {name}", | ||
req.RequestId, | ||
req.Quantity, | ||
req.ItemName); | ||
|
||
// Ensure that the store has items | ||
var item = await client.GetStateAsync<InventoryItem>( | ||
StoreName, | ||
req.ItemName.ToLowerInvariant()); | ||
|
||
// Catch for the case where the statestore isn't setup | ||
if (item == null) | ||
{ | ||
// Not enough items. | ||
return new InventoryResult(false, item); | ||
} | ||
|
||
_logger.LogInformation( | ||
"There are {quantity} {name} available for purchase", | ||
item.Quantity, | ||
item.Name); | ||
|
||
// See if there're enough items to purchase | ||
if (item.Quantity >= req.Quantity) | ||
{ | ||
// Simulate slow processing | ||
await Task.Delay(TimeSpan.FromSeconds(2)); | ||
|
||
return new InventoryResult(true, item); | ||
} | ||
|
||
// Not enough items. | ||
return new InventoryResult(false, item); | ||
} | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
samples/Workflow/ConsoleSample/WorkflowConsoleApp/Activities/UpdateInventoryActivity.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using Dapr.Client; | ||
using Dapr.Workflow; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace WorkflowConsoleApp.Activities | ||
{ | ||
public class UpdateInventoryActivity(ILoggerFactory loggerFactory, DaprClient client) : WorkflowActivity<PaymentRequest, object> | ||
{ | ||
private const string StoreName = "statestore"; | ||
private readonly ILogger _logger = loggerFactory.CreateLogger<UpdateInventoryActivity>(); | ||
|
||
public override async Task<object> RunAsync(WorkflowActivityContext context, PaymentRequest req) | ||
{ | ||
_logger.LogInformation( | ||
"Checking inventory for order '{requestId}' for {amount} {name}", | ||
req.RequestId, | ||
req.Amount, | ||
req.ItemName); | ||
|
||
// Simulate slow processing | ||
await Task.Delay(TimeSpan.FromSeconds(5)); | ||
|
||
// Determine if there are enough Items for purchase | ||
var item = await client.GetStateAsync<InventoryItem>( | ||
StoreName, | ||
req.ItemName.ToLowerInvariant()); | ||
var newQuantity = item.Quantity - req.Amount; | ||
if (newQuantity < 0) | ||
{ | ||
_logger.LogInformation( | ||
"Payment for request ID '{requestId}' could not be processed. Insufficient inventory.", | ||
req.RequestId); | ||
throw new InvalidOperationException($"Not enough '{req.ItemName}' inventory! Requested {req.Amount} but only {item.Quantity} available."); | ||
} | ||
|
||
// Update the statestore with the new amount of the item | ||
await client.SaveStateAsync( | ||
StoreName, | ||
req.ItemName.ToLowerInvariant(), | ||
new InventoryItem(Name: req.ItemName, PerItemCost: item.PerItemCost, Quantity: newQuantity)); | ||
|
||
_logger.LogInformation( | ||
"There are now {quantity} {name} left in stock", | ||
newQuantity, | ||
item.Name); | ||
|
||
return null; | ||
} | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
samples/Workflow/ConsoleSample/WorkflowConsoleApp/Models.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#pragma warning disable SA1649 // File name should match first type name | ||
namespace WorkflowConsoleApp | ||
{ | ||
public record OrderPayload(string Name, double TotalCost, int Quantity = 1); | ||
|
||
public record InventoryRequest(string RequestId, string ItemName, int Quantity); | ||
|
||
public record InventoryResult(bool Success, InventoryItem OrderPayload); | ||
|
||
public record PaymentRequest(string RequestId, string ItemName, int Amount, double Currency); | ||
|
||
public record OrderResult(bool Processed); | ||
|
||
public record InventoryItem(string Name, double PerItemCost, int Quantity); | ||
|
||
public enum ApprovalResult | ||
{ | ||
Unspecified = 0, | ||
Approved = 1, | ||
Rejected = 2, | ||
} | ||
} | ||
#pragma warning restore SA1649 // File name should match first type name |
Oops, something went wrong.