Windows Tray/Serice update improvements

This commit is contained in:
flightlevel
2018-06-26 19:44:12 +10:00
parent 28bbeec462
commit 060972475f
7 changed files with 100 additions and 17 deletions

View File

@@ -270,15 +270,27 @@ namespace Jackett.Common.Services
private void StartUpdate(string updaterExePath, string installLocation, bool isWindows, bool NoRestart, bool trayWasRunning) private void StartUpdate(string updaterExePath, string installLocation, bool isWindows, bool NoRestart, bool trayWasRunning)
{ {
string appType = "Console";
//DI once off Owin
IProcessService processService = new ProcessService(logger);
IServiceConfigService windowsService = new WindowsServiceConfigService(processService, logger);
if (isWindows && windowsService.ServiceExists())
{
appType = "WindowsService";
}
var exe = Path.GetFileName(ExePath()); var exe = Path.GetFileName(ExePath());
var args = string.Join(" ", Environment.GetCommandLineArgs().Skip(1).Select(a => a.Contains(" ") ? "\"" +a + "\"" : a )).Replace("\"", "\\\""); var args = string.Join(" ", Environment.GetCommandLineArgs().Skip(1).Select(a => a.Contains(" ") ? "\"" +a + "\"" : a )).Replace("\"", "\\\"");
var startInfo = new ProcessStartInfo(); var startInfo = new ProcessStartInfo();
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
// Note: add a leading space to the --Args argument to avoid parsing as arguments // Note: add a leading space to the --Args argument to avoid parsing as arguments
if (isWindows) if (isWindows)
{ {
startInfo.Arguments = $"--Path \"{installLocation}\" --Type \"{exe}\" --Args \" {args}\""; startInfo.Arguments = $"--Path \"{installLocation}\" --Type \"{appType}\" --Args \" {args}\"";
startInfo.FileName = Path.Combine(updaterExePath); startInfo.FileName = Path.Combine(updaterExePath);
} }
else else
@@ -287,13 +299,12 @@ namespace Jackett.Common.Services
args = exe + " " + args; args = exe + " " + args;
exe = "mono"; exe = "mono";
startInfo.Arguments = $"{Path.Combine(updaterExePath)} --Path \"{installLocation}\" --Type \"{exe}\" --Args \" {args}\""; startInfo.Arguments = $"{Path.Combine(updaterExePath)} --Path \"{installLocation}\" --Type \"{appType}\" --Args \" {args}\"";
startInfo.FileName = "mono"; startInfo.FileName = "mono";
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
} }
try { try
{
var pid = Process.GetCurrentProcess().Id; var pid = Process.GetCurrentProcess().Id;
startInfo.Arguments += $" --KillPids \"{pid}\""; startInfo.Arguments += $" --KillPids \"{pid}\"";
} }
@@ -317,7 +328,7 @@ namespace Jackett.Common.Services
var procInfo = Process.Start(startInfo); var procInfo = Process.Start(startInfo);
logger.Info($"Updater started process id: {procInfo.Id}"); logger.Info($"Updater started process id: {procInfo.Id}");
if (NoRestart == false) if (NoRestart == false)
{ {
logger.Info("Exiting Jackett.."); logger.Info("Exiting Jackett..");
lockService.Signal(); lockService.Signal();
//TODO: Remove once off Owin //TODO: Remove once off Owin

View File

@@ -77,10 +77,12 @@ namespace Jackett.Service
private void ProcessExited(object sender, EventArgs e) private void ProcessExited(object sender, EventArgs e)
{ {
logger.Info("Console process exited");
if (!serviceStopInitiated) if (!serviceStopInitiated)
{ {
logger.Info("Service stop not responsible for process exit"); logger.Info("Service stop not responsible for process exit");
OnStop(); Stop();
} }
} }
@@ -89,7 +91,7 @@ namespace Jackett.Service
if (consoleProcess != null && !consoleProcess.HasExited) if (consoleProcess != null && !consoleProcess.HasExited)
{ {
consoleProcess.StandardInput.Close(); consoleProcess.StandardInput.Close();
System.Threading.Thread.Sleep(1000); consoleProcess.WaitForExit(2000);
if (consoleProcess != null && !consoleProcess.HasExited) if (consoleProcess != null && !consoleProcess.HasExited)
{ {
consoleProcess.Kill(); consoleProcess.Kill();

View File

@@ -66,6 +66,7 @@
</Compile> </Compile>
<Compile Include="Program.cs" /> <Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TrayConsoleOptions.cs" />
<EmbeddedResource Include="Main.resx"> <EmbeddedResource Include="Main.resx">
<DependentUpon>Main.cs</DependentUpon> <DependentUpon>Main.cs</DependentUpon>
</EmbeddedResource> </EmbeddedResource>
@@ -109,6 +110,11 @@
<EmbedInteropTypes>True</EmbedInteropTypes> <EmbedInteropTypes>True</EmbedInteropTypes>
</COMReference> </COMReference>
</ItemGroup> </ItemGroup>
<ItemGroup>
<PackageReference Include="CommandLineParser">
<Version>2.2.1</Version>
</PackageReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets. Other similar extension points exist, see Microsoft.Common.targets.

View File

@@ -25,7 +25,7 @@ namespace Jackett.Tray
private Logger logger; private Logger logger;
private bool closeApplicationInitiated; private bool closeApplicationInitiated;
public Main() public Main(string updatedVersion)
{ {
Hide(); Hide();
InitializeComponent(); InitializeComponent();
@@ -65,12 +65,22 @@ namespace Jackett.Tray
StartConsoleApplication(); StartConsoleApplication();
} }
if (!string.IsNullOrWhiteSpace(updatedVersion))
{
notifyIcon1.BalloonTipTitle = "Jackett";
notifyIcon1.BalloonTipText = $"Jackett has updated to version {updatedVersion}";
notifyIcon1.BalloonTipIcon = ToolTipIcon.Info;
notifyIcon1.ShowBalloonTip(10000);
logger.Info($"Display balloon tip, updated to {updatedVersion}");
}
Task.Factory.StartNew(WaitForEvent); Task.Factory.StartNew(WaitForEvent);
} }
private void WaitForEvent() private void WaitForEvent()
{ {
trayLockService.WaitForSignal(); trayLockService.WaitForSignal();
logger.Info("Received signal from tray lock service");
CloseTrayApplication(); CloseTrayApplication();
} }

View File

@@ -1,4 +1,5 @@
using System; using CommandLine;
using System;
using System.Diagnostics; using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Windows.Forms; using System.Windows.Forms;
@@ -11,7 +12,7 @@ namespace Jackett.Tray
/// The main entry point for the application. /// The main entry point for the application.
/// </summary> /// </summary>
[STAThread] [STAThread]
static void Main() static void Main(string[] args)
{ {
var JacketTrayProcess = Process.GetCurrentProcess(); var JacketTrayProcess = Process.GetCurrentProcess();
var runningProcesses = Process.GetProcesses(); var runningProcesses = Process.GetProcesses();
@@ -23,10 +24,29 @@ namespace Jackett.Tray
MessageBox.Show("JackettTray is already running"); MessageBox.Show("JackettTray is already running");
} }
else else
{ {
string newVersion = "";
var commandLineParser = new Parser(settings => settings.CaseSensitive = false);
try
{
var optionsResult = commandLineParser.ParseArguments<TrayConsoleOptions>(args);
optionsResult.WithParsed(options =>
{
if (!string.IsNullOrWhiteSpace(options.UpdatedVersion))
{
newVersion = options.UpdatedVersion;
}
});
}
catch (Exception e)
{
newVersion = "";
}
Application.EnableVisualStyles(); Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false); Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Main()); Application.Run(new Main(newVersion));
} }
} }
} }

View File

@@ -0,0 +1,10 @@
using CommandLine;
namespace Jackett.Tray
{
public class TrayConsoleOptions
{
[Option("UpdatedVersion", HelpText = "Indicates the new version that Jackett just updated to so that user understands why they are getting a prompt to start Windows service")]
public string UpdatedVersion { get; set; }
}
}

View File

@@ -38,6 +38,14 @@ namespace Jackett.Updater
logger.Info("Jackett Updater v" + GetCurrentVersion()); logger.Info("Jackett Updater v" + GetCurrentVersion());
logger.Info("Options \"" + string.Join("\" \"", args) + "\""); logger.Info("Options \"" + string.Join("\" \"", args) + "\"");
bool isWindows = Environment.OSVersion.Platform == PlatformID.Win32NT;
if (isWindows)
{
//The updater starts before Jackett closes
logger.Info("Pausing for 3 seconds to give Jackett & tray time to shutdown");
System.Threading.Tasks.Task.Delay(3000);
}
processService = new ProcessService(logger); processService = new ProcessService(logger);
windowsService = new WindowsServiceConfigService(processService, logger); windowsService = new WindowsServiceConfigService(processService, logger);
@@ -248,15 +256,16 @@ namespace Jackett.Updater
if (options.NoRestart == false) if (options.NoRestart == false)
{ {
if (trayRunning || options.StartTray) if (isWindows && (trayRunning || options.StartTray))
{ {
var startInfo = new ProcessStartInfo() var startInfo = new ProcessStartInfo()
{ {
Arguments = options.Args, Arguments = $"--UpdatedVersion \" {EnvironmentUtil.JackettVersion}\"",
FileName = Path.Combine(options.Path, "JackettTray.exe"), FileName = Path.Combine(options.Path, "JackettTray.exe"),
UseShellExecute = true UseShellExecute = true
}; };
logger.Info("Starting Tray: " + startInfo.FileName + " " + startInfo.Arguments);
Process.Start(startInfo); Process.Start(startInfo);
if (!windowsService.ServiceExists()) if (!windowsService.ServiceExists())
@@ -266,12 +275,27 @@ namespace Jackett.Updater
} }
} }
if (string.Equals(options.Type, "JackettService.exe", StringComparison.InvariantCultureIgnoreCase)) if (string.Equals(options.Type, "WindowsService", StringComparison.InvariantCultureIgnoreCase))
{ {
if (windowsService.ServiceExists()) logger.Info("Starting Windows service");
if (ServerUtil.IsUserAdministrator())
{ {
windowsService.Start(); windowsService.Start();
} }
else
{
try
{
var consolePath = Path.Combine(options.Path, "JackettConsole.exe");
processService.StartProcessAndLog(consolePath, "--Start", true);
}
catch
{
logger.Error("Failed to get admin rights to start the service.");
}
}
} }
else else
{ {