Skip to content

Commit d2d54e3

Browse files
Refactor and fixes email activity
1 parent d8c7b82 commit d2d54e3

File tree

4 files changed

+31
-29
lines changed

4 files changed

+31
-29
lines changed

MessageSenderService/Activities/SendEmailActivity.cs

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,16 @@ namespace MessageSenderService.DurableFunctions.Activities
1414
public static class SendEmailActivity
1515
{
1616
[FunctionName("SendEmail")]
17-
public static async Task<string> SendEmailAsync([ActivityTrigger] string serviceBusQueueMessage, ILogger log)
17+
public static async Task<bool> SendEmail([ActivityTrigger] string serviceBusQueueMessage, ILogger log)
1818
{
1919

2020
try
2121
{
2222
log.LogInformation($"SendEmailActivity trigged from the durable orchestratoion");
2323

24-
if (serviceBusQueueMessage != null)
25-
{
26-
log.LogInformation($"Sending email to recipient with queue message: {serviceBusQueueMessage}.");
27-
bool result = await SendEmailAsync(serviceBusQueueMessage);
28-
if (result == false)
29-
{
30-
31-
return $"Email not sent!";
32-
}
33-
else
34-
{
35-
log.LogInformation($"Sending email with message: {serviceBusQueueMessage}");
36-
}
37-
}
38-
return $"Email was sent successfully!";
24+
log.LogInformation($"Sending email to recipient with queue message: {serviceBusQueueMessage}.");
25+
bool result = await SendEmailAsync(serviceBusQueueMessage);
26+
return result;
3927
}
4028
catch (Exception)
4129
{

MessageSenderService/ClientFunctions/ServiceBusQueueTrigger.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
using Microsoft.Extensions.Logging;
99
using System.Collections.Generic;
1010
using System.IO;
11-
using Microsoft.Azure.ServiceBus;
11+
using Microsoft.Azure;
1212

1313
namespace MessageSenderService.DurableFunctions.ClientFunctions
1414
{
@@ -25,7 +25,7 @@ public static async Task ServiceBusTriggerFunction(
2525
log.LogInformation($"C# ServiceBus queue trigger function processed message: {myServiceBusQueueItem}");
2626

2727
// When the service bus is triggered by a new queue message it initiates the orchestrator
28-
string orchestrationInstanceId = await starter.StartNewAsync("ServiceBusQueueOrchestator", null);
28+
string orchestrationInstanceId = await starter.StartNewAsync("ServiceBusQueueOrchestrator", myServiceBusQueueItem);
2929

3030
log.LogInformation($"Started orchestration with ID = '{orchestrationInstanceId}'.");
3131
}

MessageSenderService/MessageSenderService.DurableFunctions.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
<PropertyGroup>
33
<TargetFramework>net6.0</TargetFramework>
44
<AzureFunctionsVersion>v4</AzureFunctionsVersion>
5+
<UserSecretsId>42b7f619-019d-4b7f-bdb7-ddaf57a5d22f</UserSecretsId>
56
</PropertyGroup>
67
<ItemGroup>
78
<PackageReference Include="Azure.Messaging.ServiceBus" Version="7.10.0" />
89
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.DurableTask" Version="2.8.0" />
910
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.ServiceBus" Version="5.7.0" />
11+
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="5.0.0" />
1012
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.1.3" />
1113
<PackageReference Include="SendGrid" Version="9.28.1" />
1214
</ItemGroup>

MessageSenderService/Orchestrators/ServiceBusOrchestratorFunction.cs

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,38 @@
22
using System.Collections.Generic;
33
using System.Threading.Tasks;
44
using Microsoft.Azure.WebJobs.Extensions.DurableTask;
5-
5+
using Microsoft.Extensions.Logging;
6+
using System;
67

78
namespace MessageSenderService.DurableFunctions.Orchestrators
89
{
910

1011
//TODO - Use fan out fan in pattern
1112
public static class ServiceBusOrchestratorFunction
1213
{
13-
[FunctionName("ServiceBusQueueOrchestator")]
14-
public static async Task<List<string>> RunOrchestrator(
15-
[OrchestrationTrigger] IDurableOrchestrationContext context)
14+
[FunctionName("ServiceBusQueueOrchestrator")]
15+
public static async Task<string> RunOrchestrator([OrchestrationTrigger] IDurableOrchestrationContext context, ILogger logger)
1616
{
17-
var resultOutputLogs = new List<string>();
18-
var serviceBusQueueMessage = context.GetInput<string>();
1917

20-
// Call the activity function and pass queue message
21-
resultOutputLogs.Add(await context.CallActivityAsync<string>("SendEmail", serviceBusQueueMessage));
22-
23-
//Return output for logs
24-
return resultOutputLogs;
18+
try
19+
{
20+
var serviceBusQueueMessage = context.GetInput<string>();
21+
22+
if (serviceBusQueueMessage != null)
23+
{
24+
// Call the activity function and pass queue message
25+
await context.CallActivityAsync<bool>("SendEmail", serviceBusQueueMessage);
26+
27+
}
28+
return $"Done with the orchestration with Durable Context Id: {context.InstanceId}";
29+
}
30+
catch (Exception ex)
31+
{
32+
//TODO Handle possible errors and do a retry if needed or retry a function
33+
logger.LogError($"Something went wrong " + ex.Message);
34+
throw;
35+
}
36+
2537
}
2638

2739
}

0 commit comments

Comments
 (0)