private void ThreadRun() { _running = true; Thread.Sleep(5 * 1000); // 5 secs initial delay for (;;) { lock (_syncobj) { _isSyncing = true; { _comChannel.Reset(); DoSync(false); } _isSyncing = false; } var tick = Environment.TickCount; do { if (_cancel) { _running = false; _log.Info("Sync", "Thread cancelled"); return; } if (_prioritysync) { _prioritysync = false; _log.Info("Sync", "Thread ffwd (priority sync)"); break; } _comChannel.WaitOne(10 * 1000); // 10 sec } while (Environment.TickCount - tick < _delay); } }
public void Init() { var sw = Stopwatch.StartNew(); if (!Directory.Exists(_pathLocalFolder)) { _logger.Info("Repository", "Create local note folder: " + _pathLocalFolder); Directory.CreateDirectory(_pathLocalFolder); } LoadNotesFromLocal(); _thread.Start(_appconfig.GetSyncDelay()); _rawFilesystemRepo.Start(); _logger.Trace("Repository", $"SyncThread init took {sw.ElapsedMilliseconds}ms"); }
public static RepoLock Lock(AlephLogger logger, string pathLocalFolder) { var filename = Path.Combine(pathLocalFolder, ".lock"); var content = $"[[AlephNote::RepoLock::Lockfile]]\n" + $"{{\n" + $" ProcessID := {Process.GetCurrentProcess().Id}\n" + $" ProcessHandle := {Process.GetCurrentProcess().Handle.ToInt64()}\n" + $" StartTime := {Process.GetCurrentProcess().StartTime:u}\n" + $" FileName := {Path.GetFileName(Process.GetCurrentProcess().MainModule?.FileName)}\n" + $" FilePath := {Process.GetCurrentProcess().MainModule?.FileName}\n" + $" ModuleName := {Process.GetCurrentProcess().MainModule?.ModuleName}\n" + $" ProcessName := {Process.GetCurrentProcess().ProcessName}\n" + $"}}\n"; try { if (File.Exists(filename)) { try { var oldcontent = File.ReadAllText(filename); logger.Warn("RepoLock", "Old Lockfile found", $"File := {filename}\n\nContent:\n{oldcontent}"); } catch (Exception) { logger.Warn("RepoLock", "Old Lockfile found (but could not read)", $"File := {filename}"); } } logger.Debug("RepoLock", "Trying to acquire lock", $"File := {filename}\n\nContent:\n{content}"); var fs = File.Open(filename, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read); var bytes = Encoding.UTF8.GetBytes(content); fs.SetLength(0); fs.Write(bytes, 0, bytes.Length); fs.Flush(); logger.Debug("RepoLock", "Lock acquired successfully", $"File := {filename}\n\nContent:\n{content}"); return(new RepoLock(logger, filename, fs)); } catch (Exception e1) { logger.Error("RepoLock", "Acquiring lock failed", $"File := {filename}\n\nException:\n{e1}"); try { var oldcontent = File.ReadAllText(filename); logger.Info("RepoLock", "Old lock file read", $"File := Content:\n{oldcontent}"); } catch (Exception e2) { logger.Error("RepoLock", "Cannot read existing lockfile", e2.ToString()); } throw new RepoLockedException($"Could not open Repository '{pathLocalFolder}'.\nThe repository is currently locked by a different process.", e1); } }
private void ThreadRun() { running = true; for (; ;) { lock (_syncobj) { isSyncing = true; { _comChannel.Reset(); DoSync(); } isSyncing = false; } var tick = Environment.TickCount; do { if (cancel) { running = false; _log.Info("Sync", "Thread cancelled"); return; } if (prioritysync) { prioritysync = false; _log.Info("Sync", "Thread ffwd (priority sync)"); break; } _comChannel.WaitOne(10 * 1000); // 10 sec } while (Environment.TickCount - tick < delay); } }