diff --git a/src/Quidjibo.SqlServer/Configurations/SqlServerQuidjiboConfiguration.cs b/src/Quidjibo.SqlServer/Configurations/SqlServerQuidjiboConfiguration.cs index b9ef0c3..1c06d67 100644 --- a/src/Quidjibo.SqlServer/Configurations/SqlServerQuidjiboConfiguration.cs +++ b/src/Quidjibo.SqlServer/Configurations/SqlServerQuidjiboConfiguration.cs @@ -1,4 +1,7 @@ -using Quidjibo.Configurations; +// // Copyright (c) smiggleworth. All rights reserved. +// // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using Quidjibo.Configurations; using Quidjibo.Constants; namespace Quidjibo.SqlServer.Configurations @@ -7,6 +10,12 @@ public class SqlServerQuidjiboConfiguration : IQuidjiboConfiguration { public int PollingInterval { get; set; } = 10; public string ConnectionString { get; set; } + + /// + /// The number of days to keep completed/faulted work items. + /// + public int DaysToKeep { get; set; } = 3; + public int BatchSize { get; set; } = 5; /// diff --git a/src/Quidjibo.SqlServer/Factories/SqlWorkProviderFactory.cs b/src/Quidjibo.SqlServer/Factories/SqlWorkProviderFactory.cs index ac1b839..bde0702 100644 --- a/src/Quidjibo.SqlServer/Factories/SqlWorkProviderFactory.cs +++ b/src/Quidjibo.SqlServer/Factories/SqlWorkProviderFactory.cs @@ -51,7 +51,8 @@ await SqlRunner.ExecuteAsync(async cmd => _sqlServerQuidjiboConfiguration.ConnectionString, queues, _sqlServerQuidjiboConfiguration.LockInterval, - _sqlServerQuidjiboConfiguration.BatchSize); + _sqlServerQuidjiboConfiguration.BatchSize, + _sqlServerQuidjiboConfiguration.DaysToKeep); } finally { diff --git a/src/Quidjibo.SqlServer/Providers/SqlWorkProvider.cs b/src/Quidjibo.SqlServer/Providers/SqlWorkProvider.cs index 4ef2093..4284378 100644 --- a/src/Quidjibo.SqlServer/Providers/SqlWorkProvider.cs +++ b/src/Quidjibo.SqlServer/Providers/SqlWorkProvider.cs @@ -1,3 +1,6 @@ +// // Copyright (c) smiggleworth. All rights reserved. +// // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + using System; using System.Collections.Generic; using System.Data.SqlClient; @@ -24,6 +27,8 @@ public enum StatusFlags private readonly int _batchSize; private readonly string _connectionString; + private readonly int _daysToKeep; + private readonly ILogger _logger; private readonly int _maxAttempts; private readonly string[] _queues; private readonly int _visibilityTimeout; @@ -34,13 +39,17 @@ public SqlWorkProvider( string connectionString, string[] queues, int visibilityTimeout, - int batchSize) + int batchSize, + int daysToKeep + ) { + _logger = logger; _queues = queues; _visibilityTimeout = visibilityTimeout; _batchSize = batchSize; _maxAttempts = 10; _connectionString = connectionString; + _daysToKeep = Math.Abs(daysToKeep); } public async Task SendAsync(WorkItem item, int delay, CancellationToken cancellationToken) @@ -70,6 +79,8 @@ await ExecuteAsync(async cmd => public async Task> ReceiveAsync(string worker, CancellationToken cancellationToken) { var receiveOn = DateTime.UtcNow; + var deleteOn = _daysToKeep > 0 ? receiveOn.AddDays(-_daysToKeep) : receiveOn.AddHours(-1); + if (_receiveSql == null) { _receiveSql = await SqlLoader.GetScript("Work.Receive"); @@ -90,10 +101,14 @@ await ExecuteAsync(async cmd => cmd.AddParameter("@VisibleOn", receiveOn.AddSeconds(Math.Max(_visibilityTimeout, 30))); cmd.AddParameter("@ReceiveOn", receiveOn); cmd.AddParameter("@MaxAttempts", _maxAttempts); - cmd.AddParameter("@DeleteOn", receiveOn.AddDays(-3)); + cmd.AddParameter("@DeleteOn", deleteOn); // dynamic parameters - _queues.Select((q, i) => cmd.Parameters.AddWithValue($"@Queue{i}", q)).ToList(); + for (var i = 0; i < _queues.Length; i++) + { + cmd.Parameters.AddWithValue($"@Queue{i}", _queues[i]); + } + using (var rdr = await cmd.ExecuteReaderAsync(cancellationToken)) { while (await rdr.ReadAsync(cancellationToken))