Use PeriodicRunner for configwatcher
MODIFY
WalletWasabi.Backend/Global.cs
WalletWasabi.Backend/Global.cs
@@ -69,8 +69,10 @@ namespace WalletWasabi.Backend
if (roundConfig.FilePath != null)
{
- RoundConfigWatcher = new ConfigWatcher(RoundConfig);
- RoundConfigWatcher.Start(TimeSpan.FromSeconds(10), async () =>
+ RoundConfigWatcher = new ConfigWatcher(
+ TimeSpan.FromSeconds(10), // Every 10 seconds check the config
+ RoundConfig,
+ async () =>
{
try
{
@@ -82,7 +84,8 @@ namespace WalletWasabi.Backend
{
Logger.LogDebug(ex);
}
- }); // Every 10 seconds check the config
+ });
+ RoundConfigWatcher.Start();
Logger.LogInfo($"{nameof(RoundConfigWatcher)} is successfully started.");
}
}

MODIFY
WalletWasabi/Services/ConfigWatcher.cs
WalletWasabi/Services/ConfigWatcher.cs
using System;
using System.Threading;
using System.Threading.Tasks;
+using WalletWasabi.Bases;
using WalletWasabi.Helpers;
using WalletWasabi.Interfaces;
using WalletWasabi.Logging;
namespace WalletWasabi.Services
{
- public class ConfigWatcher
+ public class ConfigWatcher : PeriodicRunner
{
- /// <summary>
- /// 0: Not started, 1: Running, 2: Stopping, 3: Stopped
- /// </summary>
- private long _running;
-
- public bool IsRunning => Interlocked.Read(ref _running) == 1;
-
- private CancellationTokenSource Stop { get; set; }
-
public IConfig Config { get; }
+ public Func<Task> ExecuteWhenChangedAsync { get; }
- public ConfigWatcher(IConfig config)
+ public ConfigWatcher(TimeSpan period, IConfig config, Func<Task> executeWhenChangedAsync) : base(period, null)
{
Config = Guard.NotNull(nameof(config), config);
+ ExecuteWhenChangedAsync = Guard.NotNull(nameof(executeWhenChangedAsync), executeWhenChangedAsync);
config.AssertFilePathSet();
-
- _running = 0;
- Stop = new CancellationTokenSource();
}
- public void Start(TimeSpan period, Func<Task> executeWhenChangedAsync)
- {
- if (Interlocked.CompareExchange(ref _running, 1, 0) != 0)
- {
- return;
- }
-
- Task.Run(async () =>
- {
- try
- {
- while (IsRunning)
- {
- try
- {
- await Task.Delay(period, Stop.Token);
-
- if (await Config.CheckFileChangeAsync())
+ public override async Task<object> ActionAsync(CancellationToken cancel)
{
- await Config.LoadOrCreateDefaultFileAsync();
-
- await executeWhenChangedAsync?.Invoke();
- }
- }
- catch (TaskCanceledException ex)
+ if (await Config.CheckFileChangeAsync().ConfigureAwait(false))
{
- Logger.LogTrace(ex);
- }
- catch (Exception ex)
- {
- Logger.LogDebug(ex);
- }
- }
- }
- finally
- {
- Interlocked.CompareExchange(ref _running, 3, 2); // If IsStopping, make it stopped.
- }
- });
- }
+ cancel.ThrowIfCancellationRequested();
+ await Config.LoadOrCreateDefaultFileAsync().ConfigureAwait(false);
- public async Task StopAsync()
- {
- Interlocked.CompareExchange(ref _running, 2, 1); // If running, make it stopping.
- Stop?.Cancel();
- while (Interlocked.CompareExchange(ref _running, 3, 0) == 2)
- {
- await Task.Delay(50);
+ await ExecuteWhenChangedAsync?.Invoke();
}
- Stop?.Dispose();
- Stop = null;
+ return null;
}
}
}

Use PeriodicRunner for configwatcher