Fix bug in TelemetryPublisherDisk not flushing & add file extension in connection string

This commit is contained in:
Denis-Cosmin Nutiu 2020-01-24 15:13:09 +02:00
parent 9f8370c651
commit 805a132183
2 changed files with 16 additions and 4 deletions

View file

@ -1,3 +1,4 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Text; using System.Text;
@ -15,12 +16,14 @@ namespace NucuCar.Domain.Telemetry
public class TelemetryPublisherDisk : TelemetryPublisher public class TelemetryPublisherDisk : TelemetryPublisher
{ {
private readonly FileStream _fileStream; private readonly FileStream _fileStream;
private int _charactersWritten;
/// <summary> /// <summary>
/// Constructs an instance of <see cref="TelemetryPublisherDisk"/>. /// Constructs an instance of <see cref="TelemetryPublisherDisk"/>.
/// <remarks> /// <remarks>
/// The connection string must contain the following options: /// The connection string must contain the following options:
/// Filename (required) - The path of the filename in which to log telemetry data. /// Filename (required) - The path of the filename in which to log telemetry data.
/// FileExtension (optional) - The extension of the filename. Default txt
/// BufferSize (optional) - The buffer size for the async writer. Default: 4096 /// BufferSize (optional) - The buffer size for the async writer. Default: 4096
/// </remarks> /// </remarks>
/// </summary> /// </summary>
@ -28,10 +31,11 @@ namespace NucuCar.Domain.Telemetry
public TelemetryPublisherDisk(TelemetryPublisherBuilderOptions opts) : base(opts) public TelemetryPublisherDisk(TelemetryPublisherBuilderOptions opts) : base(opts)
{ {
var connectionStringParams = ConnectionStringParser.Parse(opts.ConnectionString); var connectionStringParams = ConnectionStringParser.Parse(opts.ConnectionString);
var fileName = connectionStringParams.GetValueOrDefault("Filename"); var fileName = connectionStringParams.GetValueOrDefault("FileName");
var fileExtension = connectionStringParams.GetValueOrDefault("FileExtension", "txt");
var bufferSize = connectionStringParams.GetValueOrDefault("BufferSize", "4096"); var bufferSize = connectionStringParams.GetValueOrDefault("BufferSize", "4096");
_fileStream = new FileStream(fileName, FileMode.Append, FileAccess.Write, _fileStream = new FileStream(NormalizeFilename(fileName, fileExtension), FileMode.Append, FileAccess.Write,
FileShare.Read, int.Parse(bufferSize), true); FileShare.Read, int.Parse(bufferSize), true);
Logger?.LogDebug("Initialized the TelemetryPublisherDisk!"); Logger?.LogDebug("Initialized the TelemetryPublisherDisk!");
} }
@ -43,13 +47,21 @@ namespace NucuCar.Domain.Telemetry
Logger?.LogDebug($"Telemetry message: {messageString}"); Logger?.LogDebug($"Telemetry message: {messageString}");
var encodedText = Encoding.Unicode.GetBytes(messageString); var encodedText = Encoding.Unicode.GetBytes(messageString);
await _fileStream.WriteAsync(encodedText, (int) _fileStream.Length, await _fileStream.WriteAsync(encodedText, _charactersWritten,
encodedText.Length, cancellationToken); encodedText.Length, cancellationToken);
_charactersWritten += encodedText.Length;
await _fileStream.FlushAsync(cancellationToken);
} }
public override void Dispose() public override void Dispose()
{ {
_fileStream.Close(); _fileStream.Close();
} }
private static string NormalizeFilename(string filename, string extension)
{
var date = DateTime.UtcNow;
return $"{filename}-{date.Year}-{date.Month}-{date.Day}_{date.ToFileTimeUtc()}.{extension}";
}
} }
} }

View file

@ -3,7 +3,7 @@
"Publisher": "Disk", "Publisher": "Disk",
"ServiceEnabled": true, "ServiceEnabled": true,
"PublishInterval": 3000, "PublishInterval": 3000,
"ConnectionString": "Filename=nucucar.telemetry.txt" "ConnectionString": "FileName=nucucar.telemetry"
}, },
"EnvironmentSensor": { "EnvironmentSensor": {
"Enabled": true, "Enabled": true,