Merge pull request #143 from oleg-nenashev/config-error-handling

Improve handling of initialization errors
pull/145/head^2
Oleg Nenashev 2016-12-09 01:19:55 +01:00 committed by GitHub
commit 5041de60a1
2 changed files with 33 additions and 7 deletions

View File

@ -513,11 +513,13 @@ namespace winsw
} }
catch (WmiException e) catch (WmiException e)
{ {
Log.Fatal("WMI Operation failure: " + e.ErrorCode, e);
Console.Error.WriteLine(e); Console.Error.WriteLine(e);
return (int)e.ErrorCode; return (int)e.ErrorCode;
} }
catch (Exception e) catch (Exception e)
{ {
Log.Fatal("Unhandled exception", e);
Console.Error.WriteLine(e); Console.Error.WriteLine(e);
return -1; return -1;
} }
@ -528,17 +530,29 @@ namespace winsw
throw new WmiException(ReturnValue.NoSuchService); throw new WmiException(ReturnValue.NoSuchService);
} }
// ReSharper disable once InconsistentNaming // ReSharper disable once InconsistentNaming
/// <summary>
/// Runs the wrapper.
/// </summary>
/// <param name="_args">Arguments. If empty, WinSW will behave in the service mode. Otherwise - CLI mode</param>
/// <param name="descriptor">Service descriptor. If null, it will be initialized within the method.
/// In such case configs will be loaded from the XML Configuration File.</param>
/// <exception cref="Exception">Any unhandled exception</exception>
public static void Run(string[] _args, ServiceDescriptor descriptor = null) public static void Run(string[] _args, ServiceDescriptor descriptor = null)
{ {
bool isCLIMode = _args.Length > 0; bool isCLIMode = _args.Length > 0;
// If descriptor is not specified, initialize the new one (and load configs from there)
var d = descriptor ?? new ServiceDescriptor(); var d = descriptor ?? new ServiceDescriptor();
// Configure the wrapper-internal logging // Configure the wrapper-internal logging.
// STDIN and STDOUT of the child process will be handled independently // STDIN and STDOUT of the child process will be handled independently.
InitLoggers(d, isCLIMode); InitLoggers(d, isCLIMode);
if (isCLIMode) // CLI mode
if (isCLIMode) // CLI mode, in-service mode otherwise
{ {
Log.Debug("Starting ServiceWrapper in CLI mode"); Log.Debug("Starting ServiceWrapper in CLI mode");

View File

@ -86,6 +86,11 @@ namespace winsw.Extensions
//TODO: Implement loading of external extensions. Current version supports internal hack //TODO: Implement loading of external extensions. Current version supports internal hack
#region Extension load management #region Extension load management
/// <summary>
/// Loads extensions according to the configuration file.
/// </summary>
/// <param name="logger">Logger</param>
/// <exception cref="Exception">Loading failure</exception>
public void LoadExtensions(IEventWriter logger) public void LoadExtensions(IEventWriter logger)
{ {
var extensionIds = ServiceDescriptor.ExtensionIds; var extensionIds = ServiceDescriptor.ExtensionIds;
@ -100,7 +105,7 @@ namespace winsw.Extensions
/// </summary> /// </summary>
/// <param name="id">Extension ID</param> /// <param name="id">Extension ID</param>
/// <param name="logger">Logger</param> /// <param name="logger">Logger</param>
/// <exception cref="ExtensionException">Loading failure</exception> /// <exception cref="Exception">Loading failure</exception>
private void LoadExtension(string id, IEventWriter logger) private void LoadExtension(string id, IEventWriter logger)
{ {
if (Extensions.ContainsKey(id)) if (Extensions.ContainsKey(id))
@ -120,8 +125,15 @@ namespace winsw.Extensions
{ {
IWinSWExtension extension = CreateExtensionInstance(descriptor.Id, descriptor.ClassName); IWinSWExtension extension = CreateExtensionInstance(descriptor.Id, descriptor.ClassName);
extension.Descriptor = descriptor; extension.Descriptor = descriptor;
//TODO: Handle exceptions try
{
extension.Configure(ServiceDescriptor, configNode, logger); extension.Configure(ServiceDescriptor, configNode, logger);
}
catch (Exception ex)
{ // Consider any unexpected exception as fatal
Log.Fatal("Failed to configure the extension " + id, ex);
throw ex;
}
Extensions.Add(id, extension); Extensions.Add(id, extension);
logger.LogEvent("Extension loaded: "+id, EventLogEntryType.Information); logger.LogEvent("Extension loaded: "+id, EventLogEntryType.Information);
} }