Updated Syncer

Added Logging functionality to write to log files.
This commit is contained in:
Mario Steele 2025-07-31 16:14:21 -05:00
parent 725a37d9cf
commit c94b709145

View file

@ -10,6 +10,8 @@ using System.Threading.Tasks;
using FreeTubeSyncer.Library; using FreeTubeSyncer.Library;
using FreeTubeSyncer.Models.DatabaseModels; using FreeTubeSyncer.Models.DatabaseModels;
using RestSharp; using RestSharp;
using Serilog;
using Serilog.Core;
namespace FreeTubeSyncer.REST; namespace FreeTubeSyncer.REST;
@ -68,34 +70,29 @@ public class Syncer<T> : ISyncer where T : class, IDataModel, new()
public async Task<bool> PingApi() public async Task<bool> PingApi()
{ {
// TODO: Replace with Logger Log.Information("Pinging API at {BuildUri}...", _client.BuildUri(new RestRequest("/ping")));
Console.WriteLine($"Pinging API at {_client.BuildUri(new RestRequest("/ping"))}...");
try try
{ {
var res = await _client.GetAsync<Ping>(new RestRequest("/ping")); var res = await _client.GetAsync<Ping>(new RestRequest("/ping"));
if (res == null) if (res == null)
{ {
// TODO: Replace with Logger Log.Information("Ping returned null, not the server we are looking for!");
Console.WriteLine($"Ping returned null, not the server we are looking for!");
return false; return false;
} }
if (res.AppVersion == "0.1.3") if (res.AppVersion == "0.1.3")
{ {
// TODO: Replace with Logger Log.Information("Server Online! {AppVersion}", res.AppVersion);
Console.WriteLine($"Server Online! {res.AppVersion}");
return true; return true;
} }
} }
catch (Exception ex) catch (Exception ex)
{ {
// TODO: Replace with Logger Log.Error("API not alive. Network Error: {Message}", ex.Message);
Console.WriteLine($"Network Error: {ex.Message}, API not alive.");
return false; return false;
} }
// TODO: Replace with Logger Log.Error("Responded with something other then Ping. This is not the server we are looking for...");
Console.WriteLine("Responded with something other then 404, API not what we expected.");
return false; return false;
} }
@ -128,8 +125,7 @@ public class Syncer<T> : ISyncer where T : class, IDataModel, new()
if (_entries.Any(x => x.EqualId(item.Id()))) if (_entries.Any(x => x.EqualId(item.Id())))
_entries.RemoveAll(x => x.EqualId(item.Id())); _entries.RemoveAll(x => x.EqualId(item.Id()));
_entries.Add(item); _entries.Add(item);
// TODO: Replace with Logger Log.Information("Posting to REST: {ItemId}", item.Id());
Console.WriteLine($"Posting to REST: {item.Id()}");
await _client.PostJsonAsync<T>(_restEndpoint, item); await _client.PostJsonAsync<T>(_restEndpoint, item);
} }
} }
@ -147,15 +143,11 @@ public class Syncer<T> : ISyncer where T : class, IDataModel, new()
if (data.Equals(entry)) continue; if (data.Equals(entry)) continue;
// TODO: Replace with Logger Log.Information("Updated Entry from REST for {DbName} - {EntryId}", _dbName, entry.Id());
Console.WriteLine($"Updated Entry from REST for {_dbName} - {entry.Id()}");
_entries.RemoveAll(x => x.EqualId(entry.Id())); _entries.RemoveAll(x => x.EqualId(entry.Id()));
} }
else else
{ Log.Information("New Entry from REST for {DbName} - {EntryId}", _dbName, entry.Id());
// TODO: Replace with Logger
Console.WriteLine($"New Entry from REST for {_dbName} - {entry.Id()}");
}
_entries.Add(entry); _entries.Add(entry);
_isDirty = true; _isDirty = true;
@ -186,8 +178,8 @@ public class Syncer<T> : ISyncer where T : class, IDataModel, new()
} }
catch (Exception iex) catch (Exception iex)
{ {
// TODO: Replace with Logger Log.Error("Failed to parse line: {EntryLine}", entryObject);
Console.WriteLine($"Failed to parse line: {entryObject}\nMessage: {iex.Message}"); Log.Error("Error Message: {Messsage}", iex.Message);
entry = null; entry = null;
} }
} }
@ -199,15 +191,11 @@ public class Syncer<T> : ISyncer where T : class, IDataModel, new()
{ {
var data = _entries.First(x => x.EqualId(entry.Id())); var data = _entries.First(x => x.EqualId(entry.Id()));
if (data.Equals(entry)) return; if (data.Equals(entry)) return;
// TODO: Replace with Logger Log.Information("Updated File Entry {EntryId} updated for {DbName}", entry.Id(), _dbName);
Console.WriteLine($"File Entry {entry.Id()} updated for {_dbName}");
_entries.RemoveAll(x => x.EqualId(entry.Id())); _entries.RemoveAll(x => x.EqualId(entry.Id()));
} }
else else
{ Log.Information("New File Entry {EntryId} for {DbName}", entry.Id(), _dbName);
// TODO: Replace with Logger
Console.WriteLine($"New File Entry {entry.Id()} for {_dbName}");
}
_entries.Add(entry); _entries.Add(entry);
await _client.PostJsonAsync<T>(_restEndpoint, entry); await _client.PostJsonAsync<T>(_restEndpoint, entry);
@ -219,8 +207,8 @@ public class Syncer<T> : ISyncer where T : class, IDataModel, new()
if (!_isDirty) if (!_isDirty)
return; return;
_syncing = true; _syncing = true;
// TODO: Replace with Logger Log.Information("Syncing {DbName}...", _dbPath);
Console.WriteLine($"Syncing {_dbPath}..."); var start = DateTime.Now;
var json = new List<string>(); var json = new List<string>();
foreach (var entry in _entries) foreach (var entry in _entries)
json.Add(entry.JsonData()); json.Add(entry.JsonData());
@ -233,8 +221,7 @@ public class Syncer<T> : ISyncer where T : class, IDataModel, new()
fh.Close(); fh.Close();
} }
_watcher.Locked = false; _watcher.Locked = false;
// TODO: Replace with Logger Log.Information("Updated {DbName}, completed in {TimeSpan}", _dbName, DateTime.Now - start);
Console.WriteLine($"Updated {_dbPath}.");
_isDirty = false; _isDirty = false;
_syncing = false; _syncing = false;
} }