public void Initialize()
 {
     configurationProvider = new Mock <IConfigurationProvider>();
     configurationProvider.Setup(cp => cp.GetSetting(It.IsAny <string>()))
     .Returns("avalue");
     configurationProvider.Setup(cp => cp.CacheDuration)
     .Returns(60);
     configuration = new CloudConfigurationSettings(configurationProvider.Object);
 }
Пример #2
0
        /// <summary>IoC constructor.</summary>
        public Runtime(CloudStorageProviders storage, IEnvironment environment, CloudConfigurationSettings settings, ILog log, ICloudRuntimeObserver observer = null)
        {
            _storage     = storage;
            _environment = environment;
            _log         = log;
            _observer    = observer;

            _settings = settings;
        }
Пример #3
0
        /// <summary>IoC constructor.</summary>
        public Runtime(CloudStorageProviders storage, IEnvironment environment, CloudConfigurationSettings settings, ILog log, ICloudRuntimeObserver observer = null)
        {
            _storage = storage;
            _environment = environment;
            _log = log;
            _observer = observer;

            _settings = settings;
        }
Пример #4
0
        /// <summary>IoC constructor.</summary>
        public Runtime(RuntimeProviders runtimeProviders, ICloudEnvironment environment, CloudConfigurationSettings settings, ICloudRuntimeObserver observer = null)
        {
            _runtimeProviders = runtimeProviders;
            _runtimeFinalizer = runtimeProviders.RuntimeFinalizer;
            _environment = environment;
            _log = runtimeProviders.Log;
            _observer = observer;

            _settings = settings;
        }
Пример #5
0
        /// <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()
        {
            var settings = new CloudConfigurationSettings
            {
                DataConnectionString                = RoleEnvironment.GetConfigurationSettingValue("DataConnectionString"),
                SelfManagementSubscriptionId        = RoleEnvironment.GetConfigurationSettingValue("SelfManagementSubscriptionId"),
                SelfManagementCertificateThumbprint = RoleEnvironment.GetConfigurationSettingValue("SelfManagementCertificateThumbprint")
            };

            // The trick is to load this same assembly in another domain, then
            // instantiate this same class and invoke Run
            var domain = AppDomain.CreateDomain("WorkerDomain", null, AppDomain.CurrentDomain.SetupInformation);

            bool restartForAssemblyUpdate;

            try
            {
                _isolatedInstance = (SingleRuntimeHost)domain.CreateInstanceAndUnwrap(
                    Assembly.GetExecutingAssembly().FullName,
                    typeof(SingleRuntimeHost).FullName);

                // This never throws, unless something went wrong with IoC setup and that's fine
                // because it is not possible to execute the worker
                restartForAssemblyUpdate = _isolatedInstance.Run(settings);
            }
            finally
            {
                _isolatedInstance = null;

                // If this throws, it's because something went wrong when unloading the AppDomain
                // The exception correctly pulls down the entire worker process so that no AppDomains are
                // left in memory
                AppDomain.Unload(domain);
            }

            return(restartForAssemblyUpdate);
        }
Пример #6
0
        /// <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()
        {
            var settings = new CloudConfigurationSettings
                {
                    DataConnectionString = RoleEnvironment.GetConfigurationSettingValue("DataConnectionString"),
                    SelfManagementSubscriptionId = RoleEnvironment.GetConfigurationSettingValue("SelfManagementSubscriptionId"),
                    SelfManagementCertificateThumbprint = RoleEnvironment.GetConfigurationSettingValue("SelfManagementCertificateThumbprint")
                };

            // The trick is to load this same assembly in another domain, then
            // instantiate this same class and invoke Run
            var domain = AppDomain.CreateDomain("WorkerDomain", null, AppDomain.CurrentDomain.SetupInformation);

            bool restartForAssemblyUpdate;

            try
            {
                _isolatedInstance = (SingleRuntimeHost)domain.CreateInstanceAndUnwrap(
                    Assembly.GetExecutingAssembly().FullName,
                    typeof(SingleRuntimeHost).FullName);

                // This never throws, unless something went wrong with IoC setup and that's fine
                // because it is not possible to execute the worker
                restartForAssemblyUpdate = _isolatedInstance.Run(settings);
            }
            finally
            {
                _isolatedInstance = null;

                // If this throws, it's because something went wrong when unloading the AppDomain
                // The exception correctly pulls down the entire worker process so that no AppDomains are
                // left in memory
                AppDomain.Unload(domain);
            }

            return restartForAssemblyUpdate;
        }
Пример #7
0
        /// <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);
            }
        }
Пример #8
0
        /// <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;
            }
        }