/// <summary> /// Run the hosted runtime, blocking the calling thread. /// </summary> /// <returns>True if the worker stopped as planned (e.g. due to updated assemblies)</returns> public bool Run(Maybe<ICloudConfigurationSettings> externalRoleConfiguration) { _stoppedWaitHandle.Reset(); // IoC Setup var builder = new ContainerBuilder(); builder.RegisterModule(new CloudModule()); if (externalRoleConfiguration.HasValue) { builder.RegisterModule(new CloudConfigurationModule(externalRoleConfiguration.Value)); } else { builder.RegisterModule(new CloudConfigurationModule()); } builder.Register(typeof (Runtime)).FactoryScoped(); // Run using (var container = builder.Build()) { var log = container.Resolve<ILog>(); _runtime = null; try { _runtime = container.Resolve<Runtime>(); _runtime.RuntimeContainer = container; // runtime endlessly keeps pinging queues for pending work _runtime.Execute(); log.Log(LogLevel.Warn, "Runtime host stopped execution."); } catch (TypeLoadException typeLoadEx) { log.Log(LogLevel.Error, typeLoadEx, string.Format( "Type {0} could not be loaded (service: {1}).", typeLoadEx.TypeName, GetServiceInExecution(_runtime))); } catch (FileLoadException fileLoadEx) { // Tentatively: referenced assembly is missing log.Log(LogLevel.Error, fileLoadEx, string.Format( "Could not load assembly probably due to a missing reference assembly (service: {0}).", GetServiceInExecution(_runtime))); } catch (SecurityException securityEx) { // Tentatively: assembly cannot be loaded due to security config log.Log(LogLevel.Error, securityEx, string.Format( "Could not load assembly {0} probably due to security configuration (service: {1}).", securityEx.FailedAssemblyInfo, GetServiceInExecution(_runtime))); } catch (TriggerRestartException) { log.Log(LogLevel.Warn, "Runtime host was triggered to stop execution."); return true; } catch (ThreadInterruptedException) { log.Log(LogLevel.Warn, "Runtime host interrupted execution."); } catch (ThreadAbortException) { log.Log(LogLevel.Warn, "Runtime host aborted execution."); Thread.ResetAbort(); } catch (Exception ex) { // Generic exception log.Log(LogLevel.Error, ex, string.Format( "An unhandled exception occurred (service: {0}).", GetServiceInExecution(_runtime))); } finally { _stoppedWaitHandle.Set(); _runtime = null; } return false; } }
/// <summary> /// Run the hosted runtime, blocking the calling thread. /// </summary> /// <returns>True if the worker stopped as planned (e.g. due to updated assemblies)</returns> public bool Run(CloudConfigurationSettings settings) { _stoppedWaitHandle.Reset(); // Runtime IoC Setup var runtimeBuilder = new ContainerBuilder(); runtimeBuilder.RegisterModule(new CloudModule()); runtimeBuilder.RegisterInstance(settings); runtimeBuilder.RegisterType <Runtime>().InstancePerDependency(); // Run using (var runtimeContainer = runtimeBuilder.Build()) { var environment = runtimeContainer.Resolve <IEnvironment>(); var log = runtimeContainer.Resolve <ILog>(); AppDomain.CurrentDomain.UnhandledException += (sender, e) => log.TryErrorFormat( e.ExceptionObject as Exception, "Runtime Host: An unhandled {0} exception occurred on worker {1} in a background thread. The Runtime Host will be restarted: {2}.", e.ExceptionObject.GetType().Name, environment.Host.WorkerName, e.IsTerminating); _runtime = null; try { _runtime = runtimeContainer.Resolve <Runtime>(); _runtime.RuntimeContainer = runtimeContainer; // runtime endlessly keeps pinging queues for pending work _runtime.Execute(); log.TryDebugFormat("Runtime Host: Runtime has stopped cleanly on worker {0}.", environment.Host.WorkerName); } catch (TypeLoadException typeLoadException) { log.TryErrorFormat(typeLoadException, "Runtime Host: Type {0} could not be loaded. The Runtime Host will be restarted.", typeLoadException.TypeName); } catch (FileLoadException fileLoadException) { // Tentatively: referenced assembly is missing log.TryFatal("Runtime Host: Could not load assembly probably due to a missing reference assembly. The Runtime Host will be restarted.", fileLoadException); } catch (SecurityException securityException) { // Tentatively: assembly cannot be loaded due to security config log.TryFatalFormat(securityException, "Runtime Host: Could not load assembly {0} probably due to security configuration. The Runtime Host will be restarted.", securityException.FailedAssemblyInfo); } catch (TriggerRestartException) { log.TryDebugFormat("Runtime Host: Triggered to stop execution on worker {0}. The Role Instance will be recycled and the Runtime Host restarted.", environment.Host.WorkerName); return(true); } catch (ThreadAbortException) { Thread.ResetAbort(); log.TryDebugFormat("Runtime Host: execution was aborted on worker {0}. The Runtime is stopping.", environment.Host.WorkerName); } catch (Exception ex) { // Generic exception log.TryErrorFormat(ex, "Runtime Host: An unhandled {0} exception occurred on worker {1}. The Runtime Host will be restarted.", ex.GetType().Name, environment.Host.WorkerName); } finally { _stoppedWaitHandle.Set(); _runtime = null; } return(false); } }
/// <summary> /// Run the hosted runtime, blocking the calling thread. /// </summary> /// <returns>True if the worker stopped as planned (e.g. due to updated assemblies)</returns> public bool Run(CloudConfigurationSettings settings) { _stoppedWaitHandle.Reset(); // Runtime IoC Setup var runtimeBuilder = new ContainerBuilder(); runtimeBuilder.RegisterModule(new CloudModule()); runtimeBuilder.RegisterInstance(settings); runtimeBuilder.RegisterType<Runtime>().InstancePerDependency(); // Run using (var runtimeContainer = runtimeBuilder.Build()) { var environment = runtimeContainer.Resolve<IEnvironment>(); var log = runtimeContainer.Resolve<ILog>(); AppDomain.CurrentDomain.UnhandledException += (sender, e) => log.TryErrorFormat( e.ExceptionObject as Exception, "Runtime Host: An unhandled {0} exception occurred on worker {1} in a background thread. The Runtime Host will be restarted: {2}.", e.ExceptionObject.GetType().Name, environment.Host.WorkerName, e.IsTerminating); _runtime = null; try { _runtime = runtimeContainer.Resolve<Runtime>(); _runtime.RuntimeContainer = runtimeContainer; // runtime endlessly keeps pinging queues for pending work _runtime.Execute(); log.TryDebugFormat("Runtime Host: Runtime has stopped cleanly on worker {0}.", environment.Host.WorkerName); } catch (TypeLoadException typeLoadException) { log.TryErrorFormat(typeLoadException, "Runtime Host: Type {0} could not be loaded. The Runtime Host will be restarted.", typeLoadException.TypeName); } catch (FileLoadException fileLoadException) { // Tentatively: referenced assembly is missing log.TryFatal("Runtime Host: Could not load assembly probably due to a missing reference assembly. The Runtime Host will be restarted.", fileLoadException); } catch (SecurityException securityException) { // Tentatively: assembly cannot be loaded due to security config log.TryFatalFormat(securityException, "Runtime Host: Could not load assembly {0} probably due to security configuration. The Runtime Host will be restarted.", securityException.FailedAssemblyInfo); } catch (TriggerRestartException) { log.TryDebugFormat("Runtime Host: Triggered to stop execution on worker {0}. The Role Instance will be recycled and the Runtime Host restarted.", environment.Host.WorkerName); return true; } catch (ThreadAbortException) { Thread.ResetAbort(); log.TryDebugFormat("Runtime Host: execution was aborted on worker {0}. The Runtime is stopping.", environment.Host.WorkerName); } catch (Exception ex) { // Generic exception log.TryErrorFormat(ex, "Runtime Host: An unhandled {0} exception occurred on worker {1}. The Runtime Host will be restarted.", ex.GetType().Name, environment.Host.WorkerName); } finally { _stoppedWaitHandle.Set(); _runtime = null; } return false; } }