private ExecutionToken GetAssignableToken(ExecutionTokenList executionTokenList, SqlCommand command)
        {
            if (HasAvailableToken(executionTokenList))
            {
                return(GetAvailableToken(executionTokenList));
            }
            else
            {
                var executionIds = executionTokenList.Tokens.Where(x => x.Status != ExecutionTokenStatus.Disabled && !string.IsNullOrEmpty(x.GrantedToExecution))
                                   .Select(x => x.GrantedToExecution)
                                   .ToList();

                if (!executionIds.Any())
                {
                    return(null);
                }

                var executionStates  = GetTaskExecutionStates(executionIds, command);
                var expiredExecution = FindExpiredExecution(executionStates);
                if (expiredExecution == null)
                {
                    return(null);
                }

                return(executionTokenList.Tokens.First(x => x.GrantedToExecution == expiredExecution.TaskExecutionId));
            }
        }
        public static ExecutionTokenList ParseTokensString(string tokensString)
        {
            if (string.IsNullOrEmpty(tokensString))
            {
                return(ReturnDefaultTokenList());
            }

            var tokenList = new ExecutionTokenList();

            try
            {
                var tokens = tokensString.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (var tokenText in tokens)
                {
                    var token      = new ExecutionToken();
                    var tokenParts = tokenText.Split(',');
                    if (tokenParts.Length != 3)
                    {
                        throw new TokenFormatException("Token text not valid. Format is I:<id>,G:<granted TaskExecutionId>,S:<status> Invalid text: " + tokensString);
                    }

                    foreach (var part in tokenParts)
                    {
                        if (part.StartsWith("I:") && part.Length > 2)
                        {
                            token.TokenId = part.Substring(2);
                        }
                        else if (part.StartsWith("G:") && part.Length > 2)
                        {
                            token.GrantedToExecution = part.Substring(2);
                        }
                        else if (part.StartsWith("S:") && part.Length > 2)
                        {
                            token.Status = (ExecutionTokenStatus)int.Parse(part.Substring(2));
                        }
                        else
                        {
                            throw new TokenFormatException("Token text not valid. Format is I:<id>,G:<granted TaskExecutionId>,S:<status> Invalid text: " + tokensString);
                        }
                    }

                    tokenList.Tokens.Add(token);
                }
            }
            catch (TokenFormatException ex)
            {
                Trace.TraceError("Failed reading tokens text: " + tokensString + " " + ex);
                return(ReturnDefaultTokenList());
            }
            catch (FormatException ex)
            {
                Trace.TraceError("Failed reading tokens text: " + tokensString + " " + ex);
                return(ReturnDefaultTokenList());
            }

            return(tokenList);
        }
        private void SetTokenAsAvailable(ExecutionTokenList executionTokenList, string executionTokenId)
        {
            var executionToken = executionTokenList.Tokens.FirstOrDefault(x => x.TokenId == executionTokenId);

            if (executionToken != null && executionToken.Status == ExecutionTokenStatus.Unavailable)
            {
                executionToken.Status = ExecutionTokenStatus.Available;
            }
        }
예제 #4
0
        private async Task PersistTokensAsync(int taskDefinitionId, ExecutionTokenList executionTokenList, SqlCommand command)
        {
            var tokenString = GenerateTokenString(executionTokenList);

            command.Parameters.Clear();
            command.CommandText = TokensQueryBuilder.UpdateExecutionTokensQuery;
            command.Parameters.Add("@TaskDefinitionId", SqlDbType.Int).Value          = taskDefinitionId;
            command.Parameters.Add("@ExecutionTokens", SqlDbType.VarChar, 8000).Value = tokenString;
            await command.ExecuteNonQueryAsync().ConfigureAwait(false);
        }
        private void PersistTokens(int taskDefinitionId, ExecutionTokenList executionTokenList, SqlCommand command)
        {
            var tokenString = GenerateTokenString(executionTokenList);

            command.Parameters.Clear();
            command.CommandText = TokensQueryBuilder.UpdateExecutionTokensQuery;
            command.Parameters.Add("@TaskDefinitionId", SqlDbType.Int).Value          = taskDefinitionId;
            command.Parameters.Add("@ExecutionTokens", SqlDbType.VarChar, 8000).Value = tokenString;
            command.ExecuteNonQuery();
        }
        private static ExecutionTokenList ReturnDefaultTokenList()
        {
            var list = new ExecutionTokenList();

            list.Tokens.Add(new ExecutionToken()
            {
                TokenId = Guid.NewGuid().ToString(),
                Status  = ExecutionTokenStatus.Available
            });

            return(list);
        }
        private string GenerateTokenString(ExecutionTokenList executionTokenList)
        {
            var sb      = new StringBuilder();
            int counter = 0;

            foreach (var token in executionTokenList.Tokens)
            {
                if (counter > 0)
                {
                    sb.Append("|");
                }

                sb.Append("I:");
                sb.Append(token.TokenId);
                sb.Append(",S:");
                sb.Append(((int)token.Status).ToString());
                sb.Append(",G:");
                sb.Append(token.GrantedToExecution);

                counter++;
            }

            return(sb.ToString());
        }
        public static ExecutionTokenList ParseTokensString(string tokensString)
        {
            if (string.IsNullOrEmpty(tokensString))
                return ReturnDefaultTokenList();

            var tokenList = new ExecutionTokenList();

            try
            {
                var tokens = tokensString.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (var tokenText in tokens)
                {
                    var token = new ExecutionToken();
                    var tokenParts = tokenText.Split(',');
                    if (tokenParts.Length != 3)
                        throw new TokenFormatException("Token text not valid. Format is I:<id>,G:<granted TaskExecutionId>,S:<status> Invalid text: " + tokensString);

                    foreach (var part in tokenParts)
                    {
                        if (part.StartsWith("I:") && part.Length > 2)
                            token.TokenId = part.Substring(2);
                        else if (part.StartsWith("G:") && part.Length > 2)
                            token.GrantedToExecution = part.Substring(2);
                        else if (part.StartsWith("S:") && part.Length > 2)
                            token.Status = (ExecutionTokenStatus)int.Parse(part.Substring(2));
                        else
                            throw new TokenFormatException("Token text not valid. Format is I:<id>,G:<granted TaskExecutionId>,S:<status> Invalid text: " + tokensString);
                    }

                    tokenList.Tokens.Add(token);
                }
            }
            catch (TokenFormatException ex)
            {
                Trace.TraceError("Failed reading tokens text: " + tokensString + " " + ex);
                return ReturnDefaultTokenList();
            }
            catch (FormatException ex)
            {
                Trace.TraceError("Failed reading tokens text: " + tokensString + " " + ex);
                return ReturnDefaultTokenList();
            }

            return tokenList;
        }
 private ExecutionToken GetAvailableToken(ExecutionTokenList executionTokenList)
 {
     return(executionTokenList.Tokens.FirstOrDefault(x => x.Status == ExecutionTokenStatus.Available ||
                                                     x.Status == ExecutionTokenStatus.Unlimited));
 }
 private bool HasAvailableToken(ExecutionTokenList executionTokenList)
 {
     return(executionTokenList.Tokens.Any(x => x.Status == ExecutionTokenStatus.Available ||
                                          x.Status == ExecutionTokenStatus.Unlimited));
 }
        private static ExecutionTokenList ReturnDefaultTokenList()
        {
            var list = new ExecutionTokenList();
            list.Tokens.Add(new ExecutionToken()
            {
                TokenId = Guid.NewGuid().ToString(),
                Status = ExecutionTokenStatus.Available
            });

            return list;
        }
        private bool AdjustTokenCount(ExecutionTokenList tokenList, int concurrencyCount)
        {
            bool modified = false;

            if (concurrencyCount == -1 || concurrencyCount == 0) // if there is no limit
            {
                if (tokenList.Tokens.Count != 1 || (tokenList.Tokens.Count == 1 && tokenList.Tokens.All(x => x.Status != ExecutionTokenStatus.Unlimited)))
                {
                    tokenList.Tokens.Clear();
                    tokenList.Tokens.Add(new ExecutionToken()
                    {
                        TokenId            = Guid.NewGuid().ToString(),
                        Status             = ExecutionTokenStatus.Unlimited,
                        GrantedToExecution = "0"
                    });

                    modified = true;
                }
            }
            else
            {
                // if has a limit then remove any unlimited tokens
                if (tokenList.Tokens.Any(x => x.Status == ExecutionTokenStatus.Unlimited))
                {
                    tokenList.Tokens = tokenList.Tokens.Where(x => x.Status != ExecutionTokenStatus.Unlimited).ToList();
                    modified         = true;
                }

                // the current token count is less than the limit then add new tokens
                if (tokenList.Tokens.Count < concurrencyCount)
                {
                    while (tokenList.Tokens.Count < concurrencyCount)
                    {
                        tokenList.Tokens.Add(new ExecutionToken()
                        {
                            TokenId            = Guid.NewGuid().ToString(),
                            Status             = ExecutionTokenStatus.Available,
                            GrantedToExecution = "0"
                        });

                        modified = true;
                    }
                }
                // if the current token count is greater than the limit then
                // start removing tokens. Remove Available tokens preferentially.
                else if (tokenList.Tokens.Count > concurrencyCount)
                {
                    while (tokenList.Tokens.Count > concurrencyCount)
                    {
                        if (tokenList.Tokens.Any(x => x.Status == ExecutionTokenStatus.Available))
                        {
                            var firstAvailable = tokenList.Tokens.First(x => x.Status == ExecutionTokenStatus.Available);
                            tokenList.Tokens.Remove(firstAvailable);
                        }
                        else
                        {
                            tokenList.Tokens.Remove(tokenList.Tokens.First());
                        }

                        modified = true;
                    }
                }
            }

            return(modified);
        }
        private bool AdjustTokenCount(ExecutionTokenList tokenList, int concurrencyCount)
        {
            bool modified = false;

            if (concurrencyCount == -1 || concurrencyCount == 0) // if there is no limit
            {
                if (tokenList.Tokens.Count != 1 || (tokenList.Tokens.Count == 1 && tokenList.Tokens.All(x => x.Status != ExecutionTokenStatus.Unlimited)))
                {
                    tokenList.Tokens.Clear();
                    tokenList.Tokens.Add(new ExecutionToken()
                    {
                        TokenId = Guid.NewGuid().ToString(),
                        Status = ExecutionTokenStatus.Unlimited,
                        GrantedToExecution = "0"
                    });

                    modified = true;
                }
            }
            else
            {
                // if has a limit then remove any unlimited tokens
                if (tokenList.Tokens.Any(x => x.Status == ExecutionTokenStatus.Unlimited))
                {
                    tokenList.Tokens = tokenList.Tokens.Where(x => x.Status != ExecutionTokenStatus.Unlimited).ToList();
                    modified = true;
                }

                // the current token count is less than the limit then add new tokens
                if (tokenList.Tokens.Count < concurrencyCount)
                {
                    while (tokenList.Tokens.Count < concurrencyCount)
                    {
                        tokenList.Tokens.Add(new ExecutionToken()
                        {
                            TokenId = Guid.NewGuid().ToString(),
                            Status = ExecutionTokenStatus.Available,
                            GrantedToExecution = "0"
                        });

                        modified = true;
                    }
                }
                // if the current token count is greater than the limit then
                // start removing tokens. Remove Available tokens preferentially.
                else if (tokenList.Tokens.Count > concurrencyCount)
                {
                    while (tokenList.Tokens.Count > concurrencyCount)
                    {
                        if (tokenList.Tokens.Any(x => x.Status == ExecutionTokenStatus.Available))
                        {
                            var firstAvailable = tokenList.Tokens.First(x => x.Status == ExecutionTokenStatus.Available);
                            tokenList.Tokens.Remove(firstAvailable);
                        }
                        else
                        {
                            tokenList.Tokens.Remove(tokenList.Tokens.First());
                        }

                        modified = true;
                    }
                }
            }

            return modified;
        }
        private ExecutionToken GetAssignableToken(ExecutionTokenList executionTokenList, SqlCommand command)
        {
            if (HasAvailableToken(executionTokenList))
            {
                return GetAvailableToken(executionTokenList);
            }
            else
            {
                var executionIds = executionTokenList.Tokens.Where(x => x.Status != ExecutionTokenStatus.Disabled && !string.IsNullOrEmpty(x.GrantedToExecution))
                    .Select(x => x.GrantedToExecution)
                    .ToList();

                if (!executionIds.Any())
                    return null;

                var executionStates = GetTaskExecutionStates(executionIds, command);
                var expiredExecution = FindExpiredExecution(executionStates);
                if (expiredExecution == null)
                    return null;

                return executionTokenList.Tokens.First(x => x.GrantedToExecution == expiredExecution.TaskExecutionId);
            }
        }
 private void SetTokenAsAvailable(ExecutionTokenList executionTokenList, string executionTokenId)
 {
     var executionToken = executionTokenList.Tokens.FirstOrDefault(x => x.TokenId == executionTokenId);
     if (executionToken != null && executionToken.Status == ExecutionTokenStatus.Unavailable)
         executionToken.Status = ExecutionTokenStatus.Available;
 }
        private string GenerateTokenString(ExecutionTokenList executionTokenList)
        {
            var sb = new StringBuilder();
            int counter = 0;
            foreach (var token in executionTokenList.Tokens)
            {
                if (counter > 0)
                    sb.Append("|");

                sb.Append("I:");
                sb.Append(token.TokenId);
                sb.Append(",S:");
                sb.Append(((int)token.Status).ToString());
                sb.Append(",G:");
                sb.Append(token.GrantedToExecution);

                counter++;
            }

            return sb.ToString();
        }
        private void PersistTokens(int taskDefinitionId, ExecutionTokenList executionTokenList, SqlCommand command)
        {
            var tokenString = GenerateTokenString(executionTokenList);

            command.Parameters.Clear();
            command.CommandText = TokensQueryBuilder.UpdateExecutionTokensQuery;
            command.Parameters.Add("@TaskDefinitionId", SqlDbType.Int).Value = taskDefinitionId;
            command.Parameters.Add("@ExecutionTokens", SqlDbType.VarChar, 8000).Value = tokenString;
            command.ExecuteNonQuery();
        }
 private ExecutionToken GetAvailableToken(ExecutionTokenList executionTokenList)
 {
     return executionTokenList.Tokens.FirstOrDefault(x => x.Status == ExecutionTokenStatus.Available
                                                          || x.Status == ExecutionTokenStatus.Unlimited);
 }
 private bool HasAvailableToken(ExecutionTokenList executionTokenList)
 {
     return executionTokenList.Tokens.Any(x => x.Status == ExecutionTokenStatus.Available
                                                          || x.Status == ExecutionTokenStatus.Unlimited);
 }