public void GetEnqueuedAndFetchedCount_ReturnsEqueuedCount_WhenExists()
        {
            EnqueuedAndFetchedCountDto result = null;

            _storage.UseConnection(connection =>
            {
                connection.Execute(
                    "insert into JobQueue (JobId, Queue) " +
                    "values (1, @queue);", new { queue = _queue });

                result = _sut.GetEnqueuedAndFetchedCount(_queue);

                connection.Execute("delete from JobQueue");
            });

            Assert.Equal(1, result.EnqueuedCount);
        }
コード例 #2
0
        public IEnumerable <string> GetQueues()
        {
            lock (_cacheLock)
            {
                if (_queuesCache.Count == 0 || _cacheUpdated.Add(QueuesCacheTimeout) < DateTime.UtcNow)
                {
                    var result = _storage.UseConnection(connection =>
                    {
                        return(connection.Query("select distinct(Queue) from JobQueue").Select(x => (string)x.Queue).ToList());
                    });

                    _queuesCache  = result;
                    _cacheUpdated = DateTime.UtcNow;
                }

                return(_queuesCache.ToList());
            }
        }
コード例 #3
0
        public void RemoveFromQueue()
        {
            Logger.TraceFormat("RemoveFromQueue JobId={0}", JobId);

            //todo: unit test
            Retry.Do(() =>
            {
                _storage.UseConnection(connection => connection.Execute(
                                           "delete from JobQueue " +
                                           "where Id = @id",
                                           new
                {
                    id = _id
                }));
            }, TimeSpan.FromSeconds(3), 10);

            _removedFromQueue = true;
        }
コード例 #4
0
        public void Execute(CancellationToken cancellationToken)
        {
            foreach (var table in ProcessedTables)
            {
                Logger.DebugFormat("Removing outdated records from table '{0}'...", _options.TablePrefix + "_" + table);

                int removedCount = 0;

                do
                {
                    _storage.UseConnection(connection =>
                    {
                        try
                        {
                            Logger.DebugFormat("delete from `{0}` where ExpireAt < @now limit @count;", _options.TablePrefix + "_" + table);

                            using (
                                new MySqlDistributedLock(
                                    connection,
                                    _options,
                                    DistributedLockKey,
                                    DefaultLockTimeout,
                                    cancellationToken).Acquire())
                            {
                                removedCount = connection.Execute(
                                    String.Format(
                                        "delete from `{0}` where ExpireAt < @now limit @count;", _options.TablePrefix + "_" + table),
                                    new { now = DateTime.UtcNow, count = NumberOfRecordsInSinglePass });
                            }

                            Logger.DebugFormat("removed records count={0}", removedCount);
                        }
                        catch (MySqlException ex)
                        {
                            Logger.Error(ex.ToString());
                        }
                    });

                    if (removedCount > 0)
                    {
                        Logger.Trace(String.Format("Removed {0} outdated record(s) from '{1}' table.", removedCount,
                                                   _options.TablePrefix + "_" + table));

                        cancellationToken.WaitHandle.WaitOne(DelayBetweenPasses);
                        cancellationToken.ThrowIfCancellationRequested();
                    }
                } while (removedCount > 0);
            }

            cancellationToken.WaitHandle.WaitOne(_checkInterval);
        }
コード例 #5
0
        public void CountersAggregatorExecutesProperly()
        {
            const string createSql = @"
insert into Counter (`Key`, Value, ExpireAt) 
values ('key', 1, @expireAt)";

            _storage.UseConnection(connection =>
            {
                // Arrange
                connection.Execute(createSql, new { expireAt = DateTime.UtcNow.AddHours(1) });

                var cts = new CancellationTokenSource();
                cts.Cancel();

                // Act
                _sut.Execute(cts.Token);

                // Assert
                Assert.Equal(1, connection.Query <int>(@"select count(*) from AggregatedCounter").Single());
            });
        }
コード例 #6
0
        // BackgroundProcessContext,CancellationToken
        //public void Execute(BackgroundProcessContext backgroundProcessContext)
        //{
        //    Logger.DebugFormat("Aggregating records in 'Counter' table...");

        //    int removedCount = 0;

        //    do
        //    {
        //        _storage.UseConnection(connection =>
        //        {
        //            removedCount = connection.Execute(
        //                GetAggregationQuery(),
        //                new { now = DateTime.UtcNow, count = NumberOfRecordsInSinglePass });
        //        });

        //        if (removedCount >= NumberOfRecordsInSinglePass)
        //        {
        //            backgroundProcessContext.StoppingToken.WaitHandle.WaitOne(DelayBetweenPasses);
        //            backgroundProcessContext.StoppingToken.ThrowIfCancellationRequested();
        //        }
        //    } while (removedCount >= NumberOfRecordsInSinglePass);

        //    backgroundProcessContext.StoppingToken.WaitHandle.WaitOne(_interval);
        //}

        public void Execute(CancellationToken cancellationToken)
        {
            Logger.DebugFormat("Aggregating records in 'Counter' table...");

            int removedCount = 0;

            do
            {
                _storage.UseConnection(connection =>
                {
                    removedCount = connection.Execute(
                        GetAggregationQuery(),
                        new { now = DateTime.UtcNow, count = NumberOfRecordsInSinglePass });
                });

                if (removedCount >= NumberOfRecordsInSinglePass)
                {
                    cancellationToken.WaitHandle.WaitOne(DelayBetweenPasses);
                    cancellationToken.ThrowIfCancellationRequested();
                }
            } while (removedCount >= NumberOfRecordsInSinglePass);

            cancellationToken.WaitHandle.WaitOne(_interval);
        }
コード例 #7
0
        public void Dequeue_ShouldThrowAnException_WhenQueuesCollectionIsNull()
        {
            _storage.UseConnection(connection =>
            {
                var queue = CreateJobQueue(connection);

                var exception = Assert.Throws <ArgumentNullException>(
                    () => queue.Dequeue(null, CreateTimingOutCancellationToken()));

                Assert.Equal("queues", exception.ParamName);
            });
        }
コード例 #8
0
        public void GetStatistics_ShouldReturnEnqueuedCount()
        {
            const int expectedEnqueuedCount = 1;

            StatisticsDto result = null;

            _storage.UseConnection(connection =>
            {
                connection.Execute(
                    "insert into Job (InvocationData, Arguments, CreatedAt, StateName) " +
                    "values ('', '', UTC_TIMESTAMP(),'Enqueued');");

                result = _sut.GetStatistics();
            });

            Assert.Equal(expectedEnqueuedCount, result.Enqueued);
        }
コード例 #9
0
 private T UseConnection <T>(Func <IDbConnection, T> action)
 {
     return(_storage.UseConnection(action));
 }
コード例 #10
0
 private T UseConnection <T>(Func <MySqlConnection, T> action) =>
 _storage.UseConnection(action);
コード例 #11
0
        public override string CreateExpiredJob(Job job, IDictionary <string, string> parameters, DateTime createdAt, TimeSpan expireIn)
        {
            if (job == null)
            {
                throw new ArgumentNullException("job");
            }
            if (parameters == null)
            {
                throw new ArgumentNullException("parameters");
            }

            var invocationData = InvocationData.SerializeJob(job);

            Logger.TraceFormat("CreateExpiredJob={0}", Json.ToJson(invocationData));

            return(_storage.UseConnection(connection =>
            {
                var jobId = connection.Query <int>(
                    $"insert into {_options.TablePrefix}_Job (InvocationData, Arguments, CreatedAt, ExpireAt) " +
                    "values (@invocationData, @arguments, @createdAt, @expireAt); " +
                    "select last_insert_id();",
                    new
                {
                    invocationData = Json.ToJson(invocationData),
                    arguments = invocationData.Arguments,
                    createdAt = createdAt,
                    expireAt = createdAt.Add(expireIn)
                }).Single().ToString();

                if (parameters.Count > 0)
                {
                    var parameterArray = new object[parameters.Count];
                    int parameterIndex = 0;
                    foreach (var parameter in parameters)
                    {
                        parameterArray[parameterIndex++] = new
                        {
                            jobId = jobId,
                            name = parameter.Key,
                            value = parameter.Value
                        };
                    }

                    connection.Execute(
                        $"insert into {_options.TablePrefix}_JobParameter (JobId, Name, Value) values (@jobId, @name, @value)",
                        parameterArray);
                }

                return jobId;
            }));
        }