-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCopyFailedBatchToBlob.cs
More file actions
67 lines (59 loc) · 2.55 KB
/
CopyFailedBatchToBlob.cs
File metadata and controls
67 lines (59 loc) · 2.55 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
namespace NHS.Screening.ReceiveCaasFile;
using System.Text;
using System.Text.Json;
using Common;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Model;
public class CopyFailedBatchToBlob : ICopyFailedBatchToBlob
{
private readonly ILogger<CopyFailedBatchToBlob> _logger;
private readonly IBlobStorageHelper _blobStorageHelper;
private readonly IExceptionHandler _handleException;
private readonly ReceiveCaasFileConfig _config;
public CopyFailedBatchToBlob(ILogger<CopyFailedBatchToBlob> logger, IBlobStorageHelper blobStorageHelper, IExceptionHandler handleException, IOptions<ReceiveCaasFileConfig> config)
{
_config = config.Value;
_logger = logger;
_blobStorageHelper = blobStorageHelper;
_handleException = handleException;
}
public async Task<bool> writeBatchToBlob(string jsonFromBatch, InvalidOperationException invalidOperationException)
{
using (var stream = GenerateStreamFromString(jsonFromBatch))
{
// we do this so that we do not have files with the same names either failing to be added or over writing another failed batch
var blobFile = new BlobFile(stream, $"failedBatch-{Guid.NewGuid()}.json");
var copied = false;
if (_config.caasfolder_STORAGE != null)
{
copied = await _blobStorageHelper.UploadFileToBlobStorage(new Uri(_config.caasfolder_STORAGE.BlobServiceUri), "failed-batch", blobFile);
}
else
{
var connectionString = Environment.GetEnvironmentVariable("caasfolder_STORAGE");
if (connectionString != null)
{
copied = await _blobStorageHelper.UploadFileToBlobStorage(connectionString, "failed-batch", blobFile);
}
}
if (copied)
{
_logger.LogInformation("adding failed batch to blob was successful");
await _handleException.CreateSystemExceptionLog(invalidOperationException, new Participant(), "file name unknown but batch was copied to FailedBatch blob store");
return true;
}
_logger.LogInformation("adding failed batch to blob was unsuccessful");
return false;
}
}
private static Stream GenerateStreamFromString(string s)
{
var stream = new MemoryStream();
var writer = new StreamWriter(stream);
writer.Write(s);
writer.Flush();
stream.Position = 0;
return stream;
}
}