public async Task ProcessAsync(ProcessingContext context) { _logger.LogDebug("Collecting expired entities."); var tables = new string[] { $"{_options.TableNamePrefix}.published", $"{_options.TableNamePrefix}.received" }; foreach (var table in tables) { var removedCount = 0; do { using (var connection = new MySqlConnection(_options.ConnectionString)) { removedCount = await connection.ExecuteAsync($@"DELETE FROM `{table}` WHERE ExpiresAt < @now limit @count;", new { now = DateTime.Now, count = MaxBatch }); } if (removedCount != 0) { await context.WaitAsync(_delay); context.ThrowIfStopping(); } } while (removedCount != 0); } await context.WaitAsync(_waitingInterval); }
public async Task ProcessAsync(ProcessingContext context) { foreach (var table in Tables) { _logger.LogDebug($"Collecting expired data from table [{_options.Schema}].[{table}]."); int removedCount; do { using (var connection = new SqlConnection(_options.ConnectionString)) { removedCount = await connection.ExecuteAsync($@" DELETE TOP (@count) FROM [{_options.Schema}].[{table}] WITH (readpast) WHERE ExpiresAt < @now;", new { now = DateTime.Now, count = MaxBatch }); } if (removedCount != 0) { await context.WaitAsync(_delay); context.ThrowIfStopping(); } } while (removedCount != 0); } await context.WaitAsync(_waitingInterval); }
public async Task ProcessAsync(ProcessingContext context) { foreach (var table in Tables) { _logger.LogDebug($"Collecting expired data from table [{_options.Schema}].[{table}]."); var removedCount = 0; do { using (var connection = new NpgsqlConnection(_options.ConnectionString)) { removedCount = await connection.ExecuteAsync( $"DELETE FROM \"{_options.Schema}\".\"{table}\" WHERE \"ExpiresAt\" < @now AND \"Id\" IN (SELECT \"Id\" FROM \"{_options.Schema}\".\"{table}\" LIMIT @count);", new { now = DateTime.Now, count = MaxBatch }); } if (removedCount != 0) { await context.WaitAsync(_delay); context.ThrowIfStopping(); } } while (removedCount != 0); } await context.WaitAsync(_waitingInterval); }
public async Task ProcessAsync(ProcessingContext context) { using (var scope = context.Provider.CreateScope()) { foreach (var table in Tables) { _logger.LogDebug($"Collecting expired data from table [{table}]."); var removedCount = 0; do { var dbContext = (DbContext)scope.ServiceProvider.GetRequiredService(_options.DbContextType); if (table == "published") { var entities = dbContext.Set <PublishedMessage>().Where(t => t.ExpiresAt < DateTime.Now).Take(MaxBatch); dbContext.Set <PublishedMessage>().RemoveRange(entities); await dbContext.SaveChangesAsync(); } else { var entities = dbContext.Set <ReceivedMessage>().Where(t => t.ExpiresAt < DateTime.Now).Take(MaxBatch); dbContext.Set <ReceivedMessage>().RemoveRange(entities); await dbContext.SaveChangesAsync(); } //using (var connection = new NpgsqlConnection(_options.ConnectionString)) //{ // removedCount = await connection.ExecuteAsync( // $"DELETE FROM \"{_options.Schema}\".\"{table}\" WHERE \"ExpiresAt\" < @now AND \"Id\" IN (SELECT \"Id\" FROM \"{_options.Schema}\".\"{table}\" LIMIT @count);", // new {now = DateTime.Now, count = MaxBatch}); //} if (removedCount != 0) { await context.WaitAsync(_delay); context.ThrowIfStopping(); } } while (removedCount != 0); } } await context.WaitAsync(_waitingInterval); }
public async Task ProcessAsync(ProcessingContext context) { _logger.LogDebug($"Collecting expired data from memory list."); var connection = (InMemoryStorageConnection)context.Provider.GetService <IStorageConnection>(); connection.PublishedMessages.RemoveAll(x => x.ExpiresAt < DateTime.Now); connection.ReceivedMessages.RemoveAll(x => x.ExpiresAt < DateTime.Now); await context.WaitAsync(_waitingInterval); }
public async Task ProcessAsync(ProcessingContext context) { _logger.LogDebug($"Collecting expired data from collection [{_options.PublishedCollection}]."); var publishedCollection = _database.GetCollection <PublishedMessage>(_options.PublishedCollection); var receivedCollection = _database.GetCollection <ReceivedMessage>(_options.ReceivedCollection); await publishedCollection.BulkWriteAsync(new[] { new DeleteManyModel <PublishedMessage>( Builders <PublishedMessage> .Filter.Lt(x => x.ExpiresAt, DateTime.Now)) }); await receivedCollection.BulkWriteAsync(new[] { new DeleteManyModel <ReceivedMessage>( Builders <ReceivedMessage> .Filter.Lt(x => x.ExpiresAt, DateTime.Now)) }); await context.WaitAsync(_waitingInterval); }