Skip to content

Commit

Permalink
fix multi-proc logging, log naming, no excpetions in happy path
Browse files Browse the repository at this point in the history
  • Loading branch information
bitdisaster committed Mar 13, 2019
1 parent 3340770 commit 073fac7
Show file tree
Hide file tree
Showing 8 changed files with 173 additions and 123 deletions.
9 changes: 8 additions & 1 deletion src/Squirrel/IUpdateManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,27 @@ public enum ShortcutLocation {
AppRoot = 1 << 3
}

public enum UpdaterIntention {
Install,
Update
}

public interface IUpdateManager : IDisposable, IEnableLogger
{
/// <summary>
/// Fetch the remote store for updates and compare against the current
/// version to determine what updates to download.
/// </summary>
/// <param name="intention">Indicates whether the UpdateManager is used
/// in a Install or Update scenario.</param>
/// <param name="ignoreDeltaUpdates">Set this flag if applying a release
/// fails to fall back to a full release, which takes longer to download
/// but is less error-prone.</param>
/// <param name="progress">A Observer which can be used to report Progress -
/// will return values from 0-100 and Complete, or Throw</param>
/// <returns>An UpdateInfo object representing the updates to install.
/// </returns>
Task<UpdateInfo> CheckForUpdate(bool ignoreDeltaUpdates = false, Action<int> progress = null);
Task<UpdateInfo> CheckForUpdate(bool ignoreDeltaUpdates = false, Action<int> progress = null, UpdaterIntention intention = UpdaterIntention.Update);

/// <summary>
/// Download a list of releases into the local package directory.
Expand Down
26 changes: 13 additions & 13 deletions src/Squirrel/UpdateManager.ApplyReleases.cs
Original file line number Diff line number Diff line change
Expand Up @@ -536,25 +536,25 @@ internal void unshimOurselves()
// directory are "dead" (i.e. already uninstalled, but not deleted), and
// we blow them away. This is to make sure that we don't attempt to run
// an uninstaller on an already-uninstalled version.
async Task cleanDeadVersions(SemanticVersion originalVersion, SemanticVersion currentVersion, bool forceUninstall = false)
async Task cleanDeadVersions(SemanticVersion currentVersion, SemanticVersion newVersion, bool forceUninstall = false)
{
if (currentVersion == null) return;
if (newVersion == null) return;

var di = new DirectoryInfo(rootAppDirectory);
if (!di.Exists) return;

this.Log().Info("cleanDeadVersions: for version {0}", currentVersion);

string originalVersionFolder = null;
if (originalVersion != null) {
originalVersionFolder = getDirectoryForRelease(originalVersion).Name;
this.Log().Info("cleanDeadVersions: exclude folder {0}", originalVersionFolder);
}
this.Log().Info("cleanDeadVersions: checking for version {0}", newVersion);

string currentVersionFolder = null;
if (currentVersion != null) {
currentVersionFolder = getDirectoryForRelease(currentVersion).Name;
this.Log().Info("cleanDeadVersions: exclude folder {0}", currentVersionFolder);
this.Log().Info("cleanDeadVersions: exclude current version folder {0}", currentVersionFolder);
}

string newVersionFolder = null;
if (newVersion != null) {
newVersionFolder = getDirectoryForRelease(newVersion).Name;
this.Log().Info("cleanDeadVersions: exclude new version folder {0}", newVersionFolder);
}

// NB: If we try to access a directory that has already been
Expand All @@ -563,7 +563,7 @@ async Task cleanDeadVersions(SemanticVersion originalVersion, SemanticVersion cu
// come from here.
var toCleanup = di.GetDirectories()
.Where(x => x.Name.ToLowerInvariant().Contains("app-"))
.Where(x => x.Name != currentVersionFolder && x.Name != originalVersionFolder)
.Where(x => x.Name != newVersionFolder && x.Name != currentVersionFolder)
.Where(x => !isAppFolderDead(x.FullName));

if (forceUninstall == false) {
Expand Down Expand Up @@ -591,7 +591,7 @@ await squirrelApps.ForEachAsync(async exe => {
// Include dead folders in folders to :fire:
toCleanup = di.GetDirectories()
.Where(x => x.Name.ToLowerInvariant().Contains("app-"))
.Where(x => x.Name != currentVersionFolder && x.Name != originalVersionFolder);
.Where(x => x.Name != newVersionFolder && x.Name != currentVersionFolder);

// Get the current process list in an attempt to not burn
// directories which have running processes
Expand Down Expand Up @@ -625,7 +625,7 @@ await toCleanup.ForEachAsync(async x => {
var releaseEntry = default(ReleaseEntry);

foreach (var entry in entries) {
if (entry.Version == currentVersion) {
if (entry.Version == newVersion) {
releaseEntry = ReleaseEntry.GenerateFromFile(Path.Combine(pkgDir, entry.Filename));
continue;
}
Expand Down
32 changes: 21 additions & 11 deletions src/Squirrel/UpdateManager.CheckForUpdates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public CheckForUpdateImpl(string rootAppDirectory)
}

public async Task<UpdateInfo> CheckForUpdate(
UpdaterIntention intention,
string localReleaseFile,
string updateUrlOrPath,
bool ignoreDeltaUpdates = false,
Expand All @@ -30,15 +31,19 @@ public async Task<UpdateInfo> CheckForUpdate(
progress = progress ?? (_ => { });

var localReleases = Enumerable.Empty<ReleaseEntry>();
var stagingId = getOrCreateStagedUserId();
var stagingId = intention == UpdaterIntention.Install ? null : getOrCreateStagedUserId();

bool shouldInitialize = false;
try {
localReleases = Utility.LoadLocalReleases(localReleaseFile);
} catch (Exception ex) {
// Something has gone pear-shaped, let's start from scratch
this.Log().WarnException("Failed to load local releases, starting from scratch", ex);
shouldInitialize = true;
bool shouldInitialize = intention == UpdaterIntention.Install;

if (intention != UpdaterIntention.Install) {
try {
localReleases = Utility.LoadLocalReleases(localReleaseFile);
}
catch (Exception ex) {
// Something has gone pear-shaped, let's start from scratch
this.Log().WarnException("Failed to load local releases, starting from scratch", ex);
shouldInitialize = true;
}
}

if (shouldInitialize) await initializeClientAppDirectory();
Expand Down Expand Up @@ -125,7 +130,7 @@ public async Task<UpdateInfo> CheckForUpdate(
throw new Exception("Remote release File is empty or corrupted");
}

ret = determineUpdateInfo(localReleases, remoteReleases, ignoreDeltaUpdates);
ret = determineUpdateInfo(intention, localReleases, remoteReleases, ignoreDeltaUpdates);

progress(100);
return ret;
Expand All @@ -142,7 +147,7 @@ async Task initializeClientAppDirectory()
Directory.CreateDirectory(pkgDir);
}

UpdateInfo determineUpdateInfo(IEnumerable<ReleaseEntry> localReleases, IEnumerable<ReleaseEntry> remoteReleases, bool ignoreDeltaUpdates)
UpdateInfo determineUpdateInfo(UpdaterIntention intention, IEnumerable<ReleaseEntry> localReleases, IEnumerable<ReleaseEntry> remoteReleases, bool ignoreDeltaUpdates)
{
var packageDirectory = Utility.PackageDirectoryForAppDir(rootAppDirectory);
localReleases = localReleases ?? Enumerable.Empty<ReleaseEntry>();
Expand All @@ -167,7 +172,12 @@ UpdateInfo determineUpdateInfo(IEnumerable<ReleaseEntry> localReleases, IEnumera
}

if (!localReleases.Any()) {
this.Log().Warn("First run or local directory is corrupt, starting from scratch");
if (intention == UpdaterIntention.Install) {
this.Log().Info("First run, starting from scratch");
} else {
this.Log().Warn("No local releases found, starting from scratch");
}

return UpdateInfo.Create(null, new[] {latestFullRelease}, packageDirectory);
}

Expand Down
6 changes: 3 additions & 3 deletions src/Squirrel/UpdateManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ public UpdateManager(string urlOrPath,
this.rootAppDirectory = Path.Combine(rootDirectory ?? GetLocalAppDataDirectory(), this.applicationName);
}

public async Task<UpdateInfo> CheckForUpdate(bool ignoreDeltaUpdates = false, Action<int> progress = null)
public async Task<UpdateInfo> CheckForUpdate(bool ignoreDeltaUpdates = false, Action<int> progress = null, UpdaterIntention intention = UpdaterIntention.Update)
{
var checkForUpdate = new CheckForUpdateImpl(rootAppDirectory);

await acquireUpdateLock();
return await checkForUpdate.CheckForUpdate(Utility.LocalReleaseFileForAppDir(rootAppDirectory), updateUrlOrPath, ignoreDeltaUpdates, progress, urlDownloader);
return await checkForUpdate.CheckForUpdate(intention, Utility.LocalReleaseFileForAppDir(rootAppDirectory), updateUrlOrPath, ignoreDeltaUpdates, progress, urlDownloader);
}

public async Task DownloadReleases(IEnumerable<ReleaseEntry> releasesToDownload, Action<int> progress = null)
Expand All @@ -75,7 +75,7 @@ public async Task<string> ApplyReleases(UpdateInfo updateInfo, Action<int> progr

public async Task FullInstall(bool silentInstall = false, Action<int> progress = null)
{
var updateInfo = await CheckForUpdate();
var updateInfo = await CheckForUpdate(intention: UpdaterIntention.Install);
await DownloadReleases(updateInfo.ReleasesToApply);

var applyReleases = new ApplyReleasesImpl(rootAppDirectory);
Expand Down
Loading

0 comments on commit 073fac7

Please sign in to comment.