Skip to content

Commit

Permalink
(GH-822) Provide pending override and file wait
Browse files Browse the repository at this point in the history
When a pending file is created, it needs to be at least 10 seconds old
before choco will automatically remove the unfinished pacakge. This
allows running choco functions inside of a package (as long as they are
done quickly).

If someone needs to override the functionality of the removal of a
package due to the pending file, choco will also look for a
.chocolateyPendingSkip and skip the removal if that file is found.
  • Loading branch information
ferventcoder committed Jun 22, 2016
1 parent 575580d commit 040fae8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public void RegisterComponents(Container container)
container.Register(() => configuration, Lifestyle.Singleton);
container.Register<IFileSystem, DotNetFileSystem>(Lifestyle.Singleton);
container.Register<IXmlService, XmlService>(Lifestyle.Singleton);
container.Register<IDateTimeService, SystemDateTimeUtcService>(Lifestyle.Singleton);

//nuget
container.Register<ILogger, ChocolateyNugetLogger>(Lifestyle.Singleton);
container.Register<INugetService, NugetService>(Lifestyle.Singleton);
Expand Down Expand Up @@ -119,14 +121,12 @@ public void RegisterComponents(Container container)
{
var list = new List<ITask>
{
new RemovePendingPackagesTask(container.GetInstance<IFileSystem>())
new RemovePendingPackagesTask(container.GetInstance<IFileSystem>(), container.GetInstance<IDateTimeService>())
};

return list.AsReadOnly();
},
Lifestyle.Singleton);

container.Register<IDateTimeService, SystemDateTimeUtcService>(Lifestyle.Singleton);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,23 @@ namespace chocolatey.infrastructure.app.tasks
using events;
using filesystem;
using infrastructure.events;
using infrastructure.services;
using infrastructure.tasks;
using logging;
using tolerance;

public class RemovePendingPackagesTask : ITask
{
private readonly IFileSystem _fileSystem;
private readonly IDateTimeService _dateTimeService;
private IDisposable _subscription;
private const int PENDING_FILE_AGE_SECONDS = 10;
private const string PENDING_SKIP_FILE = ".chocolateyPendingSkip";

public RemovePendingPackagesTask(IFileSystem fileSystem)
public RemovePendingPackagesTask(IFileSystem fileSystem, IDateTimeService dateTimeService)
{
_fileSystem = fileSystem;
_dateTimeService = dateTimeService;
}

public void initialize()
Expand All @@ -54,8 +59,25 @@ private void handle_message(PreRunMessage message)
foreach (var pendingFile in pendingFiles.or_empty_list_if_null())
{
var packageFolder = _fileSystem.get_directory_name(pendingFile);
this.Log().Warn("[Pending] Removing incomplete install for '{0}'".format_with(_fileSystem.get_directory_info_for(packageFolder).Name));
var packageFolderName = _fileSystem.get_directory_info_for(packageFolder).Name;

var pendingSkipFiles = _fileSystem.get_files(packageFolder, PENDING_SKIP_FILE, SearchOption.AllDirectories).ToList();
if (pendingSkipFiles.Count != 0)
{
this.Log().Warn("Pending file found for {0}, but a {1} file was also found. Skipping removal".format_with(packageFolderName, PENDING_SKIP_FILE));
continue;
}

// wait for the file to be at least x seconds old
// this allows commands running from the package for configuring sources, etc
var fileInfo = _fileSystem.get_file_info_for(pendingFile);
if (fileInfo.CreationTimeUtc.AddSeconds(PENDING_FILE_AGE_SECONDS) > _dateTimeService.get_current_date_time())
{
this.Log().Debug("Pending file found for {0}, but the file is not {1} seconds old yet.".format_with(packageFolderName, PENDING_FILE_AGE_SECONDS));
continue;
}

this.Log().Warn("[Pending] Removing incomplete install for '{0}'".format_with(packageFolderName));
FaultTolerance.retry(2, () => _fileSystem.delete_directory_if_exists(packageFolder, recursive: true, overrideAttributes: true, isSilent: true), 500, isSilent: true);
}
}
Expand Down

0 comments on commit 040fae8

Please sign in to comment.