Skip to content

Commit e51dd5a

Browse files
committed
Added auto restart support and fix minor installation bug
Added option to restart syncthing automatically in case of shutdown or crash with limits on the number of crash permitted before to stop the service Fix an installation crash that would occur if the syncthing priority was not set
1 parent 0f84275 commit e51dd5a

10 files changed

+265
-49
lines changed

Debug/SyncThingTray.exe

89 KB
Binary file not shown.

Debug/SyncThingTray.exe.config

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0"?>
2+
<configuration>
3+
<startup>
4+
5+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1"/></startup>
6+
</configuration>

Debug/SyncThingTray.pdb

91.5 KB
Binary file not shown.
181 KB
Binary file not shown.

Program.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,9 @@ public static void RunAsAdministrator(params string[] Params)
253253
public static string SyncExePath { get; private set; }
254254
public static string SyncConfigPath { get; private set; }
255255
public static ProcessPriorityClass SyncPriority { get; private set; }
256+
public static bool AutoRestart { get; private set; }
257+
public static int AutoRestartCount { get; private set; }
258+
public static TimeSpan AutoRestartPeriod { get; private set; }
256259

257260
public static bool IsConfigured
258261
{
@@ -285,6 +288,34 @@ public static bool IsConfigured
285288
case "Idle": SyncPriority = ProcessPriorityClass.Idle; break;
286289
default: SyncPriority = ProcessPriorityClass.Normal; break;
287290
}
291+
string[] autos = (ksync.GetValue("Autorestart", "") as string).Split(new char[] { ':' }, 2);
292+
if ((autos.Length < 2) || (autos[0] != "Yes"))
293+
AutoRestart = false;
294+
else if (autos[1] == "unlimited")
295+
{
296+
AutoRestart = true;
297+
AutoRestartCount = 0;
298+
AutoRestartPeriod = TimeSpan.Zero;
299+
}
300+
else
301+
{
302+
string[] opts = autos[1].Split(',');
303+
int t; TimeSpan d;
304+
if (!int.TryParse(opts[0], out t))
305+
t = 0;
306+
if ((opts.Length<2)|| !TimeSpan.TryParse(opts[1], out d))
307+
d = TimeSpan.Zero;
308+
if ((t == 0) || (d == TimeSpan.Zero))
309+
{
310+
AutoRestart = false;
311+
}
312+
else
313+
{
314+
AutoRestart = true;
315+
AutoRestartCount = t;
316+
AutoRestartPeriod = d;
317+
}
318+
}
288319
return !string.IsNullOrEmpty(SyncExePath) && !string.IsNullOrEmpty(SyncConfigPath);
289320
}
290321
}

Releases/SyncThingTray.exe

4.5 KB
Binary file not shown.

SyncThingTray.csproj.user

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|AnyCPU'">
4-
<StartArguments>
5-
</StartArguments>
4+
<StartArguments>configure</StartArguments>
65
</PropertyGroup>
76
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|AnyCPU'">
87
<StartArguments>uninstall</StartArguments>

SyngThingService.cs

Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ namespace SyncThingTray
1717
public partial class SyngThingService : ServiceBase
1818
{
1919
Process syncthing;
20+
List<DateTime> shutdowns;
2021
public SyngThingService()
2122
{
2223
InitializeComponent();
@@ -28,25 +29,58 @@ protected override void OnStart(string[] args)
2829
{
2930
if (Program.IsConfigured)
3031
{
31-
ProcessStartInfo pinfo = new ProcessStartInfo(Program.SyncExePath, "-home=\"" + Program.SyncConfigPath + "\"");
32-
pinfo.WindowStyle = ProcessWindowStyle.Hidden;
33-
pinfo.RedirectStandardOutput = true;
34-
pinfo.RedirectStandardInput = true;
35-
pinfo.UseShellExecute = false;
36-
syncthing = new Process();
37-
syncthing.EnableRaisingEvents = true;
38-
syncthing.OutputDataReceived += syncthing_OutputDataReceived;
39-
syncthing.Exited += syncthing_Exited;
40-
syncthing.StartInfo = pinfo;
41-
syncthing.Start();
42-
syncthing.BeginOutputReadLine();
43-
syncthing.PriorityClass = Program.SyncPriority;
32+
shutdowns = null;
33+
StartSyncThing();
4434
}
4535
}
4636

37+
void StartSyncThing()
38+
{
39+
ProcessStartInfo pinfo = new ProcessStartInfo(Program.SyncExePath, "-home=\"" + Program.SyncConfigPath + "\"");
40+
pinfo.WindowStyle = ProcessWindowStyle.Hidden;
41+
pinfo.RedirectStandardOutput = true;
42+
pinfo.RedirectStandardInput = true;
43+
pinfo.UseShellExecute = false;
44+
syncthing = new Process();
45+
syncthing.EnableRaisingEvents = true;
46+
syncthing.OutputDataReceived += syncthing_OutputDataReceived;
47+
syncthing.Exited += syncthing_Exited;
48+
syncthing.StartInfo = pinfo;
49+
syncthing.Start();
50+
syncthing.BeginOutputReadLine();
51+
syncthing.PriorityClass = Program.SyncPriority;
52+
}
53+
4754
void syncthing_Exited(object sender, EventArgs e)
4855
{
49-
Stop();
56+
string reason;
57+
if (Program.AutoRestart)
58+
{
59+
if (Program.AutoRestartCount > 0)
60+
{
61+
if (shutdowns == null) shutdowns = new List<DateTime>();
62+
shutdowns.Add(DateTime.Now);
63+
shutdowns.RemoveAll(d => d < (DateTime.Now - Program.AutoRestartPeriod));
64+
reason = shutdowns.Count <= Program.AutoRestartCount ? null : "The maximum number of shutdown has been reached";
65+
}
66+
else
67+
reason = null;
68+
}
69+
else
70+
reason = "The service is not configured to restart";
71+
if (reason == null)
72+
{
73+
EventLog.WriteEntry("Syncthing has stopped and is being restarted", EventLogEntryType.Warning);
74+
syncthing.OutputDataReceived -= syncthing_OutputDataReceived;
75+
syncthing.Exited -= syncthing_Exited;
76+
syncthing.Dispose();
77+
StartSyncThing();
78+
}
79+
else
80+
{
81+
EventLog.WriteEntry("Syncthing has stopped and will not be restarted:\n" + reason, EventLogEntryType.Warning);
82+
Stop();
83+
}
5084
}
5185

5286
void syncthing_OutputDataReceived(object sender, DataReceivedEventArgs e)
@@ -59,6 +93,8 @@ protected override void OnStop()
5993
if (!syncthing.HasExited)
6094
{
6195
syncthing.CancelOutputRead();
96+
syncthing.OutputDataReceived -= syncthing_OutputDataReceived;
97+
syncthing.Exited -= syncthing_Exited;
6298

6399
var res = ShutdownProgram();
64100
if (res == null)

frmInstall.Designer.cs

Lines changed: 107 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)