Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: addressing comments made my sonnar cube #575

Merged
merged 3 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ namespace NHS.Screening.RetrieveMeshFile;

using System;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
Expand Down Expand Up @@ -30,7 +31,7 @@ public RetrieveMeshFile(ILogger<RetrieveMeshFile> logger, IMeshToBlobTransferHan
_meshToBlobTransferHandler = meshToBlobTransferHandler;
_blobStorageHelper = blobStorageHelper;
_mailboxId = options.Value.BSSMailBox;
_blobConnectionString = Environment.GetEnvironmentVariable("caasfolder_STORAGE");
_blobConnectionString = Environment.GetEnvironmentVariable("caasfolder_STORAGE");
}
/// <summary>
/// This function polls the MESH Mailbox every 5 minutes, if there is a file posted to the mailbox.
Expand All @@ -39,40 +40,40 @@ public RetrieveMeshFile(ILogger<RetrieveMeshFile> logger, IMeshToBlobTransferHan
[Function("RetrieveMeshFile")]
public async Task RunAsync([TimerTrigger("0 */5 * * * *")] TimerInfo myTimer)
{
_logger.LogInformation("C# Timer trigger function executed at: ,{datetime}",DateTime.Now);
_logger.LogInformation("C# Timer trigger function executed at: ,{datetime}", DateTime.Now);

static bool messageFilter(MessageMetaData i) => true; // No current filter defined there might be business rules here

static string fileNameFunction(MessageMetaData i) => string.Concat(i.MessageId, "_-_", i.WorkflowID,".parquet");
static string fileNameFunction(MessageMetaData i) => string.Concat(i.MessageId, "_-_", i.WorkflowID, ".parquet");

try
{
var shouldExecuteHandShake = await ShouldExecuteHandShake();
var result = await _meshToBlobTransferHandler.MoveFilesFromMeshToBlob(messageFilter, fileNameFunction, _mailboxId,_blobConnectionString,"inbound",shouldExecuteHandShake);
var result = await _meshToBlobTransferHandler.MoveFilesFromMeshToBlob(messageFilter, fileNameFunction, _mailboxId, _blobConnectionString, "inbound", shouldExecuteHandShake);

if(!result)
if (!result)
{
_logger.LogError("An error was encountered while moving files from Mesh to Blob");
}
}
catch(Exception ex)
catch (Exception ex)
{
_logger.LogError(ex,"An error encountered while moving files from Mesh to Blob");
_logger.LogError(ex, "An error encountered while moving files from Mesh to Blob");
}

if (myTimer.ScheduleStatus is not null)
{
_logger.LogInformation("Next timer schedule at: {scheduleStatus}",myTimer.ScheduleStatus.Next);
_logger.LogInformation("Next timer schedule at: {scheduleStatus}", myTimer.ScheduleStatus.Next);
}
}

private async Task<bool> ShouldExecuteHandShake()
{

Dictionary<string,string> configValues;
Dictionary<string, string> configValues;
TimeSpan handShakeInterval = new TimeSpan(0, 23, 54, 0);
var meshState = await _blobStorageHelper.GetFileFromBlobStorage(_blobConnectionString,"config",ConfigFileName);
if(meshState == null)
var meshState = await _blobStorageHelper.GetFileFromBlobStorage(_blobConnectionString, "config", ConfigFileName);
if (meshState == null)
{

_logger.LogInformation("MeshState File did not exist, Creating new MeshState File in blob Storage");
Expand All @@ -85,15 +86,16 @@ private async Task<bool> ShouldExecuteHandShake()
return true;

}
using(StreamReader reader = new StreamReader(meshState.Data)){
meshState.Data.Seek(0,SeekOrigin.Begin);
using (StreamReader reader = new StreamReader(meshState.Data))
{
meshState.Data.Seek(0, SeekOrigin.Begin);
string jsonData = await reader.ReadToEndAsync();
configValues = JsonSerializer.Deserialize<Dictionary<string,string>>(jsonData);
configValues = JsonSerializer.Deserialize<Dictionary<string, string>>(jsonData);
}

string nextHandShakeDateString;
//config value doenst exist
if(!configValues.TryGetValue(NextHandShakeTimeConfigKey, out nextHandShakeDateString))
if (!configValues.TryGetValue(NextHandShakeTimeConfigKey, out nextHandShakeDateString))
{
_logger.LogInformation("NextHandShakeTime config item does not exist, creating new config item");
configValues.Add(NextHandShakeTimeConfigKey, DateTime.UtcNow.Add(handShakeInterval).ToString());
Expand All @@ -104,15 +106,16 @@ private async Task<bool> ShouldExecuteHandShake()
}
DateTime nextHandShakeDateTime;
//date cannot be parsed
if(!DateTime.TryParse(nextHandShakeDateString, out nextHandShakeDateTime))
if (!DateTime.TryParse(nextHandShakeDateString, CultureInfo.InvariantCulture, out nextHandShakeDateTime))
{
_logger.LogInformation("Unable to Parse NextHandShakeTime, Updating config value");
configValues[NextHandShakeTimeConfigKey] = DateTime.UtcNow.Add(handShakeInterval).ToString();
SetConfigState(configValues);
return true;
}

if(DateTime.Compare(nextHandShakeDateTime,DateTime.UtcNow) <= 0){
if (DateTime.Compare(nextHandShakeDateTime, DateTime.UtcNow) <= 0)
{
_logger.LogInformation("Next HandShakeTime was in the past, will execute handshake");
var NextHandShakeTimeConfig = DateTime.UtcNow.Add(handShakeInterval).ToString();

Expand All @@ -127,20 +130,21 @@ private async Task<bool> ShouldExecuteHandShake()
}


private async Task<bool> SetConfigState(Dictionary<string,string> state)
private async Task<bool> SetConfigState(Dictionary<string, string> state)
{
try{
try
{
string jsonString = JsonSerializer.Serialize(state);
using(var stream = GenerateStreamFromString(jsonString))
using (var stream = GenerateStreamFromString(jsonString))
{
var blobFile = new BlobFile(stream,ConfigFileName);
var result = await _blobStorageHelper.UploadFileToBlobStorage(_blobConnectionString,"config",blobFile,true);
var blobFile = new BlobFile(stream, ConfigFileName);
var result = await _blobStorageHelper.UploadFileToBlobStorage(_blobConnectionString, "config", blobFile, true);
return result;
}
}
catch(Exception ex)
catch (Exception ex)
{
_logger.LogError(ex,"Unable To set Config State");
_logger.LogError(ex, "Unable To set Config State");
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ public async Task Run([QueueTrigger("%AddQueueName%", Connection = "AzureWebJobs
{
_logger.LogInformation(ex, "Unable to call function.\nMessage: {Message}\nStack Trace: {StackTrace}", ex.Message, ex.StackTrace);
await _handleException.CreateSystemExceptionLog(ex, basicParticipantCsvRecord.Participant, basicParticipantCsvRecord.FileName);
return;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ namespace Common;

public class AzureQueueStorageHelper : IAzureQueueStorageHelper
{
private QueueClient _queueClient;

private readonly string storageConnectionString;

Expand All @@ -23,7 +22,7 @@ public AzureQueueStorageHelper(ILogger<AzureQueueStorageHelper> logger)

public async Task<bool> AddItemToQueueAsync<T>(T participantCsvRecord, string queueName)
{
_queueClient = new QueueClient(storageConnectionString, queueName);
var _queueClient = new QueueClient(storageConnectionString, queueName);
await _queueClient.CreateIfNotExistsAsync();
var json = JsonSerializer.Serialize(participantCsvRecord);
var bytes = Encoding.UTF8.GetBytes(json);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ public static class EnumHelper
public static string GetDisplayName(Enum enumValue)
{
var displayName = enumValue.GetType()
.GetMember(enumValue.ToString())
.First()
.GetMember(enumValue.ToString())[0]
.GetCustomAttribute<DisplayAttribute>()?
.GetName();

Expand Down
Loading