Exemplo n.º 1
0
        public DatasourceModule(IMyDbContext context, IRealTimeDataBroker rtb, IHistoricalDataBroker hdb, IEconomicReleaseBroker erb)
            : base("/datasources")
        {
            this.RequiresAuthentication();

            Get["/", runAsync : true] = async(_, token) => await context.Datasources.ToListAsync(token).ConfigureAwait(false);

            Get["/status"] = _ =>
            {
                var realtime     = rtb.DataSources.Values.ToDictionary(x => x.Name, x => x.Connected);
                var historical   = hdb.DataSources.Values.ToDictionary(x => x.Name, x => x.Connected);
                var econReleases = erb.DataSources.Values.ToDictionary(x => x.Name, x => x.Connected);

                var names = realtime.Keys.Union(historical.Keys).Union(econReleases.Keys);

                var statuses = new List <DataSourceStatus>();
                foreach (var name in names)
                {
                    var status = new DataSourceStatus
                    {
                        Name = name,
                        RealtimeConnected     = realtime.ContainsKey(name) ? realtime[name] : (bool?)null,
                        HistoricalConnected   = historical.ContainsKey(name) ? historical[name] : (bool?)null,
                        EconReleasesConnected = econReleases.ContainsKey(name) ? econReleases[name] : (bool?)null,
                    };
                    statuses.Add(status);
                }

                return(statuses);
            };

            Get["/activestreams"] = _ => rtb.ActiveStreams;
        }
        internal DataSourceUpdatesImpl(
            IDataStore store,
            IDataStoreStatusProvider dataStoreStatusProvider,
            TaskExecutor taskExecutor,
            Logger baseLogger,
            TimeSpan?outageLoggingTimeout
            )
        {
            _store = store;
            _dataStoreStatusProvider = dataStoreStatusProvider;
            _taskExecutor            = taskExecutor;
            _log = baseLogger.SubLogger(LogNames.DataSourceSubLog);

            _dependencyTracker = new DependencyTracker();

            _outageTracker = outageLoggingTimeout.HasValue ?
                             new DataSourceOutageTracker(_log, outageLoggingTimeout.Value) : null;

            var initialStatus = new DataSourceStatus
            {
                State      = DataSourceState.Initializing,
                StateSince = DateTime.Now,
                LastError  = null
            };

            _status = new StateMonitor <DataSourceStatus, StateAndError>(initialStatus, MaybeUpdateStatus, _log);
        }
        public void UpdateStatus(DataSourceState newState, DataSourceStatus.ErrorInfo?newError)
        {
            DataSourceStatus?statusToBroadcast = null;

            lock (_stateLock)
            {
                var oldStatus = _currentStatus;

                if (newState == DataSourceState.Interrupted && oldStatus.State == DataSourceState.Initializing)
                {
                    newState = DataSourceState.Initializing; // see comment on IDataSourceUpdates.UpdateStatus
                }

                if (newState != oldStatus.State || newError.HasValue)
                {
                    _currentStatus = new DataSourceStatus
                    {
                        State      = newState,
                        StateSince = newState == oldStatus.State ? oldStatus.StateSince : DateTime.Now,
                        LastError  = newError.HasValue ? newError : oldStatus.LastError
                    };
                    statusToBroadcast = _currentStatus;
                    _stateChangedSignal.NotifyAll();
                }
            }

            _outageTracker?.TrackDataSourceState(newState, newError);

            if (statusToBroadcast.HasValue)
            {
                _taskExecutor.ScheduleEvent(this, statusToBroadcast.Value, StatusChanged);
            }
        }
 private bool SetFailureInternal(Exception failure)
 {
     lock (_gate)
     {
         if (_isClosed || _dataSourceStatus != DataSourceStatus.IN_PROGRESS)
         {
             return(false);
         }
         else
         {
             _dataSourceStatus = DataSourceStatus.FAILURE;
             _failure          = failure;
             return(true);
         }
     }
 }
        /// <summary>
        /// Updates the status display
        /// </summary>
        public void UpdateStatus()
        {
            string keywordDisplay = KeywordStatus != null ? KeywordStatus : String.Empty;

            if (keywordDisplay.Length > 200)
            {
                keywordDisplay = keywordDisplay.Substring(0, 200) + "...";
            }
            string sourceDisplay = DataSourceStatus != null ? DataSourceStatus : String.Empty;

            if (sourceDisplay.Length > 200)
            {
                sourceDisplay = DataSourceStatus.Substring(0, 200) + "...";
            }

            searchStatusPanel.Caption = String.Format("Area: {0} | Keywords: {1} | Sources: {2}", AreaStatus, keywordDisplay, sourceDisplay);
        }
 public void VerifyDataSourceStatusError(DataSourceStatus status)
 {
     Assert.Equal(Recoverable ? DataSourceState.Interrupted : DataSourceState.Shutdown, status.State);
     Assert.NotNull(status.LastError);
     Assert.Equal(
         IOException is null
             ? DataSourceStatus.ErrorKind.ErrorResponse
             : DataSourceStatus.ErrorKind.NetworkError,
         status.LastError.Value.Kind);
     Assert.Equal(
         IOException is null ? StatusCode : 0,
         status.LastError.Value.StatusCode
         );
     if (IOException != null)
     {
         Assert.Contains(IOException.Message, status.LastError.Value.Message);
     }
 }
        internal DataSourceUpdateSinkImpl(
            FlagDataManager dataStore,
            bool isConfiguredOffline,
            TaskExecutor taskExecutor,
            Logger log
            )
        {
            _dataStore    = dataStore;
            _taskExecutor = taskExecutor;
            var initialStatus = new DataSourceStatus
            {
                State      = isConfiguredOffline ? DataSourceState.SetOffline : DataSourceState.Initializing,
                StateSince = DateTime.Now,
                LastError  = null
            };

            _status = new StateMonitor <DataSourceStatus, StateAndError>(initialStatus, MaybeUpdateStatus, log);
        }
        private static DataSourceStatus?MaybeUpdateStatus(
            DataSourceStatus oldStatus,
            StateAndError update
            )
        {
            var newState =
                (update.State == DataSourceState.Interrupted && oldStatus.State == DataSourceState.Initializing)
                ? DataSourceState.Initializing  // see comment on IDataSourceUpdates.UpdateStatus
                : update.State;

            if (newState == oldStatus.State && !update.Error.HasValue)
            {
                return(null);
            }
            return(new DataSourceStatus
            {
                State = newState,
                StateSince = newState == oldStatus.State ? oldStatus.StateSince : DateTime.Now,
                LastError = update.Error ?? oldStatus.LastError
            });
        }
        private bool SetResultInternal(T value, bool isLast)
        {
            T resultToClose = default(T);

            try
            {
                lock (_gate)
                {
                    if (_isClosed || _dataSourceStatus != DataSourceStatus.IN_PROGRESS)
                    {
                        resultToClose = value;
                        return(false);
                    }
                    else
                    {
                        if (isLast)
                        {
                            _dataSourceStatus = DataSourceStatus.SUCCESS;
                            _progress         = 1;
                        }

                        if (!Equals(_result, value))
                        {
                            resultToClose = _result;
                            _result       = value;
                        }

                        return(true);
                    }
                }
            }
            finally
            {
                if (resultToClose != null)
                {
                    CloseResult(resultToClose);
                }
            }
        }
 /// <summary>
 /// Instantiates the <see cref="AbstractDataSource{T}"/>.
 /// </summary>
 protected AbstractDataSource()
 {
     _isClosed         = false;
     _dataSourceStatus = DataSourceStatus.IN_PROGRESS;
     _subscribers      = new BlockingCollection <Tuple <IDataSubscriber <T>, IExecutorService> >();
 }