Skip to content

Commit

Permalink
Merge pull request #332 from Tinyakov/fix/TransactionScope-enlistment
Browse files Browse the repository at this point in the history
Fix new jobs signalling in case of ambient transaction #331
  • Loading branch information
azygis committed Oct 18, 2023
2 parents ae22240 + cf9eab8 commit 2a709e4
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/Hangfire.PostgreSql/PostgreSqlWriteOnlyTransaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,26 @@ public PostgreSqlWriteOnlyTransaction(
public override void Commit()
{
_storage.UseTransaction(_dedicatedConnectionFunc(), (connection, _) => {
RegisterNewJobsEventWithTransactionCompletedEvent();
foreach (Action<IDbConnection> command in _commandQueue)
{
command(connection);
}
}, CreateTransactionScope);

// Triggers signals for all queues to which jobs have been added in this transaction
_queuesWithAddedJobs.ForEach(PostgreSqlJobQueue._queueEventRegistry.Set);
_queuesWithAddedJobs.Clear();
}

private void RegisterNewJobsEventWithTransactionCompletedEvent()
{
// TransactionCompleted event is required here, because if this TransactionScope is enlisted
// within an ambient TransactionScope, the ambient TransactionScope controls when the TransactionScope completes.
Transaction.Current.TransactionCompleted += (_, args) => {
if (args.Transaction.TransactionInformation.Status == TransactionStatus.Committed)
{
// Triggers signals for all queues to which jobs have been added in this transaction
_queuesWithAddedJobs.ForEach(PostgreSqlJobQueue._queueEventRegistry.Set);
_queuesWithAddedJobs.Clear();
}
};
}

private TransactionScope CreateTransactionScope()
Expand Down

0 comments on commit 2a709e4

Please sign in to comment.