Avoid race condition on changes. Ensure timers restart even on exceptions.

This commit is contained in:
Simon Pither 2010-07-03 20:37:43 +01:00
parent f73aff54bb
commit 6e76fadd36

View file

@ -35,6 +35,7 @@ namespace SparkleShare {
private FileSystemWatcher Watcher; private FileSystemWatcher Watcher;
private bool HasChanged = false; private bool HasChanged = false;
private DateTime LastChange; private DateTime LastChange;
private System.Object ChangeLock = new System.Object();
public string Name; public string Name;
public string Domain; public string Domain;
@ -126,14 +127,16 @@ namespace SparkleShare {
BufferTimer.Elapsed += delegate (object o, ElapsedEventArgs args) { BufferTimer.Elapsed += delegate (object o, ElapsedEventArgs args) {
SparkleHelpers.DebugInfo ("Buffer", "[" + Name + "] Checking for changes."); SparkleHelpers.DebugInfo ("Buffer", "[" + Name + "] Checking for changes.");
if (HasChanged) { lock(ChangeLock) {
SparkleHelpers.DebugInfo ("Buffer", "[" + Name + "] Changes found, checking if settled."); if (HasChanged) {
DateTime now = DateTime.UtcNow; SparkleHelpers.DebugInfo ("Buffer", "[" + Name + "] Changes found, checking if settled.");
TimeSpan changed = new TimeSpan (now.Ticks - LastChange.Ticks); DateTime now = DateTime.UtcNow;
if (changed.TotalMilliseconds > 5000) { TimeSpan changed = new TimeSpan (now.Ticks - LastChange.Ticks);
HasChanged = false; if (changed.TotalMilliseconds > 5000) {
SparkleHelpers.DebugInfo ("Buffer", "[" + Name + "] Changes have settled, adding."); HasChanged = false;
AddCommitAndPush (); SparkleHelpers.DebugInfo ("Buffer", "[" + Name + "] Changes have settled, adding.");
AddCommitAndPush ();
}
} }
} }
}; };
@ -156,8 +159,10 @@ namespace SparkleShare {
if (!ShouldIgnore (args.Name)) { if (!ShouldIgnore (args.Name)) {
SparkleHelpers.DebugInfo ("Event", "[" + Name + "] " + wct.ToString () + " '" + args.Name + "'"); SparkleHelpers.DebugInfo ("Event", "[" + Name + "] " + wct.ToString () + " '" + args.Name + "'");
FetchTimer.Stop (); FetchTimer.Stop ();
LastChange = DateTime.UtcNow; lock(ChangeLock) {
HasChanged = true; LastChange = DateTime.UtcNow;
HasChanged = true;
}
} }
} }
@ -165,22 +170,23 @@ namespace SparkleShare {
// so this method does them all with appropriate timers, etc switched off // so this method does them all with appropriate timers, etc switched off
public void AddCommitAndPush () public void AddCommitAndPush ()
{ {
BufferTimer.Stop (); try {
FetchTimer.Stop (); BufferTimer.Stop ();
FetchTimer.Stop ();
Add (); Add ();
string Message = FormatCommitMessage (); string Message = FormatCommitMessage ();
if (!Message.Equals ("")) { if (!Message.Equals ("")) {
Commit (Message); Commit (Message);
Fetch (); Fetch ();
Push (); Push ();
SparkleHelpers.CheckForUnicorns (Message);
}
}
finally {
FetchTimer.Start ();
BufferTimer.Start ();
} }
FetchTimer.Start ();
BufferTimer.Start ();
SparkleHelpers.CheckForUnicorns (Message);
} }
// Stages the made changes // Stages the made changes
@ -207,18 +213,22 @@ namespace SparkleShare {
// Fetches changes from the remote repo // Fetches changes from the remote repo
public void Fetch () public void Fetch ()
{ {
FetchTimer.Stop (); try {
// SparkleUI.NotificationIcon.SetSyncingState (); FetchTimer.Stop ();
SparkleHelpers.DebugInfo ("Git", "[" + Name + "] Fetching changes..."); // SparkleUI.NotificationIcon.SetSyncingState ();
Process.StartInfo.Arguments = "fetch -v"; SparkleHelpers.DebugInfo ("Git", "[" + Name + "] Fetching changes...");
Process.Start (); Process.StartInfo.Arguments = "fetch -v";
string Output = Process.StandardOutput.ReadToEnd ().Trim (); // TODO: This doesn't work :( Process.Start ();
Process.WaitForExit (); string Output = Process.StandardOutput.ReadToEnd ().Trim (); // TODO: This doesn't work :(
SparkleHelpers.DebugInfo ("Git", "[" + Name + "] Changes fetched."); Process.WaitForExit ();
if (!Output.Contains ("up to date")) SparkleHelpers.DebugInfo ("Git", "[" + Name + "] Changes fetched.");
Rebase (); if (!Output.Contains ("up to date"))
// SparkleUI.NotificationIcon.SetIdleState (); Rebase ();
FetchTimer.Start (); // SparkleUI.NotificationIcon.SetIdleState ();
}
finally {
FetchTimer.Start ();
}
} }
// Merges the fetched changes // Merges the fetched changes