This repository was archived by the owner on Jul 28, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFileTransformFunction.cs
More file actions
70 lines (60 loc) · 2.7 KB
/
FileTransformFunction.cs
File metadata and controls
70 lines (60 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using Google.Protobuf.WellKnownTypes;
using Microsoft.Azure.Functions.Worker;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using ServiceLayer.Mesh.Configuration;
using ServiceLayer.Mesh.Data;
using ServiceLayer.Mesh.Messaging;
using ServiceLayer.Mesh.Models;
using ServiceLayer.Mesh.Storage;
namespace ServiceLayer.Mesh.Functions;
public class FileTransformFunction(
ILogger<FileTransformFunction> logger,
ServiceLayerDbContext serviceLayerDbContext,
IMeshFilesBlobStore meshFileBlobStore,
IFileTransformFunctionConfiguration configuration)
{
[Function("FileTransformFunction")]
public async Task Run([QueueTrigger("%FileTransformQueueName%")] FileTransformQueueMessage message)
{
await using var transaction = await serviceLayerDbContext.Database.BeginTransactionAsync();
var file = await serviceLayerDbContext.MeshFiles.FirstOrDefaultAsync(f => f.FileId == message.FileId);
if (file == null)
{
logger.LogWarning("File with id: {fileId} not found in MeshFiles table.", message.FileId);
return;
}
if (!IsFileSuitableForTransformation(file))
{
return;
}
await UpdateFileStatusForTransformation(file);
await transaction.CommitAsync();
var fileContent = await meshFileBlobStore.DownloadAsync(file);
// TODO - take dependency on IEnumerable<IFileTransformer>.
// After initial common checks against database, find the appropriate implementation of IFileTransformer to handle the functionality that differs between file type.
}
private async Task UpdateFileStatusForTransformation(MeshFile file)
{
file.Status = MeshFileStatus.Transforming;
file.LastUpdatedUtc = DateTime.UtcNow;
await serviceLayerDbContext.SaveChangesAsync();
}
private bool IsFileSuitableForTransformation(MeshFile file)
{
// We only want to transform files if they are in a Extracted state,
// or are in a Transforming state and were last touched over 12 hours ago.
var expectedStatuses = new[] { MeshFileStatus.Extracted, MeshFileStatus.Transforming };
if (!expectedStatuses.Contains(file.Status) ||
(file.Status == MeshFileStatus.Transforming && file.LastUpdatedUtc > DateTime.UtcNow.AddHours(-configuration.StaleHours)))
{
logger.LogWarning(
"File with id: {fileId} found in MeshFiles table but is not suitable for transformation. Status: {status}, LastUpdatedUtc: {lastUpdatedUtc}.",
file.FileId,
file.Status,
file.LastUpdatedUtc.ToTimestamp());
return false;
}
return true;
}
}