Skip to content

Commit 153afd0

Browse files
Merge pull request #2 from jonahandersson/dev-jonah
Updated dev version
2 parents e10db23 + d2d54e3 commit 153afd0

File tree

4 files changed

+63
-52
lines changed

4 files changed

+63
-52
lines changed

MessageSenderService/Activities/SendEmailActivity.cs

Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,24 @@
66
using System.Text;
77
using System.Threading.Tasks;
88
using Microsoft.Extensions.Logging;
9+
using SendGrid;
10+
using SendGrid.Helpers.Mail;
911

1012
namespace MessageSenderService.DurableFunctions.Activities
1113
{
1214
public static class SendEmailActivity
1315
{
1416
[FunctionName("SendEmail")]
15-
public static string SendEmail([ActivityTrigger] string serviceBusQueueMessage, ILogger log)
16-
{
17-
17+
public static async Task<bool> SendEmail([ActivityTrigger] string serviceBusQueueMessage, ILogger log)
18+
{
1819

1920
try
2021
{
2122
log.LogInformation($"SendEmailActivity trigged from the durable orchestratoion");
2223

23-
if (serviceBusQueueMessage != null)
24-
{
25-
log.LogInformation($"Sending email to recipient with queue message: {serviceBusQueueMessage}.");
26-
var result = SendEmailAsync(serviceBusQueueMessage);
27-
}
28-
29-
30-
31-
32-
33-
log.LogInformation($"Sending email with message: {serviceBusQueueMessage}");
34-
return $"Email sent!";
24+
log.LogInformation($"Sending email to recipient with queue message: {serviceBusQueueMessage}.");
25+
bool result = await SendEmailAsync(serviceBusQueueMessage);
26+
return result;
3527
}
3628
catch (Exception)
3729
{
@@ -41,34 +33,38 @@ public static string SendEmail([ActivityTrigger] string serviceBusQueueMessage,
4133

4234
}
4335

44-
private async Task<bool> SendEmailAsync(string serviceBusQueueMessage)
45-
{
46-
var apiKey = Environment.GetEnvironmentVariable("SendGrid_API_KEY");
47-
var adminEmailAddress = Environment.GetEnvironmentVariable("AdminEmailAddress");
48-
var recipientEmailAddress = Environment.GetEnvironmentVariable("RecipientEmailAddress");
49-
50-
var client = new SendGridClient(apiKey);
51-
var emailMessage = new SendGridMessage()
36+
private static async Task<bool> SendEmailAsync(string serviceBusQueueMessage)
37+
{
38+
try
5239
{
53-
From = new EmailAddress(adminEmailAddress, "Jonah Andersson"),
54-
Subject = "Email from Service Bus Receive Function for Azure Back to School 2022!",
55-
PlainTextContent = myQueueMessageItem
56-
};
40+
var apiKey = Environment.GetEnvironmentVariable("SendGrid_API_KEY");
41+
var adminEmailAddress = Environment.GetEnvironmentVariable("AdminEmailAddress");
42+
var recipientEmailAddress = Environment.GetEnvironmentVariable("RecipientEmailAddress");
5743

58-
emailMessage.AddTo(new EmailAddress(recipientEmailAddress, "Jonah at Work Email"));
59-
var response = await client.SendEmailAsync(emailMessage);
44+
var client = new SendGridClient(apiKey);
45+
var emailMessage = new SendGridMessage()
46+
{
47+
From = new EmailAddress(adminEmailAddress, "Jonah Andersson"),
48+
Subject = "Email from Service Bus Receive Function for Azure Back to School 2022!",
49+
PlainTextContent = serviceBusQueueMessage
50+
};
6051

61-
// A success status code means SendGrid received the email request and will process it.
62-
// Errors can still occur when SendGrid tries to send the email.
63-
// If email is not received, use this URL to debug: https://app.sendgrid.com/email_activity
64-
Console.WriteLine(response.IsSuccessStatusCode ? "Email queued successfully!" : "Something went wrong!");
65-
try
66-
{
52+
emailMessage.AddTo(new EmailAddress(recipientEmailAddress, "Jonah at Work Email"));
53+
var response = await client.SendEmailAsync(emailMessage);
54+
55+
// A success status code means SendGrid received the email request and will process it.
56+
// Errors can still occur when SendGrid tries to send the email.
57+
// If email is not received, use this URL to debug: https://app.sendgrid.com/email_activity
58+
Console.WriteLine(response.IsSuccessStatusCode ? "Email queued successfully!" : "Something went wrong!");
59+
60+
if (response.IsSuccessStatusCode)
61+
return true;
62+
else return false;
6763

6864
}
6965
catch (Exception)
7066
{
71-
67+
//TODO: More robust error general handling here
7268
throw;
7369
}
7470
}

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: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
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" />
8-
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.DurableTask" Version="2.7.1" />
9-
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.ServiceBus" Version="4.3.0" />
10-
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.1.1" />
9+
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.DurableTask" Version="2.8.0" />
10+
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.ServiceBus" Version="5.7.0" />
11+
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="5.0.0" />
12+
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.1.3" />
13+
<PackageReference Include="SendGrid" Version="9.28.1" />
1114
</ItemGroup>
1215
<ItemGroup>
1316
<None Update="host.json">

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)