From 9ff8a3fcd6f399889c391602256183025ffe9a45 Mon Sep 17 00:00:00 2001 From: Jeff Repanich Date: Fri, 23 Nov 2018 16:18:09 -0500 Subject: [PATCH 1/2] pass days to keep on config --- .../Configurations/SqlServerQuidjiboConfiguration.cs | 11 ++++++++++- .../Factories/SqlWorkProviderFactory.cs | 3 ++- src/Quidjibo.SqlServer/Providers/SqlWorkProvider.cs | 12 ++++++++++-- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/Quidjibo.SqlServer/Configurations/SqlServerQuidjiboConfiguration.cs b/src/Quidjibo.SqlServer/Configurations/SqlServerQuidjiboConfiguration.cs index b9ef0c3..4f4d529 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; } = 2; + 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..eb223fa 100644 --- a/src/Quidjibo.SqlServer/Providers/SqlWorkProvider.cs +++ b/src/Quidjibo.SqlServer/Providers/SqlWorkProvider.cs @@ -7,6 +7,7 @@ using Microsoft.Extensions.Logging; using Quidjibo.Models; using Quidjibo.Providers; +using Quidjibo.SqlServer.Configurations; using Quidjibo.SqlServer.Extensions; using Quidjibo.SqlServer.Utils; @@ -23,8 +24,10 @@ public enum StatusFlags } private readonly int _batchSize; + private readonly int _daysToKeep; private readonly string _connectionString; private readonly int _maxAttempts; + private readonly ILogger _logger; private readonly string[] _queues; private readonly int _visibilityTimeout; private string _receiveSql; @@ -34,13 +37,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 +77,7 @@ await ExecuteAsync(async cmd => public async Task> ReceiveAsync(string worker, CancellationToken cancellationToken) { var receiveOn = DateTime.UtcNow; + if (_receiveSql == null) { _receiveSql = await SqlLoader.GetScript("Work.Receive"); @@ -90,7 +98,7 @@ 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", receiveOn.AddDays(-_daysToKeep)); // dynamic parameters _queues.Select((q, i) => cmd.Parameters.AddWithValue($"@Queue{i}", q)).ToList(); From 00fadec0dfa69ebd8973871a084e055209ca382f Mon Sep 17 00:00:00 2001 From: Jeff Repanich Date: Fri, 23 Nov 2018 16:22:29 -0500 Subject: [PATCH 2/2] code cleanup --- .../SqlServerQuidjiboConfiguration.cs | 2 +- .../Providers/SqlWorkProvider.cs | 19 +++++++++++++------ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/Quidjibo.SqlServer/Configurations/SqlServerQuidjiboConfiguration.cs b/src/Quidjibo.SqlServer/Configurations/SqlServerQuidjiboConfiguration.cs index 4f4d529..1c06d67 100644 --- a/src/Quidjibo.SqlServer/Configurations/SqlServerQuidjiboConfiguration.cs +++ b/src/Quidjibo.SqlServer/Configurations/SqlServerQuidjiboConfiguration.cs @@ -14,7 +14,7 @@ public class SqlServerQuidjiboConfiguration : IQuidjiboConfiguration /// /// The number of days to keep completed/faulted work items. /// - public int DaysToKeep { get; set; } = 2; + public int DaysToKeep { get; set; } = 3; public int BatchSize { get; set; } = 5; diff --git a/src/Quidjibo.SqlServer/Providers/SqlWorkProvider.cs b/src/Quidjibo.SqlServer/Providers/SqlWorkProvider.cs index eb223fa..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; @@ -7,7 +10,6 @@ using Microsoft.Extensions.Logging; using Quidjibo.Models; using Quidjibo.Providers; -using Quidjibo.SqlServer.Configurations; using Quidjibo.SqlServer.Extensions; using Quidjibo.SqlServer.Utils; @@ -24,10 +26,10 @@ public enum StatusFlags } private readonly int _batchSize; - private readonly int _daysToKeep; private readonly string _connectionString; - private readonly int _maxAttempts; + private readonly int _daysToKeep; private readonly ILogger _logger; + private readonly int _maxAttempts; private readonly string[] _queues; private readonly int _visibilityTimeout; private string _receiveSql; @@ -39,7 +41,7 @@ public SqlWorkProvider( int visibilityTimeout, int batchSize, int daysToKeep - ) + ) { _logger = logger; _queues = queues; @@ -77,6 +79,7 @@ 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) { @@ -98,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(-_daysToKeep)); + 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))