Updated App
Added Last Update stamp, for when the last update was in the database, to see if we have newer events to process. Applied settings for enabling/disabling syncers on startup. Updated SyncMonitor() to use new Update check, before iterating through all the syncers and fetching them. Added logging messages for when we are updating settings to reflect what has been changed.
This commit is contained in:
parent
a88da7f6d0
commit
823bb11b83
1 changed files with 67 additions and 8 deletions
|
|
@ -32,6 +32,7 @@ public partial class App : Application
|
|||
private string? _oldSettings;
|
||||
private static readonly SemaphoreSlim _semaphoreSlim = new SemaphoreSlim(1, 1);
|
||||
private TimeSpan _checkInterval;
|
||||
private DateTime _lastUpdated;
|
||||
|
||||
public event EventHandler? SettingsChanged;
|
||||
|
||||
|
|
@ -67,10 +68,15 @@ public partial class App : Application
|
|||
|
||||
_watcher = new DBSyncWatcher(path);
|
||||
_historySyncer = new Syncer<History>(_watcher, Path.Join(path, "history.db"), "history.db", _settings.RestBaseUrl, "/history");
|
||||
_historySyncer.SetEnabled(_settings.SyncHistory);
|
||||
_playlistSyncer = new Syncer<Playlist>(_watcher, Path.Join(path, "playlists.db"), "playlists.db", _settings.RestBaseUrl, "/playlist");
|
||||
_playlistSyncer.SetEnabled(_settings.SyncPlaylist);
|
||||
_profileSyncer = new Syncer<Profile>(_watcher, Path.Join(path, "profiles.db"), "profiles.db", _settings.RestBaseUrl, "/profile");
|
||||
_profileSyncer.SetEnabled(_settings.SyncProfile);
|
||||
_searchHistorySyncer = new Syncer<SearchHistory>(_watcher, Path.Join(path, "search-history.db"), "search-history.db", _settings.RestBaseUrl, "/searchHistory");
|
||||
_searchHistorySyncer.SetEnabled(_settings.SyncSearchHistory);
|
||||
_settingSyncer = new Syncer<Setting>(_watcher, Path.Join(path, "settings.db"), "settings.db", _settings.RestBaseUrl, "/settings");
|
||||
_settingSyncer.SetEnabled(_settings.SyncSettings);
|
||||
_syncers =
|
||||
[
|
||||
_historySyncer,
|
||||
|
|
@ -155,6 +161,8 @@ public partial class App : Application
|
|||
await syncer.FetchDatabase();
|
||||
}
|
||||
|
||||
_lastUpdated = await _syncers[0].GetLastUpdated();
|
||||
|
||||
_semaphoreSlim.Release();
|
||||
break;
|
||||
}
|
||||
|
|
@ -173,8 +181,15 @@ public partial class App : Application
|
|||
var start = DateTime.Now;
|
||||
// TODO: Replace with Logger
|
||||
Console.WriteLine("Checking for updates...");
|
||||
foreach (var syncer in _syncers)
|
||||
await syncer.FetchDatabase();
|
||||
var updateCheck = await _syncers[0].GetLastUpdated();
|
||||
if (_lastUpdated < updateCheck)
|
||||
{
|
||||
// TODO: Replace with Logger
|
||||
Console.WriteLine($"Update Found, fetching updates...");
|
||||
_lastUpdated = updateCheck;
|
||||
foreach (var syncer in _syncers)
|
||||
await syncer.FetchDatabase();
|
||||
}
|
||||
lastCheck = DateTime.Now;
|
||||
var end = DateTime.Now - start;
|
||||
_semaphoreSlim.Release();
|
||||
|
|
@ -207,20 +222,64 @@ public partial class App : Application
|
|||
|
||||
private async void HandleSettingsChanged(object? sender, EventArgs e)
|
||||
{
|
||||
// TODO: Replace with Logger
|
||||
Console.WriteLine("Settings have changed. Updating Settings...");
|
||||
await _semaphoreSlim.WaitAsync();
|
||||
var old = JsonSerializer.Deserialize<AppSettings>(_oldSettings!);
|
||||
if (_settings!.RestBaseUrl != old!.RestBaseUrl)
|
||||
{
|
||||
// TODO: Replace with Logger
|
||||
Console.WriteLine($"Updating syncers with new URL: {_settings.RestBaseUrl}.");
|
||||
foreach (var syncer in _syncers!)
|
||||
syncer.UpdateBaseUrl(_settings.RestBaseUrl);
|
||||
}
|
||||
|
||||
if (old.CheckInterval != _settings.CheckInterval) _checkInterval = TimeSpan.FromSeconds(_settings.CheckInterval);
|
||||
if (old.SyncHistory != _settings.SyncHistory) _historySyncer!.SetEnabled(_settings.SyncHistory);
|
||||
if (old.SyncPlaylist != _settings.SyncPlaylist) _playlistSyncer!.SetEnabled(_settings.SyncPlaylist);
|
||||
if (old.SyncProfile != _settings.SyncProfile) _profileSyncer!.SetEnabled(_settings.SyncProfile);
|
||||
if (old.SyncSearchHistory != _settings.SyncSearchHistory) _searchHistorySyncer!.SetEnabled(_settings.SyncSearchHistory);
|
||||
if (old.SyncSettings != _settings.SyncSettings) _settingSyncer!.SetEnabled(_settings.SyncSettings);
|
||||
if (old.CheckInterval != _settings.CheckInterval)
|
||||
{
|
||||
// TODO: Replace with Logger
|
||||
Console.WriteLine($"Updating Check Interval to {_settings.CheckInterval}.");
|
||||
_checkInterval = TimeSpan.FromSeconds(_settings.CheckInterval);
|
||||
}
|
||||
|
||||
if (old.SyncHistory != _settings.SyncHistory)
|
||||
{
|
||||
// TODO: Replace with Logger
|
||||
Console.WriteLine("History Syncer: " + (_settings.SyncHistory ? "Enabled" : "Disabled"));
|
||||
_historySyncer!.SetEnabled(_settings.SyncHistory);
|
||||
await _historySyncer.FetchDatabase();
|
||||
}
|
||||
|
||||
if (old.SyncPlaylist != _settings.SyncPlaylist)
|
||||
{
|
||||
// TODO: Replace with Logger
|
||||
Console.WriteLine("Playlist Syncer: " + (_settings.SyncHistory ? "Enabled" : "Disabled"));
|
||||
_playlistSyncer!.SetEnabled(_settings.SyncPlaylist);
|
||||
await _playlistSyncer.FetchDatabase();
|
||||
}
|
||||
|
||||
if (old.SyncProfile != _settings.SyncProfile)
|
||||
{
|
||||
// TODO: Replace with Logger
|
||||
Console.WriteLine("Profile Syncer: " + (_settings.SyncHistory ? "Enabled" : "Disabled"));
|
||||
_profileSyncer!.SetEnabled(_settings.SyncProfile);
|
||||
await _profileSyncer.FetchDatabase();
|
||||
}
|
||||
|
||||
if (old.SyncSearchHistory != _settings.SyncSearchHistory)
|
||||
{
|
||||
// TODO: Replace with Logger
|
||||
Console.WriteLine("Search History Syncer: " + (_settings.SyncHistory ? "Enabled" : "Disabled"));
|
||||
_searchHistorySyncer!.SetEnabled(_settings.SyncSearchHistory);
|
||||
await _searchHistorySyncer.FetchDatabase();
|
||||
}
|
||||
|
||||
if (old.SyncSettings != _settings.SyncSettings)
|
||||
{
|
||||
// TODO: Replace with Logger
|
||||
Console.WriteLine("Settings Syncer: " + (_settings.SyncHistory ? "Enabled" : "Disabled"));
|
||||
_settingSyncer!.SetEnabled(_settings.SyncSettings);
|
||||
await _settingSyncer.FetchDatabase();
|
||||
}
|
||||
_semaphoreSlim.Release();
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue