Skip to content

Commit

Permalink
(GH-106) switch to fault tolerance try catch
Browse files Browse the repository at this point in the history
Move methods that implement try/catch logging to use the now built-in
FaultTolerance method that wraps it all up.
  • Loading branch information
ferventcoder committed Feb 19, 2015
1 parent 855b4ab commit 217e8fa
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 62 deletions.
33 changes: 14 additions & 19 deletions src/chocolatey/infrastructure.app/builders/ConfigurationBuilder.cs
Expand Up @@ -29,6 +29,7 @@ namespace chocolatey.infrastructure.app.builders
using infrastructure.services;
using logging;
using platforms;
using tolerance;
using Environment = adapters.Environment;

/// <summary>
Expand Down Expand Up @@ -83,17 +84,14 @@ private static void set_file_configuration(ChocolateyConfiguration config, IFile
config.CacheLocation = !string.IsNullOrWhiteSpace(configFileSettings.CacheLocation) ? configFileSettings.CacheLocation : System.Environment.GetEnvironmentVariable("TEMP");
if (string.IsNullOrWhiteSpace(config.CacheLocation))
{
config.CacheLocation = fileSystem.combine_paths(ApplicationParameters.InstallLocation, "temp");
config.CacheLocation = fileSystem.combine_paths(ApplicationParameters.InstallLocation, "temp");
}
try
{
fileSystem.create_directory_if_not_exists(config.CacheLocation);
}
catch (Exception ex)
{
"chocolatey".Log().Warn("Could not create temp directory at '{0}':{1} {2}".format_with(config.CacheLocation,Environment.NewLine,ex.Message));
}


FaultTolerance.try_catch_with_logging_exception(
() => fileSystem.create_directory_if_not_exists(config.CacheLocation),
"Could not create temp directory at '{0}'".format_with(config.CacheLocation),
logWarningInsteadOfError: true);

config.ContainsLegacyPackageInstalls = configFileSettings.ContainsLegacyPackageInstalls;
if (configFileSettings.CommandExecutionTimeoutSeconds <= 0)
{
Expand All @@ -116,15 +114,12 @@ private static void set_file_configuration(ChocolateyConfiguration config, IFile
}
}

try
{
// save so all updated configuration items get set to existing config
xmlService.serialize(configFileSettings, globalConfigPath);
}
catch (Exception ex)
{
"chocolatey".Log().Warn(() => "Error updating '{0}'. Please ensure you have permissions to do so.{1} {2}".format_with(globalConfigPath, Environment.NewLine, ex.Message));
}

// save so all updated configuration items get set to existing config
FaultTolerance.try_catch_with_logging_exception(
() => xmlService.serialize(configFileSettings, globalConfigPath),
"Error updating '{0}'. Please ensure you have permissions to do so".format_with(globalConfigPath),
logWarningInsteadOfError: true);
}

private static void set_feature_flags(ChocolateyConfiguration config, ConfigFileSettings configFileSettings)
Expand Down
Expand Up @@ -27,6 +27,7 @@ namespace chocolatey.infrastructure.app.services
using logging;
using platforms;
using results;
using tolerance;

