|
| 1 | +using Azure.Storage.Blobs; |
| 2 | +using Azure.Storage.Queues; |
| 3 | +using Microsoft.Azure.Functions.Worker; |
| 4 | +using Microsoft.EntityFrameworkCore; |
| 5 | +using Microsoft.Extensions.Logging; |
| 6 | +using NHS.MESH.Client.Contracts.Services; |
| 7 | +using ServiceLayer.Mesh.Data; |
| 8 | +using ServiceLayer.Mesh.Models; |
| 9 | + |
| 10 | +namespace ServiceLayer.Mesh.Functions; |
| 11 | + |
| 12 | +public class ExtractFunction |
| 13 | +{ |
| 14 | + private readonly ILogger _logger; |
| 15 | + private readonly IMeshInboxService _meshInboxService; |
| 16 | + private readonly ServiceLayerDbContext _serviceLayerDbContext; |
| 17 | + private readonly QueueClient _queueClient; |
| 18 | + private readonly BlobClient _blobClient; |
| 19 | + |
| 20 | + |
| 21 | + public ExtractFunction(ILogger logger, IMeshInboxService meshInboxService, ServiceLayerDbContext serviceLayerDbContext, QueueClient queueClient, BlobClient blobClient) |
| 22 | + { |
| 23 | + _logger = logger; |
| 24 | + _meshInboxService = meshInboxService; |
| 25 | + _serviceLayerDbContext = serviceLayerDbContext; |
| 26 | + _queueClient = queueClient; |
| 27 | + _blobClient = blobClient; |
| 28 | + } |
| 29 | + |
| 30 | + [Function("ExtractFunction")] |
| 31 | + public async Task Run([QueueTrigger("TODO")] ExtractQueueMessage message) |
| 32 | + { |
| 33 | + _logger.LogInformation($"ExtractFunction started at: {DateTime.Now}"); |
| 34 | + |
| 35 | + await using var transaction = await _serviceLayerDbContext.Database.BeginTransactionAsync(); |
| 36 | + |
| 37 | + var file = await _serviceLayerDbContext.MeshFiles |
| 38 | + .FirstOrDefaultAsync(f => f.FileId == message.FileId); |
| 39 | + |
| 40 | + if (file == null) |
| 41 | + { |
| 42 | + // TODO - do we want to throw exception or just exit silently? |
| 43 | + throw new InvalidOperationException("File not found"); |
| 44 | + } |
| 45 | + |
| 46 | + // We only want to extract files if they are in a Discovered state, |
| 47 | + // or are in an Extracting state and have been last touched over 12 hours ago. |
| 48 | + var expectedStatuses = new[] { MeshFileStatus.Discovered, MeshFileStatus.Extracting }; |
| 49 | + if (!expectedStatuses.Contains(file.Status) || |
| 50 | + file.Status == MeshFileStatus.Extracting && file.LastUpdatedUtc > DateTime.UtcNow.AddHours(-12)) |
| 51 | + { |
| 52 | + // TODO - do we want to throw exception or just exit silently? |
| 53 | + throw new InvalidOperationException("File is not in expected status"); |
| 54 | + } |
| 55 | + |
| 56 | + file.Status = MeshFileStatus.Extracting; |
| 57 | + file.LastUpdatedUtc = DateTime.UtcNow; |
| 58 | + |
| 59 | + await _serviceLayerDbContext.SaveChangesAsync(); |
| 60 | + await transaction.CommitAsync(); |
| 61 | + |
| 62 | + var mailboxId = Environment.GetEnvironmentVariable("BSSMailBox") |
| 63 | + ?? throw new InvalidOperationException($"Environment variable 'BSSMailBox' is not set or is empty."); |
| 64 | + |
| 65 | + var meshResponse = await _meshInboxService.GetMessageByIdAsync(mailboxId, file.FileId); |
| 66 | + if (!meshResponse.IsSuccessful) |
| 67 | + { |
| 68 | + // TODO - what to do if unsuccessful? |
| 69 | + throw new InvalidOperationException($"Mesh extraction failed: {meshResponse.Error}"); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | +} |
| 74 | + |
| 75 | +public class ExtractQueueMessage |
| 76 | +{ |
| 77 | + public string FileId { get; set; } |
| 78 | +} |
0 commit comments