Skip to content

Commit

Permalink
Changed compression task to use CliWrap.
Browse files Browse the repository at this point in the history
  • Loading branch information
rogerfar committed Nov 30, 2022
1 parent 06b35cf commit 2be2724
Showing 1 changed file with 37 additions and 51 deletions.
88 changes: 37 additions & 51 deletions src/dotnet/Datack.Agent.Service/Services/Tasks/CompressTask.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Diagnostics;
using System.Text;
using ByteSizeLib;
using CliWrap;
using CliWrap.EventStream;
using Datack.Common.Models.Data;
using StringTokenFormatter;

Expand Down Expand Up @@ -76,8 +78,6 @@ public override async Task Run(JobRunTask jobRunTask, JobRunTask previousTask, C

var storePath = Path.Combine(filePath, fileName);

var resultArtifact = storePath;

OnProgress(jobRunTask.JobRunTaskId, $"Testing path {storePath}");

if (!Directory.Exists(filePath))
Expand Down Expand Up @@ -136,69 +136,55 @@ public override async Task Run(JobRunTask jobRunTask, JobRunTask previousTask, C

var executable = $"{AppDomain.CurrentDomain.BaseDirectory}7zip{Path.DirectorySeparatorChar}7za.exe";

var cmd = @$"""{executable}"" {argumentsString}";
var logCmd = @$"""{executable}"" {argumentsString}";

if (!String.IsNullOrWhiteSpace(password))
{
var decryptedPassword = _dataProtector.Decrypt(password);

cmd = cmd.Replace(decryptedPassword, "****");
logCmd = logCmd.Replace(decryptedPassword, "****");
}

OnProgress(jobRunTask.JobRunTaskId, $"Starting compress cmd {jobRunTask.ItemName} with parameters {cmd}");

var stdErr = new StringBuilder();

using (var process = new Process())
{
process.StartInfo = new ProcessStartInfo
{
FileName = executable,
Arguments = argumentsString,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
RedirectStandardInput = false,
};

process.EnableRaisingEvents = true;

process.OutputDataReceived += (_, args) =>
{
var line = args.Data?.Trim();
OnProgress(jobRunTask.JobRunTaskId, $"Starting compress cmd {jobRunTask.ItemName} with parameters {logCmd}");

if (String.IsNullOrWhiteSpace(line))
{
return;
}
var stdErrBuffer = new StringBuilder();

OnProgress(jobRunTask.JobRunTaskId, line, line.Contains('%'));
};
try
{
var cmd = Cli.Wrap(executable).WithArguments(arguments, false);

process.ErrorDataReceived += (_, args) =>
await foreach (var cmdEvent in cmd.ListenAsync(cancellationToken))
{
var line = args.Data?.Trim();
if (String.IsNullOrWhiteSpace(line))
switch (cmdEvent)
{
return;
case StandardOutputCommandEvent stdOut:
{
var line = stdOut.Text.Trim();

if (!String.IsNullOrWhiteSpace(line))
{
OnProgress(jobRunTask.JobRunTaskId, line, line.Contains('%'));
}

break;
}
case StandardErrorCommandEvent stdErr:
{
var line = stdErr.Text.Trim();

if (!String.IsNullOrWhiteSpace(line))
{
stdErrBuffer.AppendLine(line);
}

break;
}
}
stdErr.AppendLine(line);
};

process.Start();

process.BeginOutputReadLine();

process.BeginErrorReadLine();

await process.WaitForExitAsync(cancellationToken);
}
}

if (stdErr.Length > 0)
catch
{
throw new Exception(stdErr.ToString());
throw new Exception(stdErrBuffer.ToString());
}

sw.Stop();
Expand All @@ -208,7 +194,7 @@ public override async Task Run(JobRunTask jobRunTask, JobRunTask previousTask, C

var message = $"Completed compression of {jobRunTask.ItemName} from {ByteSize.FromBytes(fileSize):0.00} to {ByteSize.FromBytes(finalFileSize):0.00} ({(Int32) ((Double)finalFileSize / fileSize * 100.0) }%) in {sw.Elapsed:g} ({ByteSize.FromBytes(fileSize / sw.Elapsed.TotalSeconds):0.00}/s)";

OnComplete(jobRunTask.JobRunTaskId, message, resultArtifact, false);
OnComplete(jobRunTask.JobRunTaskId, message, storePath, false);
}
catch (Exception ex)
{
Expand Down

0 comments on commit 2be2724

Please sign in to comment.