Skip to content

Commit

Permalink
(GH-561) Warn when execution timeout has elapsed
Browse files Browse the repository at this point in the history
Right now there is no warning, so it's not always clear that a timeout
was reached. Be explicit in providing that warning so users are aware
and have an avenue for adjustment if the command needs longer than a
given period of time.
  • Loading branch information
ferventcoder committed Jan 20, 2016
1 parent 90d53ee commit 15883bb
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
7 changes: 7 additions & 0 deletions src/chocolatey/infrastructure/commands/CommandExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ namespace chocolatey.infrastructure.commands
using System.IO;
using adapters;
using filesystem;
using logging;
using platforms;
using Process = adapters.Process;

Expand Down Expand Up @@ -164,6 +165,12 @@ bool allowUseWindow
{
exitCode = p.ExitCode;
}
else
{
"chocolatey".Log().Warn(ChocolateyLoggers.Important, () => @"Chocolatey timed out waiting for the command to finish. The timeout
specified (or the default value) was '{0}' seconds. Perhaps try a
higher `--execution-timeout`? See `choco -h` for details.".format_with(waitForExitInSeconds));
}
}
}

Expand Down
24 changes: 21 additions & 3 deletions src/chocolatey/infrastructure/commands/Execute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace chocolatey.infrastructure.commands
using System;
using System.Threading;
using System.Threading.Tasks;
using logging;

/// <summary>
/// Execute a method or function
Expand Down Expand Up @@ -70,7 +71,10 @@ public T command<T>(Func<T> function, T timeoutDefaultValue)
if (task.IsCompleted) return task.Result;

cancelToken.Cancel();

this.Log().Warn(ChocolateyLoggers.Important,() => @"Chocolatey timed out waiting for the command to finish. The timeout
specified (or the default value) was '{0}' seconds. Perhaps try a
higher `--execution-timeout`? See `choco -h` for details.".format_with(_timespan.TotalSeconds));

return timeoutDefaultValue;

//T result = timeoutDefaultValue;
Expand All @@ -92,10 +96,24 @@ public bool command(Action action)
{
if (action == null) return false;

var task = Task.Factory.StartNew(action);
var completed = false;

var cancelToken = new CancellationTokenSource();
cancelToken.Token.ThrowIfCancellationRequested();
var task = Task.Factory.StartNew(action, cancelToken.Token);
task.Wait(_timespan);
completed = task.IsCompleted;

if (!completed)
{
cancelToken.Cancel();
this.Log().Warn(ChocolateyLoggers.Important, () => @"Chocolatey timed out waiting for the command to finish. The timeout
specified (or the default value) was '{0}' seconds. Perhaps try a
higher `--execution-timeout`? See `choco -h` for details.".format_with(_timespan.TotalSeconds));

}

return task.IsCompleted;
return completed;
}
}
}

0 comments on commit 15883bb

Please sign in to comment.