瀏覽代碼

Re-apply config when Syncthing restarted for any reason

Fixes #31
Antony Male 10 年之前
父節點
當前提交
a9d4abcc03
共有 2 個文件被更改,包括 25 次插入9 次删除
  1. 13 9
      src/SyncTrayzor/SyncThing/SyncThingManager.cs
  2. 12 0
      src/SyncTrayzor/SyncThing/SyncThingProcessRunner.cs

+ 13 - 9
src/SyncTrayzor/SyncThing/SyncThingManager.cs

@@ -154,6 +154,19 @@ namespace SyncTrayzor.SyncThing
 
             this.processRunner.ProcessStopped += (o, e) => this.ProcessStopped(e.ExitStatus);
             this.processRunner.MessageLogged += (o, e) => this.OnMessageLogged(e.LogMessage);
+            this.processRunner.Starting += (o, e) =>
+            {
+                // This is fired on restarts, too, so we can update config
+                this.apiClient.SetConnectionDetails(this.Address, this.ApiKey);
+                this.processRunner.ApiKey = this.ApiKey;
+                this.processRunner.HostAddress = this.Address.ToString();
+                this.processRunner.ExecutablePath = this.ExecutablePath;
+                this.processRunner.CustomHomeDir = this.SyncthingCustomHomeDir;
+                this.processRunner.Traces = this.SyncthingTraceFacilities;
+                this.processRunner.DenyUpgrade = this.SyncthingDenyUpgrade;
+
+                this.SetState(SyncThingState.Starting);
+            };
 
             this.eventWatcher.StartupComplete += (o, e) => { var t = this.StartupCompleteAsync(); };
             this.eventWatcher.SyncStateChanged += (o, e) => this.OnFolderSyncStateChanged(e);
@@ -169,16 +182,7 @@ namespace SyncTrayzor.SyncThing
         {
             try
             {
-                this.apiClient.SetConnectionDetails(this.Address, this.ApiKey);
-                this.processRunner.ApiKey = this.ApiKey;
-                this.processRunner.HostAddress = this.Address.ToString();
-                this.processRunner.ExecutablePath = this.ExecutablePath;
-                this.processRunner.CustomHomeDir = this.SyncthingCustomHomeDir;
-                this.processRunner.Traces = this.SyncthingTraceFacilities;
-                this.processRunner.DenyUpgrade = this.SyncthingDenyUpgrade;
-
                 this.processRunner.Start();
-                this.SetState(SyncThingState.Starting);
             }
             catch (Exception e)
             {

+ 12 - 0
src/SyncTrayzor/SyncThing/SyncThingProcessRunner.cs

@@ -41,6 +41,7 @@ namespace SyncTrayzor.SyncThing
         string Traces { get; set; }
         bool DenyUpgrade { get; set; }
 
+        event EventHandler Starting;
         event EventHandler<MessageLoggedEventArgs> MessageLogged;
         event EventHandler<ProcessStoppedEventArgs> ProcessStopped;
 
@@ -64,6 +65,7 @@ namespace SyncTrayzor.SyncThing
         public string Traces { get; set; }
         public bool DenyUpgrade { get; set; }
 
+        public event EventHandler Starting;
         public event EventHandler<MessageLoggedEventArgs> MessageLogged;
         public event EventHandler<ProcessStoppedEventArgs> ProcessStopped;
 
@@ -75,6 +77,9 @@ namespace SyncTrayzor.SyncThing
         {
             logger.Info("Starting syncthing: {0}", this.ExecutablePath);
 
+            // This might cause our config to be set...
+            this.OnStarting();
+
             if (!File.Exists(this.ExecutablePath))
                 throw new Exception(String.Format("Unable to find Syncthing at path {0}", this.ExecutablePath));
 
@@ -187,6 +192,13 @@ namespace SyncTrayzor.SyncThing
             }
         }
 
+        private void OnStarting()
+        {
+            var handler = this.Starting;
+            if (handler != null)
+                handler(this, EventArgs.Empty);
+        }
+
         private void OnMessageLogged(string logMessage)
         {
             logger.Debug(logMessage);