Interface ILockApi
Namespace: Casewhere.Runtime.DSL.Api
Assembly: Casewhere.Runtime.dll
Provides method to operate with shared resources on different processes
public interface ILockApi : IDslApi
Implements
Extension Methods
ObjectExtension.ConvertToBsonValue(object), EnumExtensions.DeepClone<ILockApi>(ILockApi)
Examples
var lockApi = ctx.Use<ILockApi>();
lockApi.Lock(lockId, waitingTime, expiredTime);
// do lock logic
lockApi.Release(lockId);
Methods
IsLocked(string)
Verify lockId is locking or not.
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
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.
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
void Release(string lockId)
Parameters
lockId
string
id of lock context