Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit a0a9eb1

Browse files
committed
Fix Unzip task, it wasn't raising OnStart/OnEnd
1 parent 1bfe6a7 commit a0a9eb1

File tree

2 files changed

+58
-20
lines changed

2 files changed

+58
-20
lines changed

src/GitHub.Api/Installer/UnzipTask.cs

Lines changed: 57 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,47 +32,85 @@ public UnzipTask(CancellationToken token, string archiveFilePath, NPath extracte
3232
this.estimatedDurationProgress = estimatedDurationProgress;
3333
}
3434

35-
protected override void Run(bool success)
35+
protected void BaseRun(bool success)
3636
{
3737
base.Run(success);
38+
}
3839

39-
Logger.Trace("Unzip File: {0} to Path: {1}", archiveFilePath, extractedPath);
40+
protected override void Run(bool success)
41+
{
42+
BaseRun(success);
43+
44+
RaiseOnStart();
4045

4146
try
4247
{
43-
zipHelper.Extract(archiveFilePath, extractedPath, Token, zipFileProgress, estimatedDurationProgress);
48+
RunUnzip(success);
4449
}
4550
catch (Exception ex)
4651
{
47-
var message = "Error Unzipping file";
48-
49-
Logger.Error(ex, message);
50-
throw new UnzipTaskException(message);
52+
Errors = ex.Message;
53+
if (!RaiseFaultHandlers(ex))
54+
throw;
5155
}
56+
finally
57+
{
58+
RaiseOnEnd();
59+
}
60+
}
5261

53-
if (expectedMD5 != null)
62+
protected virtual void RunUnzip(bool success)
63+
{
64+
Logger.Trace("Unzip File: {0} to Path: {1}", archiveFilePath, extractedPath);
65+
66+
Exception exception = null;
67+
var attempts = 0;
68+
do
5469
{
55-
var calculatedMD5 = fileSystem.CalculateFolderMD5(extractedPath);
56-
if (!calculatedMD5.Equals(expectedMD5, StringComparison.InvariantCultureIgnoreCase))
70+
if (Token.IsCancellationRequested)
71+
break;
72+
73+
exception = null;
74+
try
5775
{
58-
extractedPath.DeleteIfExists();
76+
zipHelper.Extract(archiveFilePath, extractedPath, Token, zipFileProgress, estimatedDurationProgress);
77+
78+
if (expectedMD5 != null)
79+
{
80+
var calculatedMD5 = fileSystem.CalculateFolderMD5(extractedPath);
81+
success = !calculatedMD5.Equals(expectedMD5, StringComparison.InvariantCultureIgnoreCase);
82+
if (!success)
83+
{
84+
extractedPath.DeleteIfExists();
5985

60-
var message = $"Extracted MD5: {calculatedMD5} Does not match expected: {expectedMD5}";
61-
Logger.Error(message);
86+
var message = $"Extracted MD5: {calculatedMD5} Does not match expected: {expectedMD5}";
87+
Logger.Error(message);
6288

63-
throw new UnzipTaskException(message);
89+
exception = new UnzipException(message);
90+
}
91+
}
6492
}
65-
}
93+
catch (Exception ex)
94+
{
95+
exception = ex;
96+
success = false;
97+
}
98+
} while (attempts++ < RetryCount);
6699

67-
Logger.Trace("Completed Unzip");
100+
if (!success)
101+
{
102+
Token.ThrowIfCancellationRequested();
103+
throw new UnzipException("Error downloading file", exception);
104+
}
68105
}
106+
protected int RetryCount { get; }
69107
}
70108

71-
public class UnzipTaskException : Exception {
72-
public UnzipTaskException(string message) : base(message)
109+
public class UnzipException : Exception {
110+
public UnzipException(string message) : base(message)
73111
{ }
74112

75-
public UnzipTaskException(string message, Exception innerException) : base(message, innerException)
113+
public UnzipException(string message, Exception innerException) : base(message, innerException)
76114
{ }
77115
}
78116
}

src/tests/IntegrationTests/UnzipTaskTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public void TaskFailsWhenMD5Incorect()
7171
extractedPath.DirectoryExists().Should().BeFalse();
7272
failed.Should().BeTrue();
7373
exception.Should().NotBeNull();
74-
exception.Should().BeOfType<UnzipTaskException>();
74+
exception.Should().BeOfType<UnzipException>();
7575
}
7676
}
7777
}

0 commit comments

Comments
 (0)