Updated DBSyncWatcher and Syncer
Added Locking system, so that we don't try to access the same file with both read and write functions at the same time causing an locked exception on the file when watching it.
This commit is contained in:
parent
8d53ce79f3
commit
ddf50a3052
2 changed files with 14 additions and 2 deletions
|
|
@ -2,6 +2,7 @@ using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
|
using System.Threading;
|
||||||
|
|
||||||
namespace FreeTubeSyncer.Library;
|
namespace FreeTubeSyncer.Library;
|
||||||
|
|
||||||
|
|
@ -13,6 +14,8 @@ public class DBSyncWatcher
|
||||||
public delegate void DatabaseChange(string db, string value);
|
public delegate void DatabaseChange(string db, string value);
|
||||||
public event DatabaseChange OnDatabaseChange;
|
public event DatabaseChange OnDatabaseChange;
|
||||||
|
|
||||||
|
public bool Locked = false;
|
||||||
|
|
||||||
public DBSyncWatcher(string path)
|
public DBSyncWatcher(string path)
|
||||||
{
|
{
|
||||||
_watcher = new FileSystemWatcher(path);
|
_watcher = new FileSystemWatcher(path);
|
||||||
|
|
@ -32,6 +35,11 @@ public class DBSyncWatcher
|
||||||
{
|
{
|
||||||
if (e.ChangeType != WatcherChangeTypes.Changed) return;
|
if (e.ChangeType != WatcherChangeTypes.Changed) return;
|
||||||
|
|
||||||
|
while (Locked)
|
||||||
|
{
|
||||||
|
Thread.Sleep(100);
|
||||||
|
}
|
||||||
|
|
||||||
var dbName = Path.GetFileName(e.FullPath);
|
var dbName = Path.GetFileName(e.FullPath);
|
||||||
|
|
||||||
if (!WatchFiles.Keys.Contains(dbName)) return;
|
if (!WatchFiles.Keys.Contains(dbName)) return;
|
||||||
|
|
|
||||||
|
|
@ -28,14 +28,16 @@ public class Syncer<T> : ISyncer where T : class, IDataModel, new()
|
||||||
private string _dbPath;
|
private string _dbPath;
|
||||||
private string _dbName;
|
private string _dbName;
|
||||||
private string _restEndpoint;
|
private string _restEndpoint;
|
||||||
|
private DBSyncWatcher _watcher;
|
||||||
|
|
||||||
private bool _isDirty = false;
|
private bool _isDirty = false;
|
||||||
private bool _syncing = false;
|
private bool _syncing = false;
|
||||||
|
|
||||||
public Syncer(DBSyncWatcher watcher, string dbPath, string dbName, string restEndpoint)
|
public Syncer(DBSyncWatcher watcher, string dbPath, string dbName, string restEndpoint)
|
||||||
{
|
{
|
||||||
watcher.WatchFiles[dbName] = typeof(T);
|
_watcher = watcher;
|
||||||
watcher.OnDatabaseChange += HandleDatabaseChange;
|
_watcher.WatchFiles[dbName] = typeof(T);
|
||||||
|
_watcher.OnDatabaseChange += HandleDatabaseChange;
|
||||||
_client = new RestClient(new RestClientOptions("http://192.168.1.30:5050"));
|
_client = new RestClient(new RestClientOptions("http://192.168.1.30:5050"));
|
||||||
_dbPath = dbPath;
|
_dbPath = dbPath;
|
||||||
_dbName = dbName;
|
_dbName = dbName;
|
||||||
|
|
@ -138,6 +140,7 @@ public class Syncer<T> : ISyncer where T : class, IDataModel, new()
|
||||||
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());
|
||||||
|
_watcher.Locked = true;
|
||||||
using (var fh = File.OpenWrite(_dbPath))
|
using (var fh = File.OpenWrite(_dbPath))
|
||||||
{
|
{
|
||||||
foreach (var line in json)
|
foreach (var line in json)
|
||||||
|
|
@ -145,6 +148,7 @@ public class Syncer<T> : ISyncer where T : class, IDataModel, new()
|
||||||
fh.Flush();
|
fh.Flush();
|
||||||
fh.Close();
|
fh.Close();
|
||||||
}
|
}
|
||||||
|
_watcher.Locked = false;
|
||||||
Console.WriteLine($"Updated {_dbPath}.");
|
Console.WriteLine($"Updated {_dbPath}.");
|
||||||
_isDirty = false;
|
_isDirty = false;
|
||||||
_syncing = false;
|
_syncing = false;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue