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 Task ProcessAsync(ProcessingContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } context.ThrowIfStopping(); return(ProcessCoreAsync(context)); }
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 ProcessCoreAsync(ProcessingContext context) { try { var worked = await Step(context); context.ThrowIfStopping(); Waiting = true; if (!worked) { var token = GetTokenToWaitOn(context); } await WaitHandleEx.WaitAnyAsync(WaitHandleEx.PulseEvent, context.CancellationToken.WaitHandle, _pollingDelay); } finally { Waiting = false; } }