public DojStop CastToDojStop(Stop stop) { DojStop dojStop = new DojStop { LEARecordID = stop.Id, ORI = stop.Ori, TX_Type = CastToDojTXType(stop), SDate = stop.StopDateTime.ToString("MM/dd/yyyy"), STime = stop.Time, SDur = stop.StopDuration.ToString(), Officer = new Officer { UID = stop.OfficerId, ExpYears = stop.ExpYears, AT = stop.OfficerAssignment.Key, ATOth = stop.OfficerAssignment.OtherType, Proxy = "" }, Location = new RIPA.Functions.Submission.Models.Location { Loc = CastToDojLocation(stop.Location), City = stop.Location.City?.Codes?.Code, K12_Flag = stop.Location.School ? "Y" : string.Empty, K12Code = stop.Location.School ? stop.Location.SchoolName.Codes.Code : string.Empty }, Is_ServCall = stop.StopInResponseToCFS ? "Y" : "N", ListPerson_Stopped = stop.ListPersonStopped.Any() ? CastToDojListPersonStopped(stop.ListPersonStopped, stop.Location.School) : null }; return(dojStop); }
private static byte[] GetFileBytes(ILogger log, DojStop dojStop, string runId) { try { log.LogInformation($"Getting file contents: {dojStop.LEARecordID} : {runId}"); var settings = new JsonSerializerSettings() { ContractResolver = new NullToEmptyStringResolver() }; return(Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(dojStop, settings))); } catch (Exception ex) { log.LogError($"Exception: {ex} --> occurred during GetFileBytes with stop id {dojStop.LEARecordID} : {runId}"); return(null); } }
public async Task Run([TimerTrigger("*/10 * * * * *")] TimerInfo myTimer, ILogger log) { Stopwatch runStopwatch = new Stopwatch(); runStopwatch.Start(); string runId = Guid.NewGuid().ToString(); log.LogInformation($"TimersSubmissionConsumer function executing: {runId}"); ServiceBusReceiver serviceBusReceiver = _submissionServiceBusService.SubmissionServiceBusClient.CreateReceiver("submission"); var messages = await _submissionServiceBusService.ReceiveMessagesAsync(serviceBusReceiver); log.LogInformation($"Received message count: {messages.Count} : {runId}"); foreach (var message in messages) { Stopwatch stopStopwatch = new Stopwatch(); stopStopwatch.Start(); await serviceBusReceiver.RenewMessageLockAsync(message); SubmissionMessage submissionMessage = DeserializeQueueItem(log, Encoding.UTF8.GetString(message.Body)); if (submissionMessage == null) { await serviceBusReceiver.DeadLetterMessageAsync(message); continue; } // Get Stop Stop stop = await GetStop(log, submissionMessage.StopId, runId); if (stop == null) { log.LogWarning($"Failed to find stop: {submissionMessage.StopId} : {runId}"); await serviceBusReceiver.AbandonMessageAsync(message); // allows for retry to occur. continue; } DateTime dateSubmitted = DateTime.UtcNow; // Get File Name string fileName = GetFileName(log, submissionMessage.SubmissionId, dateSubmitted, stop.Ori, stop.Id, runId); log.LogInformation($"Using filename: {fileName} : {runId}"); if (fileName == null) { await serviceBusReceiver.AbandonMessageAsync(message); // allows for retry to occur. continue; } // Get Doj Stop DojStop dojStop = GetDojStop(log, stop, runId); if (dojStop == null) { // if the cast error fails report, retry the message if (!await HandledDojCastError(log, stop, dateSubmitted, fileName, submissionMessage.SubmissionId, runId)) { await serviceBusReceiver.AbandonMessageAsync(message); // allows for retry to occur. continue; } else { await serviceBusReceiver.CompleteMessageAsync(message); // message complete continue; } } //Get File Bytes byte[] bytes = GetFileBytes(log, dojStop, runId); if (bytes == null) { log.LogWarning($"Failed to get file contents: {dojStop.LEARecordID} : {runId}"); await serviceBusReceiver.AbandonMessageAsync(message); // allows for retry to occur. continue; } //Upload Blob if (!await UploadBlob(log, bytes, fileName, stop.Id, runId)) { log.LogWarning($"Failed to upload blob: {stop.Id} : {runId}"); await serviceBusReceiver.AbandonMessageAsync(message); // allows for retry to occur. continue; } if (!await UploadSftpFile(log, bytes, fileName, stop.Id, runId, stop)) { log.LogWarning($"Failed to upload to FTP: {stop.Id} : {runId}"); await RemoveBlob(log, fileName, stop.Id, runId); // delete the blob to clean up the failed run await serviceBusReceiver.AbandonMessageAsync(message); // allows for retry to occur. continue; } if (!await HandleDojSubmitSuccess(log, stop, dateSubmitted, submissionMessage.SubmissionId, fileName, runId)) { log.LogWarning($"Failed to handle doj submit success: {stop.Id} : {runId}"); RemoveSftpFile(log, fileName, stop.Id, runId); // remove the file from the SFTP server so it doesnt get duplicated. await serviceBusReceiver.AbandonMessageAsync(message); // allows for retry to occur. continue; } await serviceBusReceiver.CompleteMessageAsync(message); // message complete stopStopwatch.Stop(); log.LogInformation($"Finished processing STOP : {stop.Id} : {stopStopwatch.ElapsedMilliseconds} : {runId}"); } runStopwatch.Stop(); log.LogInformation($"TimersSubmissionConsumer finished: {runStopwatch.ElapsedMilliseconds} : {runId}"); }