/// <inheritdoc />
        public List <ResetHeartBeatOutput> Reset(CancellationToken cancelToken)
        {
            if (!_configuration.HeartBeat.Enabled)
            {
                return(new List <ResetHeartBeatOutput>(0));
            }

            if (string.IsNullOrEmpty(_configuration.TransportConfiguration.ConnectionInfo.ConnectionString))
            {
                return(new List <ResetHeartBeatOutput>(0));
            }

            List <ResetHeartBeatOutput> returnData = new List <ResetHeartBeatOutput>();
            var      query   = new FindMessagesToResetByHeartBeatQuery <T>(cancelToken);
            DateTime start   = _getTime.GetCurrentUtcDate();
            var      results = _queryHandler.Handle(query);
            DateTime end     = _getTime.GetCurrentUtcDate();

            foreach (var result in results)
            {
                var id          = new MessageQueueId <T>(result.QueueId);
                var command     = new ResetHeartBeatCommand <T>(result);
                var resetResult = _commandHandler.Handle(command);
                if (resetResult <= 0)
                {
                    continue;
                }
                var data = new ResetHeartBeatOutput(id, result.Headers, start, end);
                returnData.Add(data);
            }
            return(returnData);
        }
 public void Create_Default()
 {
     using (var cancel = new CancellationTokenSource())
     {
         var test = new FindMessagesToResetByHeartBeatQuery <long>(cancel.Token);
         Assert.Equal(cancel.Token, test.Cancellation);
     }
 }
 public void Create_Default()
 {
     using (var cancel = new CancellationTokenSource())
     {
         var test = new FindMessagesToResetByHeartBeatQuery(cancel.Token);
         Assert.Equal(cancel.Token, test.Cancellation);
     }
 }
        /// <summary>
        /// Used to find and reset work items that are out of the heart beat window
        /// </summary>
        public long Reset(CancellationToken cancelToken)
        {
            if (!_configuration.HeartBeat.Enabled)
                return 0;

            if (string.IsNullOrEmpty(_configuration.TransportConfiguration.ConnectionInfo.ConnectionString)) return 0;

            var query = new FindMessagesToResetByHeartBeatQuery(cancelToken);
            return _queryHandler.Handle(query).Select(queueid => 
                new ResetHeartBeatCommand(queueid)).Aggregate<ResetHeartBeatCommand, long>(0, (current, command) => 
                    current + _commandHandler.Handle(command));
        }
        /// <summary>
        /// Handles the specified query.
        /// </summary>
        /// <param name="query">The query.</param>
        /// <param name="dbCommand">The database command.</param>
        /// <param name="commandType">Type of the command.</param>
        public void Handle(FindMessagesToResetByHeartBeatQuery query, IDbCommand dbCommand, CommandStringTypes commandType)
        {
            dbCommand.CommandText =
                _commandCache.GetCommand(commandType);

            var command = (NpgsqlCommand)dbCommand;

            command.Parameters.Add("@Time", NpgsqlDbType.Bigint);
            var selectTime = _getTime.GetCurrentUtcDate().AddSeconds(_configuration.HeartBeat.Time.TotalSeconds * -1);

            command.Parameters["@time"].Value = selectTime.Ticks;
            command.Parameters.Add("@Status", NpgsqlDbType.Integer);
            command.Parameters["@Status"].Value = Convert.ToInt16(QueueStatuses.Processing);
        }
        public IEnumerable <MessageToReset <T> > Handle(FindMessagesToResetByHeartBeatQuery <T> query)
        {
            var results = new List <MessageToReset <T> >();

            if (query.Cancellation.IsCancellationRequested)
            {
                return(results);
            }

            using (var connection = _dbConnectionFactory.Create())
            {
                connection.Open();

                //before executing a query, double check that we aren't stopping
                //otherwise, there is a chance that the tables no longer exist in memory mode
                if (query.Cancellation.IsCancellationRequested)
                {
                    return(results);
                }

                using (var command = connection.CreateCommand())
                {
                    _prepareQuery.Handle(query, command, CommandStringTypes.GetHeartBeatExpiredMessageIds);
                    using (var reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            if (query.Cancellation.IsCancellationRequested)
                            {
                                break;
                            }

                            var headers = _readColumn.ReadAsByteArray(CommandStringTypes.GetHeartBeatExpiredMessageIds, 2,
                                                                      reader);
                            if (headers != null)
                            {
                                var allheaders = _serialization.InternalSerializer.ConvertBytesTo <IDictionary <string, object> >(headers);
                                results.Add(new MessageToReset <T>(_readColumn.ReadAsType <T>(CommandStringTypes.GetHeartBeatExpiredMessageIds, 0, reader), _readColumn.ReadAsDateTime(CommandStringTypes.GetHeartBeatExpiredMessageIds, 1, reader), new ReadOnlyDictionary <string, object>(allheaders)));
                            }
                            else
                            {
                                results.Add(new MessageToReset <T>(_readColumn.ReadAsType <T>(CommandStringTypes.GetHeartBeatExpiredMessageIds, 0, reader), _readColumn.ReadAsDateTime(CommandStringTypes.GetHeartBeatExpiredMessageIds, 1, reader), null));
                            }
                        }
                    }
                }
            }
            return(results);
        }
        /// <inheritdoc />
        public IEnumerable <MessageToReset <int> > Handle(FindMessagesToResetByHeartBeatQuery <int> query)
        {
            if (query.Cancellation.IsCancellationRequested)
            {
                return(Enumerable.Empty <MessageToReset <int> >());
            }

            using (var db = _connectionInformation.GetDatabase())
            {
                //before executing a query, double check that we aren't stopping
                //otherwise, there is a chance that the tables no longer exist in memory mode
                if (query.Cancellation.IsCancellationRequested)
                {
                    return(Enumerable.Empty <MessageToReset <int> >());
                }

                var col  = db.Database.GetCollection <Schema.MetaDataTable>(_tableNameHelper.MetaDataName);
                var date = DateTime.UtcNow.Subtract(_configuration.Time);

                var results = col.Query()
                              .Where(x => x.Status == QueueStatuses.Processing)
                              .Where(x => x.HeartBeat.HasValue && x.HeartBeat.Value < date)
                              .ToList();

                var data  = new List <MessageToReset <int> >(results.Count);
                var queue = db.Database.GetCollection <Schema.QueueTable>(_tableNameHelper.QueueName);
                foreach (var record in results)
                {
                    if (record.HeartBeat.HasValue)
                    {
                        var queueRecord = queue.FindById(record.QueueId);
                        if (queueRecord != null)
                        {
                            var headers =
                                _serialization.InternalSerializer.ConvertBytesTo <IDictionary <string, object> >(
                                    queueRecord
                                    .Headers);
                            var reset = new MessageToReset <int>(record.QueueId, record.HeartBeat.Value,
                                                                 new ReadOnlyDictionary <string, object>(headers));
                            data.Add(reset);
                        }
                    }
                }

                return(data);
            }
        }
        /// <inheritdoc />
        public long Reset(CancellationToken cancelToken)
        {
            if (!_configuration.HeartBeat.Enabled)
            {
                return(0);
            }

            if (string.IsNullOrEmpty(_configuration.TransportConfiguration.ConnectionInfo.ConnectionString))
            {
                return(0);
            }

            var query = new FindMessagesToResetByHeartBeatQuery(cancelToken);

            return(_queryHandler.Handle(query).Select(queueid =>
                                                      new ResetHeartBeatCommand(queueid)).Aggregate <ResetHeartBeatCommand, long>(0, (current, command) =>
                                                                                                                                  current + _commandHandler.Handle(command)));
        }
Example #9
0
        /// <inheritdoc />
        public void Handle(FindMessagesToResetByHeartBeatQuery query, IDbCommand dbCommand, CommandStringTypes commandType)
        {
            dbCommand.CommandText =
                _commandCache.GetCommand(commandType);

            var param = dbCommand.CreateParameter();

            param.ParameterName = "@Time";
            param.DbType        = DbType.Int64;
            param.Value         = _configuration.HeartBeat.Time.TotalSeconds;
            dbCommand.Parameters.Add(param);

            param = dbCommand.CreateParameter();
            param.ParameterName = "@Status";
            param.DbType        = DbType.Int32;
            param.Value         = Convert.ToInt16(QueueStatuses.Processing);
            dbCommand.Parameters.Add(param);
        }
Example #10
0
 /// <summary>
 /// Handles the specified query.
 /// </summary>
 /// <param name="query">The query.</param>
 /// <returns></returns>
 public IEnumerable <MessageToReset <long> > Handle(FindMessagesToResetByHeartBeatQuery <long> query)
 {
     try
     {
         return(_decorated.Handle(query));
     }
     catch (Exception e)
     {
         if (e.Message.IndexOf("abort due to ROLLBACK", StringComparison.InvariantCultureIgnoreCase) >= 0)
         {
             _logger.LogWarning($"The query has been aborted{System.Environment.NewLine}{e}");
             return(Enumerable.Empty <MessageToReset <long> >());
         }
         else
         {
             throw;
         }
     }
 }
 /// <summary>
 /// Handles the specified query.
 /// </summary>
 /// <param name="query">The query.</param>
 /// <returns></returns>
 public IEnumerable <MessageToReset> Handle(FindMessagesToResetByHeartBeatQuery query)
 {
     try
     {
         return(_decorated.Handle(query));
     }
     catch (Exception e)
     {
         if (e.Message.IndexOf("abort due to ROLLBACK", StringComparison.InvariantCultureIgnoreCase) >= 0)
         {
             _logger.WarnException("The query has been aborted", e);
             return(Enumerable.Empty <MessageToReset>());
         }
         else
         {
             throw;
         }
     }
 }
        public IEnumerable <MessageToReset> Handle(FindMessagesToResetByHeartBeatQuery query)
        {
            var results = new List <MessageToReset>();

            if (query.Cancellation.IsCancellationRequested)
            {
                return(results);
            }

            using (var connection = _dbConnectionFactory.Create())
            {
                connection.Open();

                //before executing a query, double check that we aren't stopping
                //otherwise, there is a chance that the tables no longer exist in memory mode
                if (query.Cancellation.IsCancellationRequested)
                {
                    return(results);
                }

                using (var command = connection.CreateCommand())
                {
                    _prepareQuery.Handle(query, command, CommandStringTypes.GetHeartBeatExpiredMessageIds);
                    using (var reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            if (query.Cancellation.IsCancellationRequested)
                            {
                                break;
                            }
                            results.Add(new MessageToReset(_readColumn.ReadAsInt64(CommandStringTypes.GetHeartBeatExpiredMessageIds, 0, reader), _readColumn.ReadAsDateTime(CommandStringTypes.GetHeartBeatExpiredMessageIds, 1, reader)));
                        }
                    }
                }
            }
            return(results);
        }
Example #13
0
 /// <summary>
 /// Handles the specified query.
 /// </summary>
 /// <param name="query">The query.</param>
 /// <returns></returns>
 public IEnumerable <MessageToReset <long> > Handle(FindMessagesToResetByHeartBeatQuery <long> query)
 {
     return(!_databaseExists.Exists(_connectionInformation.ConnectionString) ? Enumerable.Empty <MessageToReset <long> >() : _decorated.Handle(query));
 }