update mono restart
This commit is contained in:
parent
d9c4775015
commit
609d1f19cc
|
@ -17,126 +17,127 @@ using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace MediaBrowser.Server.Mono
|
namespace MediaBrowser.Server.Mono
|
||||||
{
|
{
|
||||||
public class MainClass
|
public class MainClass
|
||||||
{
|
{
|
||||||
private static ApplicationHost _appHost;
|
private static ApplicationHost _appHost;
|
||||||
|
|
||||||
private static ILogger _logger;
|
private static ILogger _logger;
|
||||||
|
|
||||||
public static void Main (string[] args)
|
public static void Main(string[] args)
|
||||||
{
|
{
|
||||||
var applicationPath = Assembly.GetEntryAssembly().Location;
|
var applicationPath = Assembly.GetEntryAssembly().Location;
|
||||||
|
|
||||||
var options = new StartupOptions();
|
|
||||||
|
|
||||||
// Allow this to be specified on the command line.
|
var options = new StartupOptions();
|
||||||
var customProgramDataPath = options.GetOption("-programdata");
|
|
||||||
|
|
||||||
var appPaths = CreateApplicationPaths(applicationPath, customProgramDataPath);
|
// Allow this to be specified on the command line.
|
||||||
|
var customProgramDataPath = options.GetOption("-programdata");
|
||||||
|
|
||||||
var logManager = new NlogManager(appPaths.LogDirectoryPath, "server");
|
var appPaths = CreateApplicationPaths(applicationPath, customProgramDataPath);
|
||||||
logManager.ReloadLogger(LogSeverity.Info);
|
|
||||||
logManager.AddConsoleOutput();
|
|
||||||
|
|
||||||
var logger = _logger = logManager.GetLogger("Main");
|
var logManager = new NlogManager(appPaths.LogDirectoryPath, "server");
|
||||||
|
logManager.ReloadLogger(LogSeverity.Info);
|
||||||
|
logManager.AddConsoleOutput();
|
||||||
|
|
||||||
|
var logger = _logger = logManager.GetLogger("Main");
|
||||||
|
|
||||||
ApplicationHost.LogEnvironmentInfo(logger, appPaths, true);
|
ApplicationHost.LogEnvironmentInfo(logger, appPaths, true);
|
||||||
|
|
||||||
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
|
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
RunApplication(appPaths, logManager, options);
|
RunApplication(appPaths, logManager, options);
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
logger.Info("Shutting down");
|
logger.Info("Shutting down");
|
||||||
|
|
||||||
_appHost.Dispose();
|
_appHost.Dispose();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static ServerApplicationPaths CreateApplicationPaths(string applicationPath, string programDataPath)
|
private static ServerApplicationPaths CreateApplicationPaths(string applicationPath, string programDataPath)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(programDataPath))
|
if (string.IsNullOrEmpty(programDataPath))
|
||||||
{
|
{
|
||||||
programDataPath = ApplicationPathHelper.GetProgramDataPath(applicationPath);
|
programDataPath = ApplicationPathHelper.GetProgramDataPath(applicationPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
return new ServerApplicationPaths(programDataPath, applicationPath, Path.GetDirectoryName(applicationPath));
|
|
||||||
}
|
|
||||||
|
|
||||||
private static readonly TaskCompletionSource<bool> ApplicationTaskCompletionSource = new TaskCompletionSource<bool>();
|
return new ServerApplicationPaths(programDataPath, applicationPath, Path.GetDirectoryName(applicationPath));
|
||||||
|
}
|
||||||
|
|
||||||
private static void RunApplication(ServerApplicationPaths appPaths, ILogManager logManager, StartupOptions options)
|
private static readonly TaskCompletionSource<bool> ApplicationTaskCompletionSource = new TaskCompletionSource<bool>();
|
||||||
{
|
|
||||||
SystemEvents.SessionEnding += SystemEvents_SessionEnding;
|
|
||||||
|
|
||||||
// Allow all https requests
|
private static void RunApplication(ServerApplicationPaths appPaths, ILogManager logManager, StartupOptions options)
|
||||||
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(delegate { return true; });
|
{
|
||||||
|
SystemEvents.SessionEnding += SystemEvents_SessionEnding;
|
||||||
|
|
||||||
var fileSystem = new CommonFileSystem(logManager.GetLogger("FileSystem"), false, true);
|
// Allow all https requests
|
||||||
|
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(delegate { return true; });
|
||||||
|
|
||||||
var nativeApp = new NativeApp();
|
var fileSystem = new CommonFileSystem(logManager.GetLogger("FileSystem"), false, true);
|
||||||
|
|
||||||
|
var nativeApp = new NativeApp();
|
||||||
|
|
||||||
_appHost = new ApplicationHost(appPaths, logManager, options, fileSystem, "MBServer.Mono", nativeApp);
|
_appHost = new ApplicationHost(appPaths, logManager, options, fileSystem, "MBServer.Mono", nativeApp);
|
||||||
|
|
||||||
if (options.ContainsOption("-v")) {
|
|
||||||
Console.WriteLine (_appHost.ApplicationVersion.ToString());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
Console.WriteLine ("appHost.Init");
|
if (options.ContainsOption("-v"))
|
||||||
|
{
|
||||||
|
Console.WriteLine(_appHost.ApplicationVersion.ToString());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
var initProgress = new Progress<double>();
|
Console.WriteLine("appHost.Init");
|
||||||
|
|
||||||
var task = _appHost.Init(initProgress);
|
var initProgress = new Progress<double>();
|
||||||
Task.WaitAll (task);
|
|
||||||
|
|
||||||
Console.WriteLine ("Running startup tasks");
|
var task = _appHost.Init(initProgress);
|
||||||
|
Task.WaitAll(task);
|
||||||
|
|
||||||
task = _appHost.RunStartupTasks();
|
Console.WriteLine("Running startup tasks");
|
||||||
Task.WaitAll (task);
|
|
||||||
|
|
||||||
task = ApplicationTaskCompletionSource.Task;
|
task = _appHost.RunStartupTasks();
|
||||||
|
Task.WaitAll(task);
|
||||||
|
|
||||||
Task.WaitAll (task);
|
task = ApplicationTaskCompletionSource.Task;
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
Task.WaitAll(task);
|
||||||
/// Handles the SessionEnding event of the SystemEvents control.
|
}
|
||||||
/// </summary>
|
|
||||||
/// <param name="sender">The source of the event.</param>
|
|
||||||
/// <param name="e">The <see cref="SessionEndingEventArgs"/> instance containing the event data.</param>
|
|
||||||
static void SystemEvents_SessionEnding(object sender, SessionEndingEventArgs e)
|
|
||||||
{
|
|
||||||
if (e.Reason == SessionEndReasons.SystemShutdown)
|
|
||||||
{
|
|
||||||
Shutdown();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Handles the UnhandledException event of the CurrentDomain control.
|
/// Handles the SessionEnding event of the SystemEvents control.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="sender">The source of the event.</param>
|
/// <param name="sender">The source of the event.</param>
|
||||||
/// <param name="e">The <see cref="UnhandledExceptionEventArgs"/> instance containing the event data.</param>
|
/// <param name="e">The <see cref="SessionEndingEventArgs"/> instance containing the event data.</param>
|
||||||
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
|
static void SystemEvents_SessionEnding(object sender, SessionEndingEventArgs e)
|
||||||
{
|
{
|
||||||
var exception = (Exception)e.ExceptionObject;
|
if (e.Reason == SessionEndReasons.SystemShutdown)
|
||||||
|
{
|
||||||
|
Shutdown();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Handles the UnhandledException event of the CurrentDomain control.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="sender">The source of the event.</param>
|
||||||
|
/// <param name="e">The <see cref="UnhandledExceptionEventArgs"/> instance containing the event data.</param>
|
||||||
|
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
|
||||||
|
{
|
||||||
|
var exception = (Exception)e.ExceptionObject;
|
||||||
|
|
||||||
new UnhandledExceptionWriter(_appHost.ServerConfigurationManager.ApplicationPaths, _logger, _appHost.LogManager).Log(exception);
|
new UnhandledExceptionWriter(_appHost.ServerConfigurationManager.ApplicationPaths, _logger, _appHost.LogManager).Log(exception);
|
||||||
|
|
||||||
if (!Debugger.IsAttached)
|
if (!Debugger.IsAttached)
|
||||||
{
|
{
|
||||||
Environment.Exit(System.Runtime.InteropServices.Marshal.GetHRForException(exception));
|
Environment.Exit(System.Runtime.InteropServices.Marshal.GetHRForException(exception));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void Shutdown()
|
public static void Shutdown()
|
||||||
{
|
{
|
||||||
ApplicationTaskCompletionSource.SetResult (true);
|
ApplicationTaskCompletionSource.SetResult(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void Restart()
|
public static void Restart()
|
||||||
{
|
{
|
||||||
|
@ -145,35 +146,38 @@ namespace MediaBrowser.Server.Mono
|
||||||
|
|
||||||
_logger.Info("Starting new instance");
|
_logger.Info("Starting new instance");
|
||||||
|
|
||||||
var currentProcess = Process.GetCurrentProcess();
|
|
||||||
|
|
||||||
var args = Environment.GetCommandLineArgs()
|
var args = Environment.GetCommandLineArgs()
|
||||||
.Select(NormalizeCommandLineArgument);
|
.Skip(1)
|
||||||
|
.Select(NormalizeCommandLineArgument);
|
||||||
|
|
||||||
var commandLineArgsString = string.Join(" ", args.ToArray());
|
var commandLineArgsString = string.Join(" ", args.ToArray());
|
||||||
|
var module = Environment.GetCommandLineArgs().First();
|
||||||
|
|
||||||
Process.Start(currentProcess.MainModule.FileName, commandLineArgsString);
|
_logger.Info("Executable: {0}", module);
|
||||||
|
_logger.Info("Arguments: {0}", commandLineArgsString);
|
||||||
|
|
||||||
|
Process.Start(module, commandLineArgsString);
|
||||||
|
|
||||||
_logger.Info("Calling Environment.Exit");
|
_logger.Info("Calling Environment.Exit");
|
||||||
Environment.Exit(0);
|
Environment.Exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string NormalizeCommandLineArgument(string arg)
|
private static string NormalizeCommandLineArgument(string arg)
|
||||||
{
|
{
|
||||||
if (arg.IndexOf(" ", StringComparison.OrdinalIgnoreCase) == -1)
|
if (arg.IndexOf(" ", StringComparison.OrdinalIgnoreCase) == -1)
|
||||||
{
|
{
|
||||||
return arg;
|
return arg;
|
||||||
}
|
}
|
||||||
|
|
||||||
return "\"" + arg + "\"";
|
return "\"" + arg + "\"";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class NoCheckCertificatePolicy : ICertificatePolicy
|
class NoCheckCertificatePolicy : ICertificatePolicy
|
||||||
{
|
{
|
||||||
public bool CheckValidationResult (ServicePoint srvPoint, X509Certificate certificate, WebRequest request, int certificateProblem)
|
public bool CheckValidationResult(ServicePoint srvPoint, X509Certificate certificate, WebRequest request, int certificateProblem)
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user