1- using Azure . Storage . Blobs . Models ;
21using Microsoft . Azure . Functions . Worker ;
32using Microsoft . EntityFrameworkCore ;
43using Microsoft . Extensions . Logging ;
@@ -15,6 +14,7 @@ public class FileExtractFunction(
1514 IMeshInboxService meshInboxService ,
1615 ServiceLayerDbContext serviceLayerDbContext ,
1716 IFileTransformQueueClient fileTransformQueueClient ,
17+ IFileExtractQueueClient fileExtractQueueClient ,
1818 IMeshFilesBlobStore meshFileBlobStore )
1919{
2020 [ Function ( "FileExtractFunction" ) ]
@@ -51,57 +51,41 @@ public async Task Run([QueueTrigger("file-extract")] FileExtractQueueMessage mes
5151 await serviceLayerDbContext . SaveChangesAsync ( ) ;
5252 await transaction . CommitAsync ( ) ;
5353
54- // TODO - approx after this point we'll need to wrap everything in a try-catch. On failure we should
55- // update the meshfile to FailedExtract, and move the message to the poison queue
56-
57- var mailboxId = Environment . GetEnvironmentVariable ( "NBSSMailBoxId" )
58- ?? throw new InvalidOperationException ( $ "Environment variable 'NBSSMailBoxId' is not set or is empty.") ;
59-
60- var meshResponse = await meshInboxService . GetMessageByIdAsync ( mailboxId , file . FileId ) ;
61- if ( ! meshResponse . IsSuccessful )
54+ try
6255 {
63- // TODO - what to do if unsuccessful?
64- throw new InvalidOperationException ( $ "Mesh extraction failed: { meshResponse . Error } ") ;
56+ var mailboxId = Environment . GetEnvironmentVariable ( "NBSSMailBoxId" )
57+ ?? throw new InvalidOperationException ( $ "Environment variable 'NBSSMailBoxId' is not set or is empty.") ;
58+
59+ var meshResponse = await meshInboxService . GetMessageByIdAsync ( mailboxId , file . FileId ) ;
60+ if ( ! meshResponse . IsSuccessful )
61+ {
62+ // TODO - what to do if unsuccessful?
63+ throw new InvalidOperationException ( $ "Mesh extraction failed: { meshResponse . Error } ") ;
64+ }
65+
66+ var blobPath = await meshFileBlobStore . UploadAsync ( file , meshResponse . Response . FileAttachment . Content ) ;
67+
68+ var meshAcknowledgementResponse = await meshInboxService . AcknowledgeMessageByIdAsync ( mailboxId , message . FileId ) ;
69+ if ( ! meshResponse . IsSuccessful )
70+ {
71+ // TODO - what to do if unsuccessful?
72+ throw new InvalidOperationException ( $ "Mesh acknowledgement failed: { meshResponse . Error } ") ;
73+ }
74+
75+ file . Status = MeshFileStatus . Extracted ;
76+ file . LastUpdatedUtc = DateTime . UtcNow ;
77+ file . BlobPath = blobPath ;
78+ await serviceLayerDbContext . SaveChangesAsync ( ) ;
79+
80+ await fileTransformQueueClient . EnqueueFileTransformAsync ( file ) ;
81+ }
82+ catch ( Exception ex )
83+ {
84+ logger . LogError ( ex , "" ) ;
85+ file . Status = MeshFileStatus . FailedExtract ;
86+ file . LastUpdatedUtc = DateTime . UtcNow ;
87+ await serviceLayerDbContext . SaveChangesAsync ( ) ;
88+ await fileExtractQueueClient . SendToPoisonQueueAsync ( message ) ;
6589 }
66-
67- await meshFileBlobStore . UploadAsync ( file , meshResponse . Response . FileAttachment . Content ) ;
6890 }
69-
70- // public async Task<bool> UploadFileToBlobStorage(BlobFile blobFile, bool overwrite = false)
71- // {
72- // var blobClient = blobContainerClient.GetBlobClient(blobFile.FileName);
73- //
74- // await blobContainerClient.CreateIfNotExistsAsync(PublicAccessType.None);
75- //
76- // try
77- // {
78- // await blobClient.UploadAsync(blobFile.Data, overwrite: overwrite);
79- // }
80- // catch (Exception ex)
81- // {
82- // logger.LogError(ex, "There has been a problem while uploading the file: {Message}", ex.Message);
83- // return false;
84- // }
85- //
86- // return true;
87- // }
8891}
89-
90- // public class BlobFile
91- // {
92- // public BlobFile(byte[] bytes, string fileName)
93- // {
94- // Data = new MemoryStream(bytes);
95- // FileName = fileName;
96- // }
97- // public BlobFile(Stream stream, string fileName)
98- // {
99- // Data = stream;
100- // FileName = fileName;
101- // }
102- //
103- // public Stream Data { get; set; }
104- // public string FileName { get; set; }
105- // }
106-
107-
0 commit comments