Skip to content

Commit

Permalink
(GH-943) Lock Pending File Until Operation Completes
Browse files Browse the repository at this point in the history
Open and hold the pending file exclusively open until install is
finished. Then remove the file lock. This allows for better concurrent
operations when running multiple choco processes at the same time
(which isn't necessarily recommended).
  • Loading branch information
ferventcoder committed Sep 16, 2016
1 parent 9b20b33 commit e818fa3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ public class ChocolateyPackageService : IChocolateyPackageService
private readonly IAutomaticUninstallerService _autoUninstallerService;
private readonly IXmlService _xmlService;
private readonly IConfigTransformService _configTransformService;
private readonly IDictionary<string, FileStream> _pendingLocks = new Dictionary<string, FileStream>();

private readonly IList<string> _proBusinessMessages = new List<string> {
@"
Are you ready for the ultimate experience? Check out Pro / Business!
Expand Down Expand Up @@ -1158,6 +1160,7 @@ public void set_pending(PackageResult packageResult, ChocolateyConfiguration con

var pendingFile = _fileSystem.combine_paths(packageDirectory, ApplicationParameters.PackagePendingFileName);
_fileSystem.write_file(pendingFile, "{0}".format_with(packageResult.Name));
_pendingLocks.Add(packageResult.Name.to_lower(), _fileSystem.open_file_exclusive(pendingFile));
}

public void remove_pending(PackageResult packageResult, ChocolateyConfiguration config)
Expand All @@ -1177,6 +1180,15 @@ public void remove_pending(PackageResult packageResult, ChocolateyConfiguration
}

var pendingFile = _fileSystem.combine_paths(packageDirectory, ApplicationParameters.PackagePendingFileName);
var lockName = packageResult.Name.to_lower();
if (_pendingLocks.ContainsKey(lockName))
{
var fileLock = _pendingLocks[lockName];
_pendingLocks.Remove(lockName);
fileLock.Close();
fileLock.Dispose();
}

if (_fileSystem.file_exists(pendingFile)) _fileSystem.delete_file(pendingFile);
}

Expand Down
3 changes: 2 additions & 1 deletion src/chocolatey/infrastructure.app/services/FilesService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace chocolatey.infrastructure.app.services
{
using System;
using System.IO;
using System.Linq;
using configuration;
using cryptography;
using domain;
Expand Down Expand Up @@ -117,7 +118,7 @@ public PackageFiles capture_package_files(string directory, ChocolateyConfigurat
this.Log().Debug(() => "Capturing package files in '{0}'".format_with(directory));
//gather all files in the folder
var files = _fileSystem.get_files(directory, pattern: "*.*", option: SearchOption.AllDirectories);
foreach (string file in files.or_empty_list_if_null())
foreach (string file in files.or_empty_list_if_null().Where(f => !f.EndsWith(ApplicationParameters.PackagePendingFileName)))
{
packageFiles.Files.Add(get_package_file(file));
}
Expand Down

0 comments on commit e818fa3

Please sign in to comment.