Repository navigation
Expand file tree
/
Copy pathProcessDocumentFunction.cs
More file actions
51 lines (43 loc) · 1.6 KB
/
Copy pathProcessDocumentFunction.cs
File metadata and controls
51 lines (43 loc) · 1.6 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
using Azure.Messaging.ServiceBus;
using DocumentProcessorFunction.Model;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;
using System;
using System.Text.Json;
using System.Threading.Tasks;
namespace DocumentProcessorFunction;
public class ProcessDocumentFunction
{
private readonly IDocumentEmbeddingService _embeddingService;
private readonly ILogger<ProcessDocumentFunction> _logger;
public ProcessDocumentFunction(
IDocumentEmbeddingService embeddingService,
ILogger<ProcessDocumentFunction> logger)
{
_embeddingService = embeddingService;
_logger = logger;
}
[Function(nameof(ProcessDocumentFunction))]
public async Task Run(
[ServiceBusTrigger("document-queue", Connection = "ServiceBusConnection")]
string messageBody)
{
_logger.LogInformation($"[Worker] Raw payload received from queue: {messageBody}");
DocumentMessageDto? documentData;
try
{
documentData = JsonSerializer.Deserialize<DocumentMessageDto>(messageBody);
}
catch (JsonException ex)
{
_logger.LogError(ex, "[Worker] JSON Deserialization error. Message will retry / move to DLQ.");
throw;
}
if (documentData == null || string.IsNullOrWhiteSpace(documentData.DocumentId))
{
_logger.LogWarning("[Worker] Invalid payload: Missing DocumentId.");
throw new ArgumentException("Payload must contain a valid DocumentId.");
}
await _embeddingService.GenerateAndIndexEmbeddingsAsync(documentData);
}
}