Exemple #1
0
        public void Start()
        {
            try
            {
                lock (_syncRoot)
                {
                    if (_pollingThread != null)
                    {
                        return;
                    }

                    _log.Info("----------------");
                    _log.Info("Service Starting");

                    var queries = new QueryLocator(new DapperWrapper()).PrepareQueries();
                    _settings.Endpoints.ForEach(e => e.SetQueries(queries));

                    var initialDelaySeconds   = _settings.TestMode ? 0 : _settings.PollIntervalSeconds;
                    var pollingThreadSettings = new PollingThreadSettings
                    {
                        Name = "SqlPoller",
                        // Set to immediate when in TestMode for instant gratification.
                        InitialPollDelaySeconds = initialDelaySeconds,
                        PollIntervalSeconds     = _settings.PollIntervalSeconds,
                        PollAction     = () => _metricCollector.QueryEndpoints(queries),
                        AutoResetEvent = new AutoResetEvent(false),
                    };

                    _pollingThread = new PollingThread(pollingThreadSettings, _log);
                    _pollingThread.ExceptionThrown += e => _log.Error("Polling thread exception", e);

                    _log.Debug("Service Threads Starting...");

                    if (initialDelaySeconds > 0)
                    {
                        _log.InfoFormat("Waiting {0} seconds to begin polling", initialDelaySeconds);
                    }

                    _pollingThread.Start();

                    _log.Debug("Service Threads Started");
                }
            }
            catch (Exception e)
            {
                _log.Fatal("Failed while attempting to start service");
                _log.Warn(e);
                throw;
            }
        }