lock required for transcript writer

This commit is contained in:
benbierens 2024-07-26 13:21:29 +02:00
parent fb4d520ba4
commit 4de35b579f
No known key found for this signature in database
GPG Key ID: 877D2C2E09A22F3A
1 changed files with 49 additions and 32 deletions

View File

@ -13,6 +13,7 @@ namespace OverwatchTranscript
public class TranscriptWriter : ITranscriptWriter
{
private readonly object _lock = new object();
private readonly string transcriptFile;
private readonly string artifactsFolder;
private readonly Dictionary<string, string> header = new Dictionary<string, string>();
@ -43,20 +44,26 @@ namespace OverwatchTranscript
Payload = JsonConvert.SerializeObject(payload)
};
if (buffer.ContainsKey(utc))
lock (_lock)
{
buffer[utc].Add(newEvent);
}
else
{
buffer.Add(utc, new List<OverwatchEvent> { newEvent });
if (buffer.ContainsKey(utc))
{
buffer[utc].Add(newEvent);
}
else
{
buffer.Add(utc, new List<OverwatchEvent> { newEvent });
}
}
}
public void AddHeader(string key, object value)
{
CheckClosed();
header.Add(key, JsonConvert.SerializeObject(value));
lock (_lock)
{
header.Add(key, JsonConvert.SerializeObject(value));
}
}
public void IncludeArtifact(string filePath)
@ -73,31 +80,7 @@ namespace OverwatchTranscript
CheckClosed();
closed = true;
var model = new OverwatchTranscript
{
Header = new OverwatchHeader
{
Entries = header.Select(h =>
{
return new OverwatchHeaderEntry
{
Key = h.Key,
Value = h.Value
};
}).ToArray()
},
Moments = buffer.Select(p =>
{
return new OverwatchMoment
{
Utc = p.Key,
Events = p.Value.ToArray()
};
}).ToArray()
};
header.Clear();
buffer.Clear();
var model = CreateModel();
File.WriteAllText(transcriptFile, JsonConvert.SerializeObject(model, Formatting.Indented));
@ -106,6 +89,40 @@ namespace OverwatchTranscript
Directory.Delete(workingDir, true);
}
private OverwatchTranscript CreateModel()
{
lock (_lock)
{
var model = new OverwatchTranscript
{
Header = new OverwatchHeader
{
Entries = header.Select(h =>
{
return new OverwatchHeaderEntry
{
Key = h.Key,
Value = h.Value
};
}).ToArray()
},
Moments = buffer.Select(p =>
{
return new OverwatchMoment
{
Utc = p.Key,
Events = p.Value.ToArray()
};
}).ToArray()
};
header.Clear();
buffer.Clear();
return model;
}
}
private void CheckClosed()
{
if (closed) throw new Exception("Transcript has already been written. Cannot modify or write again.");