Skip to content

Commit

Permalink
Respect DAPR_GRPC_PORT environment variable if set externally, allow …
Browse files Browse the repository at this point in the history
…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
badgeratu authored Nov 3, 2024
1 parent f739fd5 commit 7a47300
Show file tree
Hide file tree
Showing 32 changed files with 804 additions and 30 deletions.
24 changes: 24 additions & 0 deletions all.sln
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Man.Dapr.Sidekick.AspNetCore.Tests", "tests\Man.Dapr.Sidekick.AspNetCore.Tests\Man.Dapr.Sidekick.AspNetCore.Tests.csproj", "{F513F641-20E2-4C47-8F57-95664383004E}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{6481CB38-F5E9-4CB4-8E5D-AB3114186E7D}"
ProjectSection(SolutionItems) = preProject
samples\Directory.Build.props = samples\Directory.Build.props
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "AspNetCore", "AspNetCore", "{DA3D8137-F2DD-465D-81AA-3CA5C75087D2}"
EndProject
Expand Down Expand Up @@ -108,6 +111,12 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "SchedulerSample", "Schedule
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SchedulerSample", "samples\AspNetCore\SchedulerSample\SchedulerSample\SchedulerSample.csproj", "{C0B2943F-3EA8-43A9-8714-7D6B84AD788E}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Workflow", "Workflow", "{2D362BDC-794A-4B1B-B20A-3E4A7214CADC}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ConsoleSample", "ConsoleSample", "{E20CFEC4-3F3F-4BA6-B796-363B76FA5B53}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WorkflowConsoleApp", "samples\Workflow\ConsoleSample\WorkflowConsoleApp\WorkflowConsoleApp.csproj", "{56EFBE8B-485A-48A7-8239-24709774E4FE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -334,6 +343,18 @@ Global
{C0B2943F-3EA8-43A9-8714-7D6B84AD788E}.Release|x64.Build.0 = Release|Any CPU
{C0B2943F-3EA8-43A9-8714-7D6B84AD788E}.Release|x86.ActiveCfg = Release|Any CPU
{C0B2943F-3EA8-43A9-8714-7D6B84AD788E}.Release|x86.Build.0 = Release|Any CPU
{56EFBE8B-485A-48A7-8239-24709774E4FE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{56EFBE8B-485A-48A7-8239-24709774E4FE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{56EFBE8B-485A-48A7-8239-24709774E4FE}.Debug|x64.ActiveCfg = Debug|Any CPU
{56EFBE8B-485A-48A7-8239-24709774E4FE}.Debug|x64.Build.0 = Debug|Any CPU
{56EFBE8B-485A-48A7-8239-24709774E4FE}.Debug|x86.ActiveCfg = Debug|Any CPU
{56EFBE8B-485A-48A7-8239-24709774E4FE}.Debug|x86.Build.0 = Debug|Any CPU
{56EFBE8B-485A-48A7-8239-24709774E4FE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{56EFBE8B-485A-48A7-8239-24709774E4FE}.Release|Any CPU.Build.0 = Release|Any CPU
{56EFBE8B-485A-48A7-8239-24709774E4FE}.Release|x64.ActiveCfg = Release|Any CPU
{56EFBE8B-485A-48A7-8239-24709774E4FE}.Release|x64.Build.0 = Release|Any CPU
{56EFBE8B-485A-48A7-8239-24709774E4FE}.Release|x86.ActiveCfg = Release|Any CPU
{56EFBE8B-485A-48A7-8239-24709774E4FE}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -369,6 +390,9 @@ Global
{2FC86574-6A81-4E2B-A0D4-78D46528A917} = {AE430C04-78BD-4CAE-86D7-EBC599774D9C}
{8FD8AFF0-A56A-4BDC-B40E-F498AA147790} = {DA3D8137-F2DD-465D-81AA-3CA5C75087D2}
{C0B2943F-3EA8-43A9-8714-7D6B84AD788E} = {8FD8AFF0-A56A-4BDC-B40E-F498AA147790}
{2D362BDC-794A-4B1B-B20A-3E4A7214CADC} = {6481CB38-F5E9-4CB4-8E5D-AB3114186E7D}
{E20CFEC4-3F3F-4BA6-B796-363B76FA5B53} = {2D362BDC-794A-4B1B-B20A-3E4A7214CADC}
{56EFBE8B-485A-48A7-8239-24709774E4FE} = {E20CFEC4-3F3F-4BA6-B796-363B76FA5B53}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {E906E97D-7D56-4E02-A13F-1C48AEB47A88}
Expand Down
6 changes: 6 additions & 0 deletions properties/dapr_sidekick_csharp.props
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@
<Authors>Man Group</Authors>
</PropertyGroup>

<PropertyGroup>
<!-- Disable package vulnerability warnings - not much choice as many packages required for legacy builds are old -->
<NuGetAudit>false</NuGetAudit>
<WarningsNotAsErrors>NU1901;NU1902;NU1903;NU1904</WarningsNotAsErrors>
</PropertyGroup>

<!-- Asset Packages -->
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="8.0.0">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Dapr.Actors" Version="1.13.0" />
<PackageReference Include="Dapr.Actors" Version="1.14.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Dapr.Actors" Version="1.13.0" />
<PackageReference Include="Dapr.Actors.AspNetCore" Version="1.13.0" />
<PackageReference Include="Dapr.Actors" Version="1.14.0" />
<PackageReference Include="Dapr.Actors.AspNetCore" Version="1.14.0" />
<PackageReference Include="Serilog.AspNetCore" Version="8.0.1" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Dapr.Actors" Version="1.13.0" />
<PackageReference Include="Dapr.Actors" Version="1.14.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Dapr.Extensions.Configuration" Version="1.13.0" />
<PackageReference Include="Dapr.Extensions.Configuration" Version="1.14.0" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.11" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
<PackageReference Include="Dapr.AspNetCore" Version="1.13.0" />
<PackageReference Include="Dapr.AspNetCore" Version="1.14.0" />
<PackageReference Include="Serilog.AspNetCore" Version="8.0.1" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Dapr.AspNetCore" Version="1.13.0" />
<PackageReference Include="Dapr.AspNetCore" Version="1.14.0" />
<PackageReference Include="Serilog.AspNetCore" Version="8.0.1" />
</ItemGroup>

Expand Down
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);
}
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;
}
}
}
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);
}
}
}
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);
}
}
}
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 samples/Workflow/ConsoleSample/WorkflowConsoleApp/Models.cs
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
Loading

0 comments on commit 7a47300

Please sign in to comment.