Close tray app when updating

This commit is contained in:
WhatFox 2015-11-18 19:36:37 +00:00
parent 0b74f762a3
commit 44aa33a7e0
6 changed files with 102 additions and 9 deletions

View File

@ -42,6 +42,14 @@ namespace JackettTray
Engine.Logger.Info("Starting server from tray");
Engine.Server.Start();
}
Task.Factory.StartNew(WaitForEvent);
}
private void WaitForEvent()
{
Engine.LockService.WaitForSignal();
Application.Exit();
}
void toolStripMenuItemWebUI_Click(object sender, EventArgs e)

View File

@ -22,8 +22,6 @@ namespace Jackett.Updater
private void Run(string[] args)
{
Engine.Logger.Info("Jackett Updater v" + GetCurrentVersion());
Engine.Logger.Info("Waiting for Jackett to close..");
Thread.Sleep(2000);
try {
var options = new UpdaterConsoleOptions();
@ -60,7 +58,7 @@ namespace Jackett.Updater
var isWindows = System.Environment.OSVersion.Platform != PlatformID.Unix;
var trayRunning = false;
var trayProcesses = Process.GetProcessesByName("JackettTray.exe");
var trayProcesses = Process.GetProcessesByName("JackettTray");
if (isWindows)
{
if (trayProcesses.Count() > 0)
@ -78,6 +76,9 @@ namespace Jackett.Updater
}
}
Engine.Logger.Info("Waiting for Jackett to close..");
Thread.Sleep(2000);
var files = Directory.GetFiles(updateLocation, "*.*", SearchOption.AllDirectories);
foreach(var file in files)
{
@ -89,10 +90,15 @@ namespace Jackett.Updater
{
continue;
}
Engine.Logger.Info("Copying " + fileName);
var dest = Path.Combine(options.Path, file.Substring(updateLocation.Length));
File.Copy(file, dest, true);
try {
Engine.Logger.Info("Copying " + fileName);
var dest = Path.Combine(options.Path, file.Substring(updateLocation.Length));
File.Copy(file, dest, true);
}
catch(Exception e)
{
Engine.Logger.Error(e);
}
}
if (trayRunning)

View File

@ -74,6 +74,14 @@ namespace Jackett
}
}
public static ITrayLockService LockService
{
get
{
return container.Resolve<ITrayLockService>();
}
}
public static IServerService Server
{
get

View File

@ -271,6 +271,7 @@
<Compile Include="Services\CacheService.cs" />
<Compile Include="Services\LogCacheService.cs" />
<Compile Include="Services\ProtectionService.cs" />
<Compile Include="Services\TrayLockService.cs" />
<Compile Include="Services\UpdateService.cs" />
<Compile Include="Utils\Clients\BaseWebResult.cs" />
<Compile Include="Utils\Clients\UnixLibCurlWebClient.cs" />

View File

@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Jackett.Services
{
public interface ITrayLockService
{
void WaitForSignal();
void Signal();
}
public class TrayLockService : ITrayLockService
{
private readonly string EVENT_HANDLE_NAME = "JACKETT.TRAY";
private EventWaitHandle GetEventHandle()
{
return new EventWaitHandle(false, EventResetMode.ManualReset, EVENT_HANDLE_NAME);
}
public void WaitForSignal()
{
if (System.Environment.OSVersion.Platform != PlatformID.Unix)
{
GetEventHandle().Reset();
GetEventHandle().WaitOne();
}
}
public void Signal()
{
if (System.Environment.OSVersion.Platform != PlatformID.Unix)
{
GetEventHandle().Set();
}
}
}
}

View File

@ -10,7 +10,9 @@ using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net.Security;
using System.Reflection;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
@ -29,12 +31,15 @@ namespace Jackett.Services
IWebClient client;
IConfigurationService configService;
ManualResetEvent locker = new ManualResetEvent(false);
ITrayLockService lockService;
bool forceupdatecheck = false;
public UpdateService(Logger l, IWebClient c, IConfigurationService cfg)
public UpdateService(Logger l, IWebClient c, IConfigurationService cfg, ITrayLockService ls)
{
logger = l;
client = c;
configService = cfg;
lockService = ls;
}
private string ExePath()
@ -50,6 +55,7 @@ namespace Jackett.Services
public void CheckForUpdatesNow()
{
forceupdatecheck = true;
locker.Set();
}
@ -63,15 +69,22 @@ namespace Jackett.Services
}
}
private bool AcceptCert(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
return true;
}
private async Task CheckForUpdates()
{
var config = configService.GetConfig<ServerConfig>();
if (config.UpdateDisabled)
if (config.UpdateDisabled && !forceupdatecheck)
{
logger.Info($"Skipping update check as it is disabled.");
return;
}
forceupdatecheck = true;
var isWindows = System.Environment.OSVersion.Platform != PlatformID.Unix;
if (Debugger.IsAttached)
{
@ -81,7 +94,14 @@ namespace Jackett.Services
try
{
if (!isWindows)
{
// Linux distros don't seem to like these certs.. todo local cert verification?
System.Net.ServicePointManager.ServerCertificateValidationCallback += AcceptCert;
}
var github = new GitHubClient(new ProductHeaderValue("Jackett"));
if(config!=null && !string.IsNullOrWhiteSpace(config.GitHubToken))
{
github.Credentials = new Credentials(config.GitHubToken);
@ -122,6 +142,13 @@ namespace Jackett.Services
{
logger.Error(e, "Error checking for updates.");
}
finally
{
if (!isWindows)
{
System.Net.ServicePointManager.ServerCertificateValidationCallback -= AcceptCert;
}
}
}
private string GetCurrentVersion()
@ -224,6 +251,7 @@ namespace Jackett.Services
var procInfo = Process.Start(startInfo);
logger.Info($"Updater started process id: {procInfo.Id}");
logger.Info("Exiting Jackett..");
lockService.Signal();
Environment.Exit(0);
}
}