Skip to content

Commit

Permalink
Merge pull request #66 from smiggleworth/sql-config
Browse files Browse the repository at this point in the history
pass days to keep on config
  • Loading branch information
smiggleworth authored Nov 23, 2018
2 parents 9918b1d + 00fadec commit 83877d3
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -7,6 +10,12 @@ public class SqlServerQuidjiboConfiguration : IQuidjiboConfiguration
{
public int PollingInterval { get; set; } = 10;
public string ConnectionString { get; set; }

/// <summary>
/// The number of days to keep completed/faulted work items.
/// </summary>
public int DaysToKeep { get; set; } = 3;

public int BatchSize { get; set; } = 5;

/// <inheritdoc />
Expand Down
3 changes: 2 additions & 1 deletion src/Quidjibo.SqlServer/Factories/SqlWorkProviderFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ await SqlRunner.ExecuteAsync(async cmd =>
_sqlServerQuidjiboConfiguration.ConnectionString,
queues,
_sqlServerQuidjiboConfiguration.LockInterval,
_sqlServerQuidjiboConfiguration.BatchSize);
_sqlServerQuidjiboConfiguration.BatchSize,
_sqlServerQuidjiboConfiguration.DaysToKeep);
}
finally
{
Expand Down
21 changes: 18 additions & 3 deletions src/Quidjibo.SqlServer/Providers/SqlWorkProvider.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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)
Expand Down Expand Up @@ -70,6 +79,8 @@ await ExecuteAsync(async cmd =>
public async Task<List<WorkItem>> 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");
Expand All @@ -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))
Expand Down

0 comments on commit 83877d3

Please sign in to comment.