Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions src/Hangfire.PostgreSql/CountersAggregator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
using Hangfire.Common;
using Hangfire.Logging;
using Hangfire.Server;
using Utility = Hangfire.PostgreSql.Utils.Utils;

namespace Hangfire.PostgreSql
{
Expand Down Expand Up @@ -81,27 +82,33 @@ public void Execute(CancellationToken cancellationToken)
private string GetAggregationQuery()
{
string schemaName = _storage.Options.SchemaName;

return
$"""
BEGIN;

INSERT INTO "{schemaName}"."aggregatedcounter" ("key", "value", "expireat")
INSERT INTO "{schemaName}"."{TableNameHandler("aggregatedcounter")}" ("key", "value", "expireat")
SELECT
"key",
SUM("value"),
MAX("expireat")
FROM "{schemaName}"."counter"
FROM "{schemaName}"."{TableNameHandler("counter")}"
GROUP BY "key"
ON CONFLICT("key") DO UPDATE
SET "value" = "aggregatedcounter"."value" + EXCLUDED."value", "expireat" = EXCLUDED."expireat";
SET "value" = "{schemaName}"."{TableNameHandler("aggregatedcounter")}"."value" + EXCLUDED."value", "expireat" = EXCLUDED."expireat";

DELETE FROM "{schemaName}"."counter"
DELETE FROM "{schemaName}"."{TableNameHandler("counter")}"
WHERE "key" IN (
SELECT "key" FROM "{schemaName}"."aggregatedcounter"
SELECT "key" FROM "{schemaName}"."{TableNameHandler("aggregatedcounter")}"
);

COMMIT;
""";
}

private string TableNameHandler(string baseName)
{
return Utility.GetTableName(baseName, _storage.Options.UseTablePrefix, _storage.Options.TablePrefixName);
}
}
}
14 changes: 10 additions & 4 deletions src/Hangfire.PostgreSql/ExpirationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
using Hangfire.Logging;
using Hangfire.Server;
using Hangfire.Storage;
using Utility = Hangfire.PostgreSql.Utils.Utils;

namespace Hangfire.PostgreSql
{
Expand Down Expand Up @@ -82,10 +83,10 @@ public void Execute(CancellationToken cancellationToken)
{
using IDbTransaction transaction = connection.BeginTransaction();
removedCount = connection.Execute($@"
DELETE FROM ""{_storage.Options.SchemaName}"".""{table}""
DELETE FROM ""{_storage.Options.SchemaName}"".""{TableNameHandler(table)}""
WHERE ""id"" IN (
SELECT ""id""
FROM ""{_storage.Options.SchemaName}"".""{table}""
FROM ""{_storage.Options.SchemaName}"".""{TableNameHandler(table)}""
WHERE ""expireat"" < NOW()
LIMIT {_storage.Options.DeleteExpiredBatchSize.ToString(CultureInfo.InvariantCulture)}
)", transaction: transaction);
Expand Down Expand Up @@ -129,7 +130,7 @@ private void AggregateCounter(string counterName)
using IDbTransaction transaction = connection.BeginTransaction(IsolationLevel.ReadCommitted);
string aggregateQuery = $@"
WITH ""counters"" AS (
DELETE FROM ""{_storage.Options.SchemaName}"".""counter""
DELETE FROM ""{_storage.Options.SchemaName}"".""{TableNameHandler("counter")}""
WHERE ""key"" = @Key
AND ""expireat"" IS NULL
RETURNING *
Expand All @@ -143,7 +144,7 @@ DELETE FROM ""{_storage.Options.SchemaName}"".""counter""

if (aggregatedValue > 0)
{
string insertQuery = $@"INSERT INTO ""{_storage.Options.SchemaName}"".""counter""(""key"", ""value"") VALUES (@Key, @Value);";
string insertQuery = $@"INSERT INTO ""{_storage.Options.SchemaName}"".""{TableNameHandler("counter")}""(""key"", ""value"") VALUES (@Key, @Value);";
connection.Execute(insertQuery, new { Key = counterName, Value = aggregatedValue });
}
});
Expand Down Expand Up @@ -176,5 +177,10 @@ private void UseConnectionDistributedLock(PostgreSqlStorage storage, Action<IDbC
e);
}
}

private string TableNameHandler(string baseName)
{
return Utility.GetTableName(baseName, _storage.Options.UseTablePrefix, _storage.Options.TablePrefixName);
}
}
}
8 changes: 4 additions & 4 deletions src/Hangfire.PostgreSql/Hangfire.PostgreSql.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<Description>PostgreSql storage implementation for Hangfire (background job system for ASP.NET and aspnet core applications).</Description>
<Copyright>Copyright © 2014-2024 Frank Hommers and others</Copyright>
<AssemblyTitle>Hangfire PostgreSql Storage</AssemblyTitle>
<VersionPrefix>1.9.4</VersionPrefix>
<VersionPrefix>1.9.5-tablePrefix</VersionPrefix>
<Authors>Frank Hommers, Vytautas Kasparavičius, Žygimantas Arūna</Authors>
<TargetFramework>netstandard2.0</TargetFramework>
<AssemblyName>Hangfire.PostgreSql</AssemblyName>
Expand All @@ -14,9 +14,9 @@
<PackageReleaseNotes>https://github.com/frankhommers/Hangfire.PostgreSql/releases</PackageReleaseNotes>
<PackageProjectUrl>http://hmm.rs/Hangfire.PostgreSql</PackageProjectUrl>
<PackageLicenseUrl></PackageLicenseUrl>
<Version>1.9.4.0</Version>
<FileVersion>1.9.4.0</FileVersion>
<AssemblyVersion>1.9.4.0</AssemblyVersion>
<Version>1.9.5-tablePrefix</Version>
<FileVersion>1.9.5-tablePrefix</FileVersion>
<AssemblyVersion>1.9.5-tablePrefix</AssemblyVersion>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<PackageLicenseFile>LICENSE.md</PackageLicenseFile>
<RepositoryUrl>https://github.com/frankhommers/Hangfire.PostgreSql</RepositoryUrl>
Expand Down
Loading