예제 #1
0
 private void responseExceptionHandler(HttpRequest req, Exception exception, HttpResponse resp)
 {
     lock (_log)
     {
         if (req != null && resp != null)
         {
             _log.Error("Error in response for request {0}, status code {1}:".Fmt(req.Url.ToFull(), (int)resp.Status));
         }
         PropellerUtil.LogException(_log, exception);
     }
 }
예제 #2
0
 private HttpResponse errorHandler(HttpRequest req, Exception exception)
 {
     if (exception is HttpException httpExc)
     {
         _log.Info("Request {0} failure code {1}.".Fmt(req.Url.ToFull(), (int)httpExc.StatusCode));
     }
     else
     {
         lock (_log)
         {
             _log.Error("Error in handler for request {0}:".Fmt(req.Url.ToFull()));
             PropellerUtil.LogException(_log, exception);
         }
     }
     throw exception;
 }
예제 #3
0
        public void Start(string settingsPath, bool backgroundThread = false)
        {
            _settingsPath = settingsPath ?? SettingsUtil.GetAttribute <PropellerSettings>().GetFileName();

            // Do one reinitialization outside of the periodic schedule so that if the first initialization fails, the service doesn’t start
            try
            {
                reinitialize();
            }
            catch (Exception e)
            {
                PropellerUtil.LogException(_log ?? new ConsoleLogger(), e);
                throw;
            }

            // Now start the periodic checking that might trigger reinitialization
            base.Start(backgroundThread);
        }
예제 #4
0
        private void checkSettingsChanges()
        {
            try
            {
                // ① If the server settings have changed, reinitialize everything.
                if (reinitialize())
                {
                    return;
                }

                // ② If a module rewrote its settings, save the settings file.
                if (_settingsSavedByModule)
                {
                    _log.Debug("A module saved the settings.");
                    try
                    {
                        lock (_lockObject)
                            CurrentSettings.Save(_settingsPath);
                    }
                    catch (Exception e)
                    {
                        _log.Error("Error saving Propeller settings:");
                        PropellerUtil.LogException(_log, e);
                    }
                    _settingsSavedByModule   = false;
                    _settingsLastChangedTime = File.GetLastWriteTimeUtc(_settingsPath);
                }

                // ③ If any module wants to reinitialize, do it
                AppDomainInfo[] actives;
                lock (_lockObject)
                    actives = _activeAppDomains.ToArray();
                foreach (var active in actives)
                {
                    if (!active.MustReinitialize)   // this adds a log message if it returns true
                    {
                        continue;
                    }
                    _log.Info("Module says it must reinitialize: {0} ({1})".Fmt(active.ModuleSettings.ModuleName, active.GetHashCode()));
                    var newAppDomain = new AppDomainInfo(_log, CurrentSettings, active.ModuleSettings, active.Saver);
                    lock (_lockObject)
                    {
                        _inactiveAppDomains.Add(active);
                        _activeAppDomains.Remove(active);
                        _activeAppDomains.Add(newAppDomain);
                        _server.Handler = createResolver().Handle;
                        _log.Info(" --- {0} replaced with {1}, {0} shutting down".Fmt(active.GetHashCode(), newAppDomain.GetHashCode()));
                        active.RunnerProxy.Shutdown();
                    }
                }

                // ④ Try to clean up as many inactive AppDomains as possible
                AppDomainInfo[] inactives;
                lock (_lockObject)
                    inactives = _inactiveAppDomains.ToArray();
                foreach (var inactive in inactives)
                {
                    // Ask the runner if it has active connections; if this throws, it’s in a broken state anyway, so unload it by force.
                    bool disposeAllowed;
                    try { disposeAllowed = inactive.HasActiveConnections; }
                    catch { disposeAllowed = true; }

                    if (disposeAllowed)
                    {
                        _log.Info("Disposing inactive module: {0} ({1})".Fmt(inactive.ModuleSettings.ModuleName, inactive.GetHashCode()));
                        lock (_lockObject)
                            _inactiveAppDomains.Remove(inactive);
                        try { inactive.Dispose(); }
                        catch { }
                    }
                    else
                    {
                        _log.Info("Inactive module still has active connections: {0} ({1})".Fmt(inactive.ModuleSettings.ModuleName, inactive.GetHashCode()));
                    }
                }
            }
            catch (Exception e)
            {
                PropellerUtil.LogException(_log, e);
            }
        }