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); }
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()); } }
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; }
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); }
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()); }); }
// 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); }
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); }); }
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); }
private T UseConnection <T>(Func <IDbConnection, T> action) { return(_storage.UseConnection(action)); }
private T UseConnection <T>(Func <MySqlConnection, T> action) => _storage.UseConnection(action);
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; })); }