Exemplo n.º 1
0
        private void Receive(object state)
        {
            var tcs = (TaskCompletionSource <object>)state;

            if (!_lastPayloadId.HasValue)
            {
                var lastPayloadIdOperation = new DbOperation(_connectionString, _maxIdSql, _trace)
                {
                    TracePrefix = _tracePrefix
                };

                try
                {
                    _lastPayloadId = (long?)lastPayloadIdOperation.ExecuteScalar();
                    OnQuery();

                    // Complete the StartReceiving task as we've successfully initialized the payload ID
                    tcs.TrySetResult(null);
                }
                catch (Exception ex)
                {
                    tcs.TrySetException(ex);
                    return;
                }
            }

            // NOTE: This is called from a BG thread so any uncaught exceptions will crash the process
            lock (this)
            {
                if (_disposed)
                {
                    return;
                }

                var parameter = _dbProviderFactory.CreateParameter();
                parameter.ParameterName = "PayloadId";
                parameter.Value         = _lastPayloadId.Value;

                _dbOperation = new ObservableDbOperation(_connectionString, _selectSql, _trace, parameter)
                {
                    OnError     = ex => _onError(ex),
                    OnQuery     = () => OnQuery(),
                    TracePrefix = _tracePrefix
                };
            }

            _dbOperation.ExecuteReaderWithUpdates((rdr, o) => ProcessRecord(rdr, o));

            _trace.TraceWarning("{0}SqlReceiver.Receive returned", _tracePrefix);
        }
Exemplo n.º 2
0
        public Task Send(IList <Message> messages)
        {
            if (messages == null || messages.Count == 0)
            {
                return(TaskAsyncHelper.Empty);
            }

            var parameter = _dbProviderFactory.CreateParameter();

            parameter.ParameterName = "Payload";
            parameter.DbType        = DbType.Binary;
            parameter.Value         = SqlPayload.ToBytes(messages);

            var operation = new DbOperation(_connectionString, _insertDml, _trace, parameter);

            return(operation.ExecuteNonQueryAsync());
        }
Exemplo n.º 3
0
        public void Install()
        {
            _logger.LogInformation("Start installing SignalR SQL objects");

            if (!IsSqlEditionSupported(_connectionString))
            {
                throw new PlatformNotSupportedException(Resources.Error_UnsupportedSqlEdition);
            }

            var script = GetType().GetTypeInfo().Assembly.StringResource("install.sql");

            script = script.Replace("SET @SCHEMA_NAME = 'SignalR';", "SET @SCHEMA_NAME = '" + SqlMessageBus.SchemaName + "';");
            script = script.Replace("SET @SCHEMA_TABLE_NAME = 'Schema';", "SET @SCHEMA_TABLE_NAME = '" + SchemaTableName + "';");
            script = script.Replace("SET @TARGET_SCHEMA_VERSION = 1;", "SET @TARGET_SCHEMA_VERSION = " + SchemaVersion + ";");
            script = script.Replace("SET @MESSAGE_TABLE_COUNT = 1;", "SET @MESSAGE_TABLE_COUNT = " + _tableCount + ";");
            script = script.Replace("SET @MESSAGE_TABLE_NAME = 'Messages';", "SET @MESSAGE_TABLE_NAME = '" + _messagesTableNamePrefix + "';");

            var operation = new DbOperation(_connectionString, script, _logger);

            operation.ExecuteNonQuery();

            _logger.LogInformation("SignalR SQL objects installed");
        }
Exemplo n.º 4
0
        private void ProcessRecord(IDataRecord record, DbOperation dbOperation)
        {
            var             id      = record.GetInt64(0);
            ScaleoutMessage message = SqlPayload.FromBytes(record);

            if (id != _lastPayloadId + 1)
            {
                _trace.TraceError("{0}Missed message(s) from SQL Server. Expected payload ID {1} but got {2}.", _tracePrefix, _lastPayloadId + 1, id);
            }

            if (id <= _lastPayloadId)
            {
                _trace.TraceInformation("{0}Duplicate message(s) or payload ID reset from SQL Server. Last payload ID {1}, this payload ID {2}", _tracePrefix, _lastPayloadId, id);
            }

            _lastPayloadId = id;

            // Update the Parameter with the new payload ID
            dbOperation.Parameters[0].Value = _lastPayloadId;

            Received((ulong)id, message);

            _trace.TraceVerbose("{0}Payload {1} containing {2} message(s) received", _tracePrefix, id, message.Messages.Count);
        }
Exemplo n.º 5
0
        private void Receive(object state)
        {
            var tcs = (DispatchingTaskCompletionSource <object>)state;

            if (!_lastPayloadId.HasValue)
            {
                var lastPayloadIdOperation = new DbOperation(_connectionString, _maxIdSql, _trace)
                {
                    TracePrefix = _tracePrefix
                };

                try
                {
                    _lastPayloadId = (long?)lastPayloadIdOperation.ExecuteScalar();
                    Queried();

                    _trace.TraceVerbose("{0}SqlReceiver started, initial payload id={1}", _tracePrefix, _lastPayloadId);

                    // Complete the StartReceiving task as we've successfully initialized the payload ID
                    tcs.TrySetResult(null);
                }
                catch (Exception ex)
                {
                    _trace.TraceError("{0}SqlReceiver error starting: {1}", _tracePrefix, ex);

                    tcs.TrySetException(ex);
                    return;
                }
            }

            // NOTE: This is called from a BG thread so any uncaught exceptions will crash the process
            lock (this)
            {
                if (_disposed)
                {
                    return;
                }

                var parameter = _dbProviderFactory.CreateParameter();
                parameter.ParameterName = "PayloadId";
                parameter.Value         = _lastPayloadId.Value;

                _dbOperation = new ObservableDbOperation(_connectionString, _selectSql, _trace, parameter)
                {
                    TracePrefix = _tracePrefix
                };
            }

            _dbOperation.Queried += () => Queried();
            _dbOperation.Faulted += ex => Faulted(ex);
            _dbOperation.Changed += () =>
            {
                _trace.TraceInformation("{0}Starting receive loop again to process updates", _tracePrefix);

                _dbOperation.ExecuteReaderWithUpdates(ProcessRecord);
            };

            _trace.TraceVerbose("{0}Executing receive reader, initial payload ID parameter={1}", _tracePrefix, _dbOperation.Parameters[0].Value);

            _dbOperation.ExecuteReaderWithUpdates(ProcessRecord);

            _trace.TraceInformation("{0}SqlReceiver.Receive returned", _tracePrefix);
        }
Exemplo n.º 6
0
 private void ProcessRecord(IDataRecord record, DbOperation dbOperation)