Skip to content

Commit

Permalink
Changed how the configuration is built in order to not hit the multip…
Browse files Browse the repository at this point in the history
…le handles error (#139)
  • Loading branch information
jotrick authored Dec 20, 2019
1 parent 5c21078 commit b92641a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public class CompanyCommunicatorDataFunction

private static SendingNotificationDataRepository sendingNotificationDataRepository = null;

private static IConfiguration configuration = null;

/// <summary>
/// Azure Function App triggered by messages from a Service Bus queue
/// Used for aggregating results for a sent notification.
Expand All @@ -52,31 +54,30 @@ public async Task Run(
ILogger log,
ExecutionContext context)
{
IConfiguration configuration = new ConfigurationBuilder()
.SetBasePath(context.FunctionAppDirectory)
.AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables()
.Build();
CompanyCommunicatorDataFunction.configuration = CompanyCommunicatorDataFunction.configuration ??
new ConfigurationBuilder()
.AddEnvironmentVariables()
.Build();

// Simply initialize the variable for certain build environments and versions
var maxMinutesOfRetryingDataFunction = 0;

// If parsing fails, out variable is set to 0, so need to set the default
if (!int.TryParse(configuration["MaxMinutesOfRetryingDataFunction"], out maxMinutesOfRetryingDataFunction))
if (!int.TryParse(CompanyCommunicatorDataFunction.configuration["MaxMinutesOfRetryingDataFunction"], out maxMinutesOfRetryingDataFunction))
{
maxMinutesOfRetryingDataFunction = 1440;
}

var messageContent = JsonConvert.DeserializeObject<ServiceBusDataQueueMessageContent>(myQueueItem);

CompanyCommunicatorDataFunction.sentNotificationDataRepository = CompanyCommunicatorDataFunction.sentNotificationDataRepository
?? new SentNotificationDataRepository(configuration, isFromAzureFunction: true);
?? new SentNotificationDataRepository(CompanyCommunicatorDataFunction.configuration, isFromAzureFunction: true);

CompanyCommunicatorDataFunction.notificationDataRepository = CompanyCommunicatorDataFunction.notificationDataRepository
?? this.CreateNotificationRepository(configuration);
?? this.CreateNotificationRepository(CompanyCommunicatorDataFunction.configuration);

CompanyCommunicatorDataFunction.sendingNotificationDataRepository = CompanyCommunicatorDataFunction.sendingNotificationDataRepository
?? new SendingNotificationDataRepository(configuration, isFromAzureFunction: true);
?? new SendingNotificationDataRepository(CompanyCommunicatorDataFunction.configuration, isFromAzureFunction: true);

var sentNotificationDataEntities = await CompanyCommunicatorDataFunction.sentNotificationDataRepository.GetAllAsync(
messageContent.NotificationId);
Expand All @@ -89,7 +90,7 @@ public async Task Run(
return;
}

await this.SendTriggerToDataFunction(configuration, messageContent);
await this.SendTriggerToDataFunction(CompanyCommunicatorDataFunction.configuration, messageContent);
return;
}

Expand Down Expand Up @@ -151,7 +152,7 @@ public async Task Run(
}
else
{
await this.SendTriggerToDataFunction(configuration, messageContent);
await this.SendTriggerToDataFunction(CompanyCommunicatorDataFunction.configuration, messageContent);
}

var operation = TableOperation.InsertOrMerge(notificationDataEntityUpdate);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public class CompanyCommunicatorSendFunction

private static MessageSender sendQueueServiceBusMessageSender = null;

private static IConfiguration configuration = null;

/// <summary>
/// Azure Function App triggered by messages from a Service Bus queue
/// Used for sending messages from the bot.
Expand All @@ -72,11 +74,10 @@ public async Task Run(
{
log.LogInformation($"C# ServiceBus queue trigger function processed message: {myQueueItem}");

IConfiguration configuration = new ConfigurationBuilder()
.SetBasePath(context.FunctionAppDirectory)
.AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables()
.Build();
CompanyCommunicatorSendFunction.configuration = CompanyCommunicatorSendFunction.configuration ??
new ConfigurationBuilder()
.AddEnvironmentVariables()
.Build();

var messageContent = JsonConvert.DeserializeObject<ServiceBusSendQueueMessageContent>(myQueueItem);

Expand All @@ -88,7 +89,7 @@ public async Task Run(
var maxNumberOfAttempts = 0;

// If parsing fails, out variable is set to 0, so need to set the default
if (!int.TryParse(configuration["MaxNumberOfAttempts"], out maxNumberOfAttempts))
if (!int.TryParse(CompanyCommunicatorSendFunction.configuration["MaxNumberOfAttempts"], out maxNumberOfAttempts))
{
maxNumberOfAttempts = 1;
}
Expand All @@ -97,26 +98,26 @@ public async Task Run(
?? new HttpClient();

CompanyCommunicatorSendFunction.userDataRepository = CompanyCommunicatorSendFunction.userDataRepository
?? new UserDataRepository(configuration, isFromAzureFunction: true);
?? new UserDataRepository(CompanyCommunicatorSendFunction.configuration, isFromAzureFunction: true);

CompanyCommunicatorSendFunction.sendingNotificationDataRepository = CompanyCommunicatorSendFunction.sendingNotificationDataRepository
?? new SendingNotificationDataRepository(configuration, isFromAzureFunction: true);
?? new SendingNotificationDataRepository(CompanyCommunicatorSendFunction.configuration, isFromAzureFunction: true);

CompanyCommunicatorSendFunction.globalSendingNotificationDataRepository = CompanyCommunicatorSendFunction.globalSendingNotificationDataRepository
?? new GlobalSendingNotificationDataRepository(configuration, isFromAzureFunction: true);
?? new GlobalSendingNotificationDataRepository(CompanyCommunicatorSendFunction.configuration, isFromAzureFunction: true);

CompanyCommunicatorSendFunction.sentNotificationDataRepository = CompanyCommunicatorSendFunction.sentNotificationDataRepository
?? new SentNotificationDataRepository(configuration, isFromAzureFunction: true);
?? new SentNotificationDataRepository(CompanyCommunicatorSendFunction.configuration, isFromAzureFunction: true);

if (CompanyCommunicatorSendFunction.botAccessToken == null
|| CompanyCommunicatorSendFunction.botAccessTokenExpiration == null
|| DateTime.UtcNow > CompanyCommunicatorSendFunction.botAccessTokenExpiration)
{
await this.FetchTokenAsync(configuration, CompanyCommunicatorSendFunction.httpClient);
await this.FetchTokenAsync(CompanyCommunicatorSendFunction.configuration, CompanyCommunicatorSendFunction.httpClient);
}

CompanyCommunicatorSendFunction.sendQueueServiceBusMessageSender = CompanyCommunicatorSendFunction.sendQueueServiceBusMessageSender
?? new MessageSender(configuration["ServiceBusConnection"], CompanyCommunicatorSendFunction.SendQueueName);
?? new MessageSender(CompanyCommunicatorSendFunction.configuration["ServiceBusConnection"], CompanyCommunicatorSendFunction.SendQueueName);

var getActiveNotificationEntityTask = CompanyCommunicatorSendFunction.sendingNotificationDataRepository.GetAsync(
PartitionKeyNames.NotificationDataTable.SendingNotificationsPartition,
Expand Down Expand Up @@ -151,7 +152,7 @@ public async Task Run(
if (globalSendingNotificationDataEntity?.SendRetryDelayTime != null
&& DateTime.UtcNow < globalSendingNotificationDataEntity.SendRetryDelayTime)
{
await this.SendDelayedRetryOfMessageToSendFunction(configuration, messageContent);
await this.SendDelayedRetryOfMessageToSendFunction(CompanyCommunicatorSendFunction.configuration, messageContent);

return;
}
Expand Down Expand Up @@ -184,7 +185,7 @@ public async Task Run(
"Bearer",
CompanyCommunicatorSendFunction.botAccessToken);

var payloadString = "{\"bot\": { \"id\": \"28:" + configuration["MicrosoftAppId"] + "\"},\"isGroup\": false, \"tenantId\": \"" + incomingUserDataEntity.TenantId + "\", \"members\": [{\"id\": \"" + incomingUserDataEntity.UserId + "\"}]}";
var payloadString = "{\"bot\": { \"id\": \"28:" + CompanyCommunicatorSendFunction.configuration["MicrosoftAppId"] + "\"},\"isGroup\": false, \"tenantId\": \"" + incomingUserDataEntity.TenantId + "\", \"members\": [{\"id\": \"" + incomingUserDataEntity.UserId + "\"}]}";
requestMessage.Content = new StringContent(payloadString, Encoding.UTF8, "application/json");

using (var sendResponse = await CompanyCommunicatorSendFunction.httpClient.SendAsync(requestMessage))
Expand Down Expand Up @@ -222,7 +223,6 @@ public async Task Run(
else
{
await this.SaveSentNotificationData(
configuration,
messageContent.NotificationId,
incomingUserDataEntity.AadId,
totalNumberOfThrottles,
Expand All @@ -237,7 +237,7 @@ await this.SaveSentNotificationData(

if (isCreateConversationThrottled)
{
await this.SetDelayTimeAndSendDelayedRetry(configuration, messageContent);
await this.SetDelayTimeAndSendDelayedRetry(CompanyCommunicatorSendFunction.configuration, messageContent);

return;
}
Expand Down Expand Up @@ -265,7 +265,6 @@ await this.SaveSentNotificationData(
log.LogInformation("MESSAGE SENT SUCCESSFULLY");

saveSentNotificationDataTask = this.SaveSentNotificationData(
configuration,
messageContent.NotificationId,
incomingUserDataEntity.AadId,
totalNumberOfThrottles,
Expand Down Expand Up @@ -296,7 +295,6 @@ await this.SaveSentNotificationData(
log.LogError($"MESSAGE FAILED: {sendResponse.StatusCode}");

saveSentNotificationDataTask = this.SaveSentNotificationData(
configuration,
messageContent.NotificationId,
incomingUserDataEntity.AadId,
totalNumberOfThrottles,
Expand All @@ -314,7 +312,7 @@ await this.SaveSentNotificationData(
if (isSendMessageThrottled)
{
setDelayTimeAndSendDelayedRetryTask =
this.SetDelayTimeAndSendDelayedRetry(configuration, messageContent);
this.SetDelayTimeAndSendDelayedRetry(CompanyCommunicatorSendFunction.configuration, messageContent);
}

await Task.WhenAll(
Expand All @@ -333,7 +331,6 @@ await Task.WhenAll(
}

await this.SaveSentNotificationData(
configuration,
messageContent.NotificationId,
messageContent.UserDataEntity.AadId,
totalNumberOfThrottles,
Expand All @@ -345,7 +342,6 @@ await this.SaveSentNotificationData(
}

private async Task SaveSentNotificationData(
IConfiguration configuration,
string notificationId,
string aadId,
int totalNumberOfThrottles,
Expand Down

0 comments on commit b92641a

Please sign in to comment.