-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathGenerateBillOfMaterialsActivity.cs
73 lines (59 loc) · 2.97 KB
/
GenerateBillOfMaterialsActivity.cs
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
71
72
73
using System;
using System.Collections.Concurrent;
using System.IO;
using System.Threading;
using Corgibytes.Freshli.Cli.Functionality.Engine;
using Corgibytes.Freshli.Cli.Services;
using Microsoft.Extensions.DependencyInjection;
namespace Corgibytes.Freshli.Cli.Functionality.BillOfMaterials;
public class GenerateBillOfMaterialsActivity : IApplicationActivity, IMutexed
{
public readonly string AgentExecutablePath;
public readonly Guid AnalysisId;
public readonly int HistoryStopPointId;
public readonly string ManifestPath;
private static readonly ConcurrentDictionary<string, object> s_lockPoints = new();
private static readonly ConcurrentDictionary<string, Mutex> s_historyPointMutexes = new();
public GenerateBillOfMaterialsActivity(Guid analysisId, string agentExecutablePath,
int historyStopPointId, string manifestPath)
{
AnalysisId = analysisId;
AgentExecutablePath = agentExecutablePath;
HistoryStopPointId = historyStopPointId;
ManifestPath = manifestPath;
}
public Mutex GetMutex(IServiceProvider provider)
{
var cacheManager = provider.GetRequiredService<ICacheManager>();
var cacheDb = cacheManager.GetCacheDb();
var historyStopPoint = cacheDb.RetrieveHistoryStopPoint(HistoryStopPointId);
// TODO create an exception class for this exception and write a test to cover it getting generated
_ = historyStopPoint ?? throw new Exception($"Failed to retrieve history stop point {HistoryStopPointId}");
var historyPointPath = historyStopPoint.LocalPath;
EnsureHistoryPointMutexExists(historyPointPath);
return s_historyPointMutexes[historyPointPath];
}
public void Handle(IApplicationEventEngine eventClient)
{
var agentManager = eventClient.ServiceProvider.GetRequiredService<IAgentManager>();
var agentReader = agentManager.GetReader(AgentExecutablePath);
var cacheManager = eventClient.ServiceProvider.GetRequiredService<ICacheManager>();
var cacheDb = cacheManager.GetCacheDb();
var historyStopPoint = cacheDb.RetrieveHistoryStopPoint(HistoryStopPointId);
_ = historyStopPoint ?? throw new Exception($"Failed to retrieve history stop point {HistoryStopPointId}");
var historyPointPath = historyStopPoint.LocalPath;
var asOfDateTime = historyStopPoint.AsOfDateTime;
var fullManifestPath = Path.Combine(historyPointPath, ManifestPath);
var bomFilePath = agentReader.ProcessManifest(fullManifestPath, asOfDateTime);
var cachedBomFilePath = cacheManager.StoreBomInCache(bomFilePath, AnalysisId, asOfDateTime);
eventClient.Fire(new BillOfMaterialsGeneratedEvent(
AnalysisId, HistoryStopPointId, cachedBomFilePath, AgentExecutablePath));
}
private static void EnsureHistoryPointMutexExists(string path)
{
if (!s_historyPointMutexes.ContainsKey(path))
{
s_historyPointMutexes[path] = new Mutex();
}
}
}