1
0
Fork 0
mirror of https://github.com/Radarr/Radarr synced 2024-12-29 03:15:19 +00:00
Radarr/NzbDrone.Update/Providers/UpdateProvider.cs

127 lines
4.6 KiB
C#
Raw Normal View History

2011-10-23 18:31:17 +00:00
using System;
using System.IO;
using NLog;
using NzbDrone.Common;
2013-05-10 23:53:50 +00:00
using IServiceProvider = NzbDrone.Common.IServiceProvider;
namespace NzbDrone.Update.Providers
{
public class UpdateProvider
{
2013-05-10 23:53:50 +00:00
private readonly IDiskProvider _diskProvider;
private readonly IServiceProvider _serviceProvider;
private readonly IProcessProvider _processProvider;
private readonly IEnvironmentProvider _environmentProvider;
2011-11-13 04:16:54 +00:00
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
2013-05-10 23:53:50 +00:00
public UpdateProvider(IDiskProvider diskProvider, IServiceProvider serviceProvider,
IProcessProvider processProvider, IEnvironmentProvider environmentProvider)
{
_diskProvider = diskProvider;
_serviceProvider = serviceProvider;
2011-10-23 18:31:17 +00:00
_processProvider = processProvider;
_environmentProvider = environmentProvider;
}
2011-11-13 20:31:02 +00:00
public UpdateProvider()
{
}
private void Verify(string targetFolder)
{
2011-11-13 04:16:54 +00:00
logger.Info("Verifying requirements before update...");
2011-10-23 18:31:17 +00:00
if (String.IsNullOrWhiteSpace(targetFolder))
2011-10-23 18:31:17 +00:00
throw new ArgumentException("Target folder can not be null or empty");
if (!_diskProvider.FolderExists(targetFolder))
throw new DirectoryNotFoundException("Target folder doesn't exist " + targetFolder);
2011-11-13 04:16:54 +00:00
logger.Info("Verifying Update Folder");
if (!_diskProvider.FolderExists(_environmentProvider.GetUpdatePackageFolder()))
throw new DirectoryNotFoundException("Update folder doesn't exist " + _environmentProvider.GetUpdatePackageFolder());
2011-10-23 18:31:17 +00:00
}
2011-11-13 20:31:02 +00:00
public virtual void Start(string targetFolder)
2011-10-23 18:31:17 +00:00
{
2011-11-13 04:07:06 +00:00
Verify(targetFolder);
AppType appType = AppType.Normal;
2011-11-13 04:07:06 +00:00
2011-11-13 04:16:54 +00:00
logger.Info("Stopping all running services");
if (_serviceProvider.ServiceExist(ServiceProvider.NZBDRONE_SERVICE_NAME)
&& _serviceProvider.IsServiceRunning(ServiceProvider.NZBDRONE_SERVICE_NAME))
{
appType = AppType.Service;
_serviceProvider.Stop(ServiceProvider.NZBDRONE_SERVICE_NAME);
}
else
{
appType = AppType.Normal;
}
//TODO:Should be able to restart service if anything beyond this point fails
2011-11-13 04:16:54 +00:00
logger.Info("Killing all running processes");
2011-10-23 18:31:17 +00:00
2013-05-07 05:38:40 +00:00
_processProvider.KillAll(ProcessProvider.NzbDroneConsoleProcessName);
_processProvider.KillAll(ProcessProvider.NzbDroneProcessName);
2011-11-13 04:16:54 +00:00
logger.Info("Creating backup of existing installation");
_diskProvider.CopyDirectory(targetFolder, _environmentProvider.GetUpdateBackUpFolder());
2011-11-13 04:07:06 +00:00
logger.Info("Moving update package to target");
2011-11-13 04:07:06 +00:00
try
{
_diskProvider.CopyDirectory(_environmentProvider.GetUpdatePackageFolder(), targetFolder);
logger.Trace("Deleting Update Package.");
_diskProvider.DeleteFolder(_environmentProvider.GetUpdatePackageFolder(), true);
2011-11-13 04:07:06 +00:00
}
catch (Exception e)
{
RollBack(targetFolder);
foreach (var key in e.Data.Keys)
{
logger.Trace("Key: {0}, Value: {1}", key, e.Data[key]);
}
logger.FatalException("Failed to copy upgrade package to target folder.", e);
2011-11-13 04:07:06 +00:00
}
finally
{
StartNzbDrone(appType, targetFolder);
2011-11-13 04:07:06 +00:00
}
}
2011-11-13 04:07:06 +00:00
private void RollBack(string targetFolder)
{
//TODO:this should ignore single file failures.
2011-11-13 04:16:54 +00:00
logger.Info("Attempting to rollback upgrade");
_diskProvider.CopyDirectory(_environmentProvider.GetUpdateBackUpFolder(), targetFolder);
2011-11-13 04:07:06 +00:00
}
private void StartNzbDrone(AppType appType, string targetFolder)
2011-11-13 04:07:06 +00:00
{
2013-01-14 21:14:32 +00:00
logger.Info("Starting NzbDrone");
if (appType == AppType.Service)
2011-11-13 04:07:06 +00:00
{
2013-01-14 21:14:32 +00:00
logger.Info("Starting NzbDrone service");
2011-11-13 04:07:06 +00:00
_serviceProvider.Start(ServiceProvider.NZBDRONE_SERVICE_NAME);
}
2013-04-13 23:57:10 +00:00
else if (appType == AppType.Console)
{
2013-01-14 21:14:32 +00:00
logger.Info("Starting NzbDrone with Console");
_processProvider.Start(Path.Combine(targetFolder, "NzbDrone.Console.exe"));
}
2011-11-13 04:07:06 +00:00
else
{
2013-01-14 21:14:32 +00:00
logger.Info("Starting NzbDrone without Console");
_processProvider.Start(Path.Combine(targetFolder, "NzbDrone.exe"));
2011-11-13 04:07:06 +00:00
}
}
}
}