Skip to content
This repository was archived by the owner on Jul 28, 2025. It is now read-only.

Commit 1950834

Browse files
committed
wip
1 parent 83e0f79 commit 1950834

2 files changed

Lines changed: 80 additions & 0 deletions

File tree

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
}

src/ServiceLayer.Mesh/ServiceLayer.Mesh.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@
99
<ItemGroup>
1010
<FrameworkReference Include="Microsoft.AspNetCore.App" />
1111
<PackageReference Include="Azure.Identity" Version="1.13.2" />
12+
<PackageReference Include="Azure.Storage.Blobs" Version="12.24.0" />
1213
<PackageReference Include="Azure.Storage.Queues" Version="12.22.0" />
1314
<!-- Application Insights isn't enabled by default. See https://aka.ms/AAt8mw4. -->
1415
<!-- <PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.22.0" /> -->
1516
<!-- <PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="2.0.0" /> -->
1617
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="2.0.0" />
1718
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore" Version="2.0.0" />
19+
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Storage.Queues" Version="5.5.2" />
1820
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Timer" Version="4.3.1" />
1921
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="2.0.0" />
2022
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.1" />

0 commit comments

Comments
 (0)