-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prevent multiple instances of SyncAll* functions running concurrently
Should fix PTCD-378 issues running in prod.
- Loading branch information
Showing
5 changed files
with
117 additions
and
25 deletions.
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
src/Dfc.CourseDirectory.Core/DataStore/Sql/Queries/GetExclusiveLock.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace Dfc.CourseDirectory.Core.DataStore.Sql.Queries | ||
{ | ||
public class GetExclusiveLock : ISqlQuery<bool> | ||
{ | ||
public string Name { get; set; } | ||
public int TimeoutMilliseconds { get; set; } | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
src/Dfc.CourseDirectory.Core/DataStore/Sql/QueryHandlers/GetExclusiveLockHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using System.Data; | ||
using System.Data.SqlClient; | ||
using System.Threading.Tasks; | ||
using Dapper; | ||
using Dfc.CourseDirectory.Core.DataStore.Sql.Queries; | ||
|
||
namespace Dfc.CourseDirectory.Core.DataStore.Sql.QueryHandlers | ||
{ | ||
public class GetExclusiveLockHandler : ISqlQueryHandler<GetExclusiveLock, bool> | ||
{ | ||
public async Task<bool> Execute(SqlTransaction transaction, GetExclusiveLock query) | ||
{ | ||
var param = new DynamicParameters(new | ||
{ | ||
Resource = query.Name, | ||
LockMode = "Exclusive", | ||
LockTimeout = query.TimeoutMilliseconds | ||
}); | ||
|
||
param.Add(name: "@RetVal", dbType: DbType.Int32, direction: ParameterDirection.ReturnValue); | ||
|
||
await transaction.Connection.ExecuteAsync( | ||
"sp_getapplock", | ||
param, | ||
transaction, | ||
commandTimeout: 0, | ||
commandType: CommandType.StoredProcedure); | ||
|
||
var result = param.Get<int>("@RetVal"); | ||
|
||
// See https://docs.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sp-getapplock-transact-sql?view=sql-server-ver15#return-code-values | ||
return result switch | ||
{ | ||
0 => true, | ||
1 => true, | ||
_ => false | ||
}; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters