private async Task <CriticalSectionState> GetCriticalSectionStateAsync(int taskDefinitionId, CriticalSectionType criticalSectionType, SqlCommand command) { command.Parameters.Clear(); if (criticalSectionType == CriticalSectionType.User) { command.CommandText = TokensQueryBuilder.GetUserCriticalSectionStateQuery; } else { command.CommandText = TokensQueryBuilder.GetClientCriticalSectionStateQuery; } command.Parameters.Add("@TaskDefinitionId", SqlDbType.Int).Value = taskDefinitionId; using (var reader = await command.ExecuteReaderAsync().ConfigureAwait(false)) { var readSuccess = await reader.ReadAsync().ConfigureAwait(false); if (readSuccess) { var csState = new CriticalSectionState(); csState.IsGranted = int.Parse(reader[GetCsStatusColumnName(criticalSectionType)].ToString()) == 0; csState.GrantedToExecution = reader[GetGrantedToColumnName(criticalSectionType)].ToString(); csState.SetQueue(reader[GetQueueColumnName(criticalSectionType)].ToString()); csState.StartTrackingModifications(); return(csState); } } throw new CriticalSectionException("No Task exists with id " + taskDefinitionId); }
private void CleanseCurrentGranteeIfExpired(CriticalSectionState csState, List <TaskExecutionState> taskExecutionStates) { if (!HasEmptyGranteeValue(csState) && csState.IsGranted) { var csStateOfGranted = taskExecutionStates.First(x => x.TaskExecutionId == csState.GrantedToExecution); if (HasCriticalSectionExpired(csStateOfGranted)) { csState.IsGranted = false; } } }
private async Task CleanseOfExpiredExecutionsAsync(CriticalSectionState csState, SqlCommand command) { var csQueue = csState.GetQueue(); var activeExecutionIds = GetActiveTaskExecutionIds(csState); if (activeExecutionIds.Any()) { var taskExecutionStates = await GetTaskExecutionStatesAsync(activeExecutionIds, command).ConfigureAwait(false); CleanseCurrentGranteeIfExpired(csState, taskExecutionStates); CleanseQueueOfExpiredExecutions(csState, taskExecutionStates, csQueue); } }
private void CleanseOfExpiredExecutions(CriticalSectionState csState, SqlCommand command) { var csQueue = csState.GetQueue(); var activeExecutionIds = GetActiveTaskExecutionIds(csState); if (activeExecutionIds.Any()) { var taskExecutionStates = GetTaskExecutionStates(activeExecutionIds, command); CleanseCurrentGranteeIfExpired(csState, taskExecutionStates); CleanseQueueOfExpiredExecutions(csState, taskExecutionStates, csQueue); } }
private List <string> GetActiveTaskExecutionIds(CriticalSectionState csState) { var taskExecutionIds = new List <string>(); if (!HasEmptyGranteeValue(csState)) { taskExecutionIds.Add(csState.GrantedToExecution); } if (csState.HasQueuedExecutions()) { taskExecutionIds.AddRange(csState.GetQueue().Select(x => x.TaskExecutionId)); } return(taskExecutionIds); }
private async Task UpdateCriticalSectionStateAsync(int taskDefinitionId, CriticalSectionState csState, CriticalSectionType criticalSectionType, SqlCommand command) { command.Parameters.Clear(); if (criticalSectionType == CriticalSectionType.User) { command.CommandText = TokensQueryBuilder.SetUserCriticalSectionStateQuery; } else { command.CommandText = TokensQueryBuilder.SetClientCriticalSectionStateQuery; } command.Parameters.Add("@TaskDefinitionId", SqlDbType.Int).Value = taskDefinitionId; command.Parameters.Add("@CsStatus", SqlDbType.Int).Value = csState.IsGranted ? 1 : 0; command.Parameters.Add("@CsTaskExecutionId", SqlDbType.Int).Value = csState.GrantedToExecution; command.Parameters.Add("@CsQueue", SqlDbType.VarChar, 8000).Value = csState.GetQueueString(); await command.ExecuteNonQueryAsync().ConfigureAwait(false); }
private void CleanseQueueOfExpiredExecutions(CriticalSectionState csState, List <TaskExecutionState> taskExecutionStates, List <CriticalSectionQueueItem> csQueue) { var validQueuedExecutions = (from tes in taskExecutionStates join q in csQueue on tes.TaskExecutionId equals q.TaskExecutionId where HasCriticalSectionExpired(tes) == false select q).ToList(); if (validQueuedExecutions.Count != csQueue.Count) { var updatedQueue = new List <CriticalSectionQueueItem>(); int newQueueIndex = 1; foreach (var validQueuedExecution in validQueuedExecutions.OrderBy(x => x.Index)) { updatedQueue.Add(new CriticalSectionQueueItem(newQueueIndex, validQueuedExecution.TaskExecutionId)); } csState.UpdateQueue(updatedQueue); } }
private void UpdateCriticalSectionState(int taskDefinitionId, CriticalSectionState csState, CriticalSectionType criticalSectionType, SqlCommand command) { command.Parameters.Clear(); if (criticalSectionType == CriticalSectionType.User) command.CommandText = TokensQueryBuilder.SetUserCriticalSectionStateQuery; else command.CommandText = TokensQueryBuilder.SetClientCriticalSectionStateQuery; command.Parameters.Add("@TaskDefinitionId", SqlDbType.Int).Value = taskDefinitionId; command.Parameters.Add("@CsStatus", SqlDbType.Int).Value = csState.IsGranted ? 1 : 0; command.Parameters.Add("@CsTaskExecutionId", SqlDbType.Int).Value = csState.GrantedToExecution; command.Parameters.Add("@CsQueue", SqlDbType.VarChar, 8000).Value = csState.GetQueueString(); command.ExecuteNonQuery(); }
private void GrantCriticalSection(CriticalSectionState csState, int taskDefinitionId, string taskExecutionId, SqlCommand command) { csState.IsGranted = true; csState.GrantedToExecution = taskExecutionId; }
private bool HasEmptyGranteeValue(CriticalSectionState csState) { return(string.IsNullOrEmpty(csState.GrantedToExecution) || csState.GrantedToExecution.Equals("0")); }
private void CleanseOfExpiredExecutions(CriticalSectionState csState, SqlCommand command) { var csQueue = csState.GetQueue(); var activeExecutionIds = GetActiveTaskExecutionIds(csState); if (activeExecutionIds.Any()) { var taskExecutionStates = GetTaskExecutionStates(activeExecutionIds, command); CleanseCurrentGranteeIfExpired(csState, taskExecutionStates); CleanseQueueOfExpiredExecutions(csState, taskExecutionStates, csQueue); } }
private List<string> GetActiveTaskExecutionIds(CriticalSectionState csState) { var taskExecutionIds = new List<string>(); if (!HasEmptyGranteeValue(csState)) taskExecutionIds.Add(csState.GrantedToExecution); if (csState.HasQueuedExecutions()) taskExecutionIds.AddRange(csState.GetQueue().Select(x => x.TaskExecutionId)); return taskExecutionIds; }
private void CleanseCurrentGranteeIfExpired(CriticalSectionState csState, List<TaskExecutionState> taskExecutionStates) { if (!HasEmptyGranteeValue(csState) && csState.IsGranted) { var csStateOfGranted = taskExecutionStates.First(x => x.TaskExecutionId == csState.GrantedToExecution); if (HasCriticalSectionExpired(csStateOfGranted)) { csState.IsGranted = false; } } }
private void CleanseQueueOfExpiredExecutions(CriticalSectionState csState, List<TaskExecutionState> taskExecutionStates, List<CriticalSectionQueueItem> csQueue) { var validQueuedExecutions = (from tes in taskExecutionStates join q in csQueue on tes.TaskExecutionId equals q.TaskExecutionId where HasCriticalSectionExpired(tes) == false select q).ToList(); if (validQueuedExecutions.Count != csQueue.Count) { var updatedQueue = new List<CriticalSectionQueueItem>(); int newQueueIndex = 1; foreach (var validQueuedExecution in validQueuedExecutions.OrderBy(x => x.Index)) updatedQueue.Add(new CriticalSectionQueueItem(newQueueIndex, validQueuedExecution.TaskExecutionId)); csState.UpdateQueue(updatedQueue); } }
private CriticalSectionState GetCriticalSectionState(int taskDefinitionId, CriticalSectionType criticalSectionType, SqlCommand command) { command.Parameters.Clear(); if (criticalSectionType == CriticalSectionType.User) command.CommandText = TokensQueryBuilder.GetUserCriticalSectionStateQuery; else command.CommandText = TokensQueryBuilder.GetClientCriticalSectionStateQuery; command.Parameters.Add("@TaskDefinitionId", SqlDbType.Int).Value = taskDefinitionId; using (var reader = command.ExecuteReader()) { var readSuccess = reader.Read(); if (readSuccess) { var csState = new CriticalSectionState(); csState.IsGranted = int.Parse(reader[GetCsStatusColumnName(criticalSectionType)].ToString()) == 0; csState.GrantedToExecution = reader[GetGrantedToColumnName(criticalSectionType)].ToString(); csState.SetQueue(reader[GetQueueColumnName(criticalSectionType)].ToString()); csState.StartTrackingModifications(); return csState; } } throw new CriticalSectionException("No Task exists with id " + taskDefinitionId); }
private void GrantCriticalSection(CriticalSectionState csState, int taskDefinitionId, string taskExecutionId, SqlCommand command) { csState.IsGranted = true; csState.GrantedToExecution = taskExecutionId; }
private bool HasEmptyGranteeValue(CriticalSectionState csState) { return string.IsNullOrEmpty(csState.GrantedToExecution) || csState.GrantedToExecution.Equals("0"); }