Skip to content

Commit ac81be8

Browse files
committed
Added support for DeleteExpiredBatchSize in PostgreSqlStorageOptions to make it configurable from the default of 1000 items
This setting determines how many expired items we attempt to delete from our Hangfire processing tables per run
1 parent 40d8416 commit ac81be8

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

src/Hangfire.PostgreSql/ExpirationManager.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ namespace Hangfire.PostgreSql
3232
internal class ExpirationManager : IBackgroundProcess, IServerComponent
3333
{
3434
private static readonly TimeSpan DelayBetweenPasses = TimeSpan.FromSeconds(1);
35-
private const int NumberOfRecordsInSinglePass = 1000;
3635

3736
private static readonly ILog Logger = LogProvider.GetLogger(typeof(ExpirationManager));
3837

@@ -93,7 +92,7 @@ DELETE FROM """ + _options.SchemaName + @""".""{0}""
9392
FROM """ + _options.SchemaName + @""".""{0}""
9493
WHERE ""expireat"" < NOW() AT TIME ZONE 'UTC'
9594
LIMIT {1}
96-
)", table, NumberOfRecordsInSinglePass.ToString(CultureInfo.InvariantCulture)), transaction);
95+
)", table, _options.DeleteExpiredBatchSize.ToString(CultureInfo.InvariantCulture)), transaction);
9796

9897
transaction.Commit();
9998
}

src/Hangfire.PostgreSql/PostgreSqlStorageOptions.cs

+22-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public class PostgreSqlStorageOptions
3030
private TimeSpan _distributedLockTimeout;
3131
private TimeSpan _transactionSerializationTimeout;
3232
private TimeSpan _jobExpirationCheckInterval;
33+
private int _deleteExpiredBatchSize;
3334

3435
public PostgreSqlStorageOptions()
3536
{
@@ -41,6 +42,7 @@ public PostgreSqlStorageOptions()
4142
SchemaName = "hangfire";
4243
UseNativeDatabaseTransactions = true;
4344
PrepareSchemaIfNecessary = true;
45+
DeleteExpiredBatchSize = 1000;
4446
}
4547

4648
public TimeSpan QueuePollInterval
@@ -93,7 +95,20 @@ public TimeSpan JobExpirationCheckInterval
9395
}
9496
}
9597

96-
public bool UseNativeDatabaseTransactions { get; set; }
98+
/// <summary>
99+
/// Gets or sets the number of records deleted in a single batch in expiration manager
100+
/// </summary>
101+
public int DeleteExpiredBatchSize
102+
{
103+
get => _deleteExpiredBatchSize;
104+
set
105+
{
106+
ThrowIfValueIsNotPositive(value, nameof(DeleteExpiredBatchSize));
107+
_deleteExpiredBatchSize = value;
108+
}
109+
}
110+
111+
public bool UseNativeDatabaseTransactions { get; set; }
97112
public bool PrepareSchemaIfNecessary { get; set; }
98113
public string SchemaName { get; set; }
99114
public bool EnableTransactionScopeEnlistment { get; set; }
@@ -111,5 +126,11 @@ private static void ThrowIfValueIsNotPositive(TimeSpan value, string fieldName)
111126
throw new ArgumentException(message, nameof(value));
112127
}
113128
}
129+
130+
private static void ThrowIfValueIsNotPositive(int value, string fieldName)
131+
{
132+
if (value < 0)
133+
throw new ArgumentException($"The {fieldName} property value should be positive. Given: {value}.");
134+
}
114135
}
115136
}

0 commit comments

Comments
 (0)