コード例 #1
0
ファイル: FolderUtility.cs プロジェクト: Jiahax/Common
        public static void ForceDeleteFile(string filePath)
        {
            Guard.ArgumentNotNullOrEmpty(filePath, nameof(filePath));

            try
            {
                File.SetAttributes(filePath, FileAttributes.Normal);
                File.Delete(filePath);
            }
            catch (Exception ex)
            {
                TraceEx.TraceWarning($"Delete File {filePath} failed.", ex);
            }
        }
コード例 #2
0
ファイル: RetryHelper.cs プロジェクト: Jiahax/Common
        public static async Task RetryAsync(
            Func <CancellationToken, Task> func,
            Func <Exception, bool>[] errorHandlers,
            TimeSpan[] timeouts,
            CancellationToken cancellationToken  = default(CancellationToken),
            [CallerMemberName] string methodName = null,
            [CallerFilePath] string filePath     = null,
            [CallerLineNumber] int lineNumber    = 0)
        {
            Guard.ArgumentNotNull(func, nameof(func));
            ValidateRetryParameters(errorHandlers, timeouts);

            int retryCount = 0;

            while (true)
            {
                cancellationToken.ThrowIfCancellationRequested();
                if (retryCount > 0)
                {
                    await Task.Delay(timeouts[retryCount - 1], cancellationToken);
                }
                try
                {
                    await func(cancellationToken);

                    break;
                }
                catch (Exception ex)
                {
                    if (retryCount++ < timeouts.Length)
                    {
                        if (errorHandlers.Any(handler => handler(ex)))
                        {
                            TraceEx.TraceWarning($"Error occurred at method {methodName} in {filePath} line {lineNumber}, current retry count: {retryCount}", ex);
                            continue;
                        }
                    }
                    throw;
                }
            }
        }
コード例 #3
0
        private void Refresh(IReadOnlyDictionary <string, object> config, bool silent)
        {
            object value;

            if (config.TryGetValue(_key, out value))
            {
                try
                {
                    T typedValue = _converter != null?_converter(value) : (T)value;

                    if (!object.Equals(Value, typedValue))
                    {
                        TraceEx.TraceInformation(string.Format("Configuration '{0}' is changed from '{1}' to '{2}'.", _key, Value, typedValue));
                        Value = typedValue;
                        if (OnChanged != null)
                        {
                            OnChanged();
                        }
                    }
                }
                catch (InvalidCastException)
                {
                    TraceEx.TraceWarning(string.Format("Cannot cast value '{0}' to type '{1}', original value is kept.", value, typeof(T)));
                    if (!silent)
                    {
                        throw;
                    }
                }
            }
            else
            {
                TraceEx.TraceWarning(string.Format("Cannot find configuration '{0}', original value is kept.", _key));
                if (!silent)
                {
                    throw new KeyNotFoundException(string.Format("Cannot find configuration '{0}'", _key));
                }
            }
        }
コード例 #4
0
ファイル: RetryHelper.cs プロジェクト: Jiahax/Common
        public static void Retry(
            Action action,
            Func <Exception, bool>[] errorHandlers,
            TimeSpan[] retryIntervals,
            [CallerMemberName] string methodName = null,
            [CallerFilePath] string filePath     = null,
            [CallerLineNumber] int lineNumber    = 0)
        {
            Guard.ArgumentNotNull(action, nameof(action));
            ValidateRetryParameters(errorHandlers, retryIntervals);

            int retryCount = 0;

            while (true)
            {
                if (retryCount > 0)
                {
                    Thread.Sleep(retryIntervals[retryCount - 1]);
                }
                try
                {
                    action();
                    break;
                }
                catch (Exception ex)
                {
                    if (retryCount++ < retryIntervals.Length)
                    {
                        if (errorHandlers.Any(handler => handler(ex)))
                        {
                            TraceEx.TraceWarning($"Error occurred at method {methodName} in {filePath} line {lineNumber}, current retry count: {retryCount}", ex);
                            continue;
                        }
                    }
                    throw;
                }
            }
        }
コード例 #5
0
ファイル: FolderUtility.cs プロジェクト: Jiahax/Common
        public static void ForceDeleteDirectoryWithAllSubDirectories(string directory, int maxDegreeOfParallelism = -1)
        {
            Guard.ArgumentNotNullOrEmpty(directory, nameof(directory));

            if (Directory.Exists(directory))
            {
                try
                {
                    Directory.Delete(directory, true);
                }
                catch
                {
                    try
                    {
                        ForceDeleteAllSubDirectories(directory, maxDegreeOfParallelism);
                        Directory.Delete(directory, true);
                    }
                    catch (Exception ex)
                    {
                        TraceEx.TraceWarning($"Delete directory {directory} failed.", ex);
                    }
                }
            }
        }
        private static async Task SetupContextAsync(BuildContext context, BuildRequest buildRequest)
        {
            // Setup XmlContext
            context.XmlContext.AddVariable(Constants.ContextData.IncrementalBuild, buildRequest.IsIncremental);

            if (buildRequest.ContextParams == null || buildRequest.ContextParams.Count == 0)
            {
                TraceEx.TraceWarning(LogMessage.FormatMessage(LogCode.PipelineStartsWithoutContextParameter, BuildActionMapper.GetPipelineName(buildRequest.ActionName)));
            }
            else
            {
                foreach (var contextParam in buildRequest.ContextParams.Where(kvp => !string.IsNullOrEmpty(kvp.Key)))
                {
                    context.XmlContext.AddVariable(contextParam.Key, contextParam.Value ?? string.Empty);
                }
            }

            // TODO: refactor this into a separate component
            if (buildRequest.EntityIdentifier != null)
            {
                context.XmlContext.AddVariable(Constants.EntityIdentifier, buildRequest.EntityIdentifier.ToString());
            }

            if (EnvironmentConfiguration.Environment.Value != null)
            {
                context.XmlContext.AddVariable(
                    Constants.Service.ContentService,
                    EnvironmentConfiguration.Environment.Value.GetValue <string>(Constants.Service.ContentServiceEndpoint).TrimEnd('/'));
            }

            if (CommonServiceConfiguration.PreviewContainerUrl.Value != null)
            {
                context.XmlContext.AddVariable(
                    Constants.PreviewContainerUrl,
                    CommonServiceConfiguration.PreviewContainerUrl.Value.TrimEnd('/'));
            }

            if (CommonServiceConfiguration.CollectionContainerUrl.Value != null)
            {
                context.XmlContext.AddVariable(
                    Constants.CollectionContainerUrl,
                    CommonServiceConfiguration.CollectionContainerUrl.Value.TrimEnd('/'));
            }

            // apply build config
            var buildConfigName = BuildActionMapper.GetBuildConfig(buildRequest.ActionName);

            if (buildRequest.EntityIdentifier != null && buildConfigName != null)
            {
                var organizationId = await context.GetOrganizationIdFromProjectAsync(buildRequest.EntityIdentifier.ProjectId);

                var buildConfig = await EntityAccessor.GetBuildConfigAsync(organizationId, buildConfigName);

                if (buildConfig != null)
                {
                    foreach (var config in buildConfig)
                    {
                        context.XmlContext.AddVariable(config.Key, config.Value);
                    }
                }
            }

            // Setup SharedObject
            if (buildRequest.SharedObject != null)
            {
                foreach (var sharedObject in buildRequest.SharedObject.Where(kvp => !string.IsNullOrEmpty(kvp.Key)))
                {
                    context.SetSharedObject(sharedObject.Key, sharedObject.Value);
                }
            }

            if (buildRequest.QueueInfo != null)
            {
                context.QueueInfo = buildRequest.QueueInfo;
            }
        }