Jackett/src/Jackett.Server/Services/ServiceConfigService.cs

122 lines
3.6 KiB
C#
Raw Normal View History

2020-02-09 18:08:34 +00:00
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
2020-02-09 18:08:34 +00:00
using System.ServiceProcess;
using Jackett.Common.Services;
2020-02-09 18:08:34 +00:00
using Jackett.Common.Services.Interfaces;
using NLog;
2018-06-10 02:33:16 +00:00
namespace Jackett.Server.Services
{
public class ServiceConfigService : IServiceConfigService
{
private const string NAME = "Jackett";
2018-06-10 02:33:16 +00:00
private const string DESCRIPTION = "API Support for your favorite torrent trackers";
private const string SERVICEEXE = "JackettService.exe";
private readonly IProcessService processService;
private readonly Logger logger;
public ServiceConfigService()
{
logger = LogManager.GetCurrentClassLogger();
processService = new ProcessService(logger);
}
public bool ServiceExists()
{
return GetService(NAME) != null;
}
public bool ServiceRunning()
{
var service = GetService(NAME);
if (service == null)
return false;
return service.Status == ServiceControllerStatus.Running;
}
public void Start()
{
var service = GetService(NAME);
service.Start();
}
public void Stop()
{
var service = GetService(NAME);
service.Stop();
}
public ServiceController GetService(string serviceName)
{
return ServiceController.GetServices().FirstOrDefault(c => string.Equals(c.ServiceName, serviceName, StringComparison.InvariantCultureIgnoreCase));
}
public void Install()
{
if (ServiceExists())
{
logger.Warn("The service is already installed!");
}
else
{
var applicationFolder = Path.GetDirectoryName(new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath);
var exePath = Path.Combine(applicationFolder, SERVICEEXE);
if (!File.Exists(exePath) && Debugger.IsAttached)
{
exePath = Path.Combine(applicationFolder, "..\\..\\..\\Jackett.Service\\bin\\Debug", SERVICEEXE);
}
var arg = $"create {NAME} start= auto binpath= \"{exePath}\" DisplayName= {NAME}";
2018-06-10 02:33:16 +00:00
processService.StartProcessAndLog("sc.exe", arg, true);
2018-06-10 02:33:16 +00:00
processService.StartProcessAndLog("sc.exe", $"description {NAME} \"{DESCRIPTION}\"", true);
}
}
public void Uninstall()
{
RemoveService();
2018-06-10 02:33:16 +00:00
processService.StartProcessAndLog("sc.exe", $"delete {NAME}", true);
logger.Info("The service was uninstalled.");
}
public void RemoveService()
{
var service = GetService(NAME);
2020-02-09 02:35:16 +00:00
if (service == null)
{
logger.Warn("The service is already uninstalled");
return;
}
if (service.Status != ServiceControllerStatus.Stopped)
{
service.Stop();
service.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromSeconds(60));
service.Refresh();
if (service.Status == ServiceControllerStatus.Stopped)
2018-06-10 02:33:16 +00:00
{
logger.Info("Service stopped.");
2018-06-10 02:33:16 +00:00
}
else
2018-06-10 02:33:16 +00:00
{
logger.Error("Failed to stop the service");
2018-06-10 02:33:16 +00:00
}
}
else
2018-06-10 02:33:16 +00:00
{
logger.Warn("The service was already stopped");
2018-06-10 02:33:16 +00:00
}
}
}
}