Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert using server time for distributed lock #383

Merged
merged 2 commits into from
Feb 8, 2024
Merged
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
3 changes: 3 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@

## Change log

### v1.10.2
- Dont use db server time for distributed lock timeout. Will addressed in a later release

### v1.10.1
- Use $addFields operator instead of $set when setting DistributedLock heartbeat for support mongo versions < 4.2

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Description />
<Version>1.10.1</Version>
<Version>1.10.2</Version>
<Description>MongoDB storage implementation for Hangfire (background job system for ASP.NET applications).</Description>
<Copyright>Copyright © 2014-2019 Sergey Zwezdin, Martin Lobger, Jonas Gottschau</Copyright>
<Authors>Sergey Zwezdin, Martin Lobger, Jonas Gottschau</Authors>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Description />
<Version>1.10.1</Version>
<Version>1.10.2</Version>
<Description>MongoDB storage implementation for Hangfire (background job system for ASP.NET applications).</Description>
<Copyright>Copyright © 2014-2019 Sergey Zwezdin, Martin Lobger, Jonas Gottschau</Copyright>
<Authors>Sergey Zwezdin, Martin Lobger, Jonas Gottschau</Authors>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<AssemblyName>Hangfire.Mongo.Sample.NETCore</AssemblyName>
<OutputType>Exe</OutputType>
<PackageId>Hangfire.Mongo.Sample.NETCore</PackageId>
<Version>1.10.1</Version>
<Version>1.10.2</Version>
<Description>MongoDB storage implementation for Hangfire (background job system for ASP.NET applications).</Description>
<Copyright>Copyright © 2014-2019 Sergey Zwezdin, Martin Lobger, Jonas Gottschau</Copyright>
<Authors>Sergey Zwezdin, Martin Lobger, Jonas Gottschau</Authors>
Expand Down
31 changes: 13 additions & 18 deletions src/Hangfire.Mongo/DistributedLock/MongoDistributedLock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -282,38 +282,33 @@ protected virtual void StartHeartBeat()

try
{
var pipeline = new BsonDocument[]
var filter = new BsonDocument
{
new BsonDocument("$match", new BsonDocument
{
[nameof(DistributedLockDto.Resource)] = _resource
}),
new BsonDocument("$addFields", new BsonDocument
[nameof(DistributedLockDto.Resource)] = _resource
};
var update = new BsonDocument
{
["$set"] = new BsonDocument
{
[nameof(DistributedLockDto.ExpireAt)] = new BsonDocument
{
["$add"] = new BsonArray
{
"$$NOW",
(int) _storageOptions.DistributedLockLifetime.TotalMilliseconds,
}
}
})
[nameof(DistributedLockDto.ExpireAt)] = DateTime
.UtcNow.Add(_storageOptions.DistributedLockLifetime)
}
};

Stopwatch sw = null;
if (Logger.IsTraceEnabled())
{
sw = Stopwatch.StartNew();
}

_dbContext.DistributedLock.Aggregate<BsonDocument>(pipeline).FirstOrDefault();
_dbContext.DistributedLock.UpdateOne(filter, update);

if (Logger.IsTraceEnabled() && sw != null)
{
var serializedModel = new Dictionary<string, BsonDocument>
{
["Filter"] = pipeline[0],
["Update"] = pipeline[1]
["Filter"] = filter,
["Update"] = update
};
sw.Stop();
var builder = new StringBuilder();
Expand Down
6 changes: 3 additions & 3 deletions src/Hangfire.Mongo/Hangfire.Mongo.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<VersionPrefix>1.10.1</VersionPrefix>
<VersionPrefix>1.10.2</VersionPrefix>
<TargetFramework>netstandard2.0</TargetFramework>
<NoWarn>$(NoWarn);CS0618</NoWarn>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
Expand All @@ -21,8 +21,8 @@
<owners>Sergey Zwezdin, Jonas Gottschau</owners>
<Description>MongoDB storage implementation for Hangfire (background job system for ASP.NET applications).</Description>
<PackageTags>Hangfire AspNet OWIN MongoDB CosmosDB Long-Running Background Fire-And-Forget Delayed Recurring Tasks Jobs Scheduler Threading Queues</PackageTags>
<PackageReleaseNotes>1.10.1
- Use $addFields operator instead of $set when setting DistributedLock heartbeat for support mongo versions &lt; 4.2
<PackageReleaseNotes>1.10.2
- Dont use db server time for distributed lock timeout. Will addressed in a later release
</PackageReleaseNotes>
<PackageReadmeFile>README.md</PackageReadmeFile>
<!--<PackageLicenseUrl>https://raw.githubusercontent.com/sergun/Hangfire.Mongo/master/LICENSE</PackageLicenseUrl>-->
Expand Down
11 changes: 10 additions & 1 deletion src/Hangfire.Mongo/MongoWriteOnlyTransaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,16 @@ protected virtual void ExecuteCommit(
List<WriteModel<BsonDocument>> writeModels,
BulkWriteOptions bulkWriteOptions)
{
jobGraph.BulkWrite(writeModels, bulkWriteOptions);
try
{
jobGraph.BulkWrite(writeModels, bulkWriteOptions);
}
catch (Exception e)
{
Logger.ErrorException("MongoWriteOnlyTransaction failed", e);
throw;
}

}

protected virtual void Log(IList<WriteModel<BsonDocument>> writeModels, long elapsedMilliseconds)
Expand Down
Loading