Exemple #1
0
        protected virtual Task DbGc()
        {
            const string sql = "delete SheepJaxMessages where CreatedUtcTime < dateadd(minute, -10, GetUtcDate())";

            return(_connectionFactory().WithinTransaction(tx =>
                                                          TplHelper.Using(new SqlCommand(sql, tx.Connection, tx), cmd =>
                                                                          cmd.ExecuteNonQueryAsync().Success(_ => tx.Commit())
                                                                          )
                                                          ).Catch(_logger));
        }
Exemple #2
0
        public virtual Task Consumed(CommandMessage last)
        {
            var msg = last as SqlCommandMessage;

            if (msg == null)
            {
                return(TplHelper.Empty);
            }

            const string sql    = "delete SheepJaxMessages where ClientId=@clientId and timestamp<@timestamp";
            var          dbTask = _connectionFactory().WithinTransaction(tx =>
                                                                         TplHelper.Using(new SqlCommand(sql, tx.Connection, tx)
            {
                Parameters =
                {
                    new SqlParameter("clientId",  msg.ClientId),
                    new SqlParameter("timestamp", msg.Timestamp)
                }
            }
                                                                                         , cmd => cmd.ExecuteNonQueryAsync().Finally(_ => tx.Commit())
                                                                                         )
                                                                         ).Catch(_logger);

            using (_cacheLock.BeginUpgradeableReadLock())
            {
                var node = _cache.Last;
                while (node != null && node.Value != last)
                {
                    node = node.Previous;
                }

                using (_cacheLock.BeginWriteLock())
                {
                    while (node != null)
                    {
                        var previousNode = node.Previous;
                        while (previousNode != null && previousNode.Value.ClientId != node.Value.ClientId)
                        {
                            previousNode = previousNode.Previous;
                        }

                        node.List.Remove(node);
                        node = previousNode;
                    }
                }
            }
            return(dbTask);
        }
Exemple #3
0
        private void SendMessage(Guid clientId, string msg)
        {
            const string sql = "insert into SheepJaxMessages (ClientId, Message) values (@clientId, @message)";

            Task.Factory.StartNew(delegate
            {
                _connectionFactory().WithinTransaction(tx =>
                                                       TplHelper.Using(new SqlCommand(sql, tx.Connection, tx)
                {
                    Parameters =
                    {
                        new SqlParameter("clientId", clientId),
                        new SqlParameter("message",  msg)
                    }
                },
                                                                       cmd => cmd.ExecuteNonQueryAsync().Finally(t => tx.Commit())
                                                                       )
                                                       );
            }).Catch(_logger);
        }
Exemple #4
0
        protected virtual Task PollDb(IObserver <SqlCommandMessage> observer)
        {
            const string sql = "select ClientId, Message, CreatedUtcTime, Timestamp from SheepJaxMessages where CreatedUtcTime > dateadd(minute, -10, GetUtcDate())";

            return(_connectionFactory().WithinTransaction(tx =>
                                                          TplHelper.Using(new SqlCommand(sql, tx.Connection, tx), cmd =>
            {
                if (_lastTimestamp != null)
                {
                    cmd.CommandText += " and timestamp > @lastTimestamp";
                    cmd.Parameters.Add(new SqlParameter("lastTimestamp", _lastTimestamp));
                }
                cmd.CommandText += " order by timestamp asc";
                return cmd.ExecuteReaderAsync()
                .Select(reader =>
                {
                    using (reader)
                        while (reader.Read())
                        {
                            var timestamp = _lastTimestamp = new byte[8];
                            reader.GetBytes(3, 0, timestamp, 0, 8);
                            var msg = new SqlCommandMessage
                            {
                                ClientId = reader.GetGuid(0),
                                Message = reader.GetString(1),
                                CreatedUtcTime = reader.GetDateTime(2),
                                Timestamp = timestamp
                            };
                            using (_cacheLock.BeginWriteLock())
                            {
                                _cache.AddLast(msg);
                                observer.OnNext(msg);
                            }
                        }
                });
            })
                                                          ).Catch(_logger));
        }