Skip to content

Commit b2d8990

Browse files
Merge pull request #114 from virajdere/patch/use-buffer-path-for-durable-relay
Improve MQTT Relay Module Buffer Path Handling
2 parents 6a19ab2 + b953a9d commit b2d8990

File tree

1 file changed

+18
-8
lines changed
  • agent/Modules/MTConnect.NET-AgentModule-MqttRelay

1 file changed

+18
-8
lines changed

agent/Modules/MTConnect.NET-AgentModule-MqttRelay/Module.cs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ public class Module : MTConnectAgentModule
3838
private static readonly object _lastSentSequenceLock = new object();
3939
private long _totalIncomingObservations = 0;
4040
private long _lastSentSequence = 0;
41-
41+
private const string DirectoryBuffer = "buffer";
42+
private const string LastSentSequenceFileName = "mqttrelay_last_sent.seq";
4243

4344
public Module(IMTConnectAgentBroker mtconnectAgent, object configuration) : base(mtconnectAgent)
4445
{
@@ -334,17 +335,25 @@ private async Task RelayBufferedObservations()
334335
}
335336
}
336337

337-
private static string GetLastSentSequenceFilePath()
338+
private static string GetLastSentSequenceFilePath(IAgentApplicationConfiguration agentConfig = null)
338339
{
339-
var bufferDir = Path.Combine(AppContext.BaseDirectory, "buffer");
340-
Directory.CreateDirectory(bufferDir);
341-
return Path.Combine(bufferDir, "mqttrelay_last_sent.seq");
340+
string baseDir = !string.IsNullOrEmpty(agentConfig?.DurableBufferPath)
341+
? agentConfig.DurableBufferPath
342+
: DirectoryBuffer;
343+
344+
if (!Path.IsPathRooted(baseDir))
345+
{
346+
baseDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, baseDir);
347+
}
348+
Directory.CreateDirectory(baseDir);
349+
return Path.Combine(baseDir, LastSentSequenceFileName);
342350
}
343351

344352
private ulong GetLastSentSequence()
345353
{
346-
if (!_configuration.DurableRelay) return 0; // Default
347-
var path = GetLastSentSequenceFilePath();
354+
if (!_configuration.DurableRelay) return 0;
355+
var agentConfig = Agent?.Configuration as IAgentApplicationConfiguration;
356+
var path = GetLastSentSequenceFilePath(agentConfig);
348357
lock (_lastSentSequenceLock)
349358
{
350359
if (File.Exists(path))
@@ -359,7 +368,8 @@ private ulong GetLastSentSequence()
359368
private void SetLastSentSequence(ulong seq)
360369
{
361370
if (!_configuration.DurableRelay) return; // Default
362-
var path = GetLastSentSequenceFilePath();
371+
var agentConfig = Agent?.Configuration as IAgentApplicationConfiguration;
372+
var path = GetLastSentSequenceFilePath(agentConfig);
363373
lock (_lastSentSequenceLock)
364374
{
365375
File.WriteAllText(path, seq.ToString());

0 commit comments

Comments
 (0)