public class ChocolateyPackageService : IChocolateyPackageService
{
Expand Down Expand Up @@ -459,25 +460,16 @@ public void uninstall_noop(ChocolateyConfiguration config)

private void ensure_bad_package_path_is_clean(ChocolateyConfiguration config, PackageResult packageResult)
{
try
{
string badPackageInstallPath = packageResult.InstallLocation.Replace(ApplicationParameters.PackagesLocation, ApplicationParameters.PackageFailuresLocation);
if (_fileSystem.directory_exists(badPackageInstallPath))
{
_fileSystem.delete_directory(badPackageInstallPath, recursive: true);
}
}
catch (Exception ex)
{
if (config.Debug)
{
this.Log().Error(() => "Attempted to delete bad package install path if existing. Had an error:{0}{1}".format_with(Environment.NewLine, ex));
}
else
{
this.Log().Error(() => "Attempted to delete bad package install path if existing. Had an error:{0}{1}".format_with(Environment.NewLine, ex.Message));
}
}
FaultTolerance.try_catch_with_logging_exception(
() =>
{
string badPackageInstallPath = packageResult.InstallLocation.Replace(ApplicationParameters.PackagesLocation, ApplicationParameters.PackageFailuresLocation);
if (_fileSystem.directory_exists(badPackageInstallPath))
{
_fileSystem.delete_directory(badPackageInstallPath, recursive: true);
}
},
"Attempted to delete bad package install path if existing. Had an error");
}

private void handle_unsuccessful_install(ChocolateyConfiguration config, PackageResult packageResult)
Expand Down
40 changes: 16 additions & 24 deletions src/chocolatey/infrastructure.app/services/NugetService.cs
Expand Up @@ -30,6 +30,7 @@ namespace chocolatey.infrastructure.app.services
using nuget;
using platforms;
using results;
using tolerance;
using DateTime = adapters.DateTime;
using Environment = System.Environment;
using IFileSystem = filesystem.IFileSystem;
Expand Down Expand Up @@ -304,14 +305,11 @@ public void install_noop(ChocolateyConfiguration config, Action<PackageResult> c
if (installedPackage != null && (installedPackage.Version == availablePackage.Version))
{
backup_existing_version(config, installedPackage);
try
{
packageManager.UninstallPackage(installedPackage, forceRemove: config.Force, removeDependencies: config.ForceDependencies);
}
catch (Exception ex)
{
this.Log().Warn("Unable to remove existing package prior to forced reinstall.{0} {1}".format_with(Environment.NewLine, ex.Message));
}

FaultTolerance.try_catch_with_logging_exception(
() => packageManager.UninstallPackage(installedPackage, forceRemove: config.Force, removeDependencies: config.ForceDependencies),
"Unable to remove existing package prior to forced reinstall",
logWarningInsteadOfError: true);
}

try
Expand Down Expand Up @@ -349,14 +347,13 @@ private void remove_existing_rollback_directory(string packageName)
rollbackDirectory = possibleRollbacks.OrderByDescending(p => p).DefaultIfEmpty(string.Empty).FirstOrDefault();
}
}
try
{
_fileSystem.delete_directory_if_exists(rollbackDirectory, recursive: true);
}
catch (Exception ex)
{
this.Log().Warn("Attempted to remove '{0}' but had an error:{1} {2}".format_with(rollbackDirectory, Environment.NewLine, ex.Message));
}

if (string.IsNullOrWhiteSpace(rollbackDirectory) || !_fileSystem.directory_exists(rollbackDirectory)) return;

FaultTolerance.try_catch_with_logging_exception(
() => _fileSystem.delete_directory_if_exists(rollbackDirectory, recursive: true),
"Attempted to remove '{0}' but had an error:".format_with(rollbackDirectory),
logWarningInsteadOfError: true);
}

public ConcurrentDictionary<string, PackageResult> upgrade_noop(ChocolateyConfiguration config, Action<PackageResult> continueAction)
Expand Down Expand Up @@ -555,14 +552,9 @@ public void rename_legacy_package_version(ChocolateyConfiguration config, IPacka
installDirectory = pathResolver.GetInstallPath(installedPackage);
if (_fileSystem.directory_exists(installDirectory))
{
try
{
_fileSystem.move_directory(installDirectory, _fileSystem.combine_paths(ApplicationParameters.PackagesLocation, installedPackage.Id));
}
catch (Exception ex)
{
this.Log().Error("Error during old package rename:{0} {1}".format_with(Environment.NewLine, ex.Message));
}
FaultTolerance.try_catch_with_logging_exception(
() => _fileSystem.move_directory(installDirectory, _fileSystem.combine_paths(ApplicationParameters.PackagesLocation, installedPackage.Id)),
"Error during old package rename");
}
}
}
Expand Down

0 comments on commit 217e8fa

Please sign in to comment.