Class LockApi
Namespace: Casewhere.Runtime.DSL.Api
Assembly: Casewhere.Runtime.dll
public class LockApi : ILockApi, IDslApi
Inheritance
Implements
Inherited Members
object.ToString(), object.Equals(object), object.Equals(object, object), object.ReferenceEquals(object, object), object.GetHashCode(), object.GetType(), object.MemberwiseClone()
Extension Methods
ObjectExtension.ConvertToBsonValue(object), EnumExtensions.DeepClone<LockApi>(LockApi)
Constructors
LockApi(IDistributedLockProvider, IDataApi)
public LockApi(IDistributedLockProvider distributedLockProvider, IDataApi dataApi)
Parameters
distributedLockProvider IDistributedLockProvider
dataApi IDataApi
Methods
IsLocked(string)
Verify lockId is locking or not.
public bool IsLocked(string lockId)
Parameters
lockId string
id of lock context.
Returns
Return true if lockId is not released and else.
Lock(string, TimeSpan, TimeSpan)
Execute the lock context
public void Lock(string lockId, TimeSpan waitingTime, TimeSpan expiredTime)
Parameters
lockId string
id of lock context
waitingTime TimeSpan
The timeout of waiting for lock context
expiredTime TimeSpan
The timeout of lock context
Examples
var lockApi = ctx.Use<ILockApi>();
var objectId = "data object id";
var lockId = "key of lock context";
var expiredTime = TimeSpan.FromSeconds(30);
var waitingTime = TimeSpan.FromSeconds(30);
// check
if (dataObject.Get<int>("Count") > 0)
{
// lock
lockApi.Lock(lockId, waitingTime, expiredTime);
// check
dataObject = dataApi.Load(objectId);
if (dataObject.Get<int>("Count") > 0)
{
// do lock logic
// release
lockApi.Release(lockId);
}
}
LockAndCheck(string, DataSourceApiQuery, TimeSpan, TimeSpan)
Execute the lock context and verify the query. If the query result is not found, throw an exception and release the lock context.
public void LockAndCheck(string lockId, DataSourceApiQuery query, TimeSpan waitingTime, TimeSpan expiredTime)
Parameters
lockId string
id of lock context
query DataSourceApiQuery
The search query
waitingTime TimeSpan
The timeout of waiting for lock context
expiredTime TimeSpan
The timeout of lock context
Examples
var lockApi = ctx.Use<ILockApi>();
var objectId = "data object id";
var lockId = "key of lock context";
var expiredTime = TimeSpan.FromSeconds(30);
var waitingTime = TimeSpan.Zero;
var filter = FilterBuilder.Create().Eq("DepartmentId", ctx.Input.Id).Eq("Active", true).Build();
var query = DataSourceApiQuery.For("Employee")
.FilterBy(filter)
.ProjectOn("FirstName", "LastName")
.OrderBy("FirstName", true);
// lock and check
lockApi.LockAndCheck(lockId, query, waitingTime, expiredTime);
// do lock logic
// release
lockApi.Release(lockId);
Release(string)
Release lock context by lock id
public void Release(string lockId)
Parameters
lockId string
id of lock context