Skip to content

Commit

Permalink
(chocolateyGH-8) Run commands with timeout
Browse files Browse the repository at this point in the history
Implement Execute.with_timeout.command chain. This allows running
commands with timeouts, so that the CommandExecution timeout can be
observed.
  • Loading branch information
ferventcoder committed Jan 1, 2016
1 parent 9cccb49 commit fc32d4f
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/chocolatey/chocolatey.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
<Link>Properties\SolutionVersion.cs</Link>
</Compile>
<Compile Include="AssemblyExtensions.cs" />
<Compile Include="infrastructure\commands\Execute.cs" />
<Compile Include="GetChocolatey.cs" />
<Compile Include="infrastructure.app\commands\ChocolateyConfigCommand.cs" />
<Compile Include="infrastructure.app\commands\ChocolateyFeatureCommand.cs" />
Expand Down
95 changes: 95 additions & 0 deletions src/chocolatey/infrastructure/commands/Execute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Copyright © 2011 - Present RealDimensions Software, LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
//
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace chocolatey.infrastructure.commands
{
using System;
using System.Threading.Tasks;

/// <summary>
/// Execute a method or function
/// </summary>
public sealed class Execute
{
private readonly TimeSpan _timespan;

/// <summary>
/// The number of seconds to wait for an operation to complete.
/// </summary>
/// <param name="timeoutInSeconds">The timeout in seconds.</param>
/// <returns></returns>
public static Execute with_timeout(int timeoutInSeconds)
{
return new Execute(TimeSpan.FromSeconds(timeoutInSeconds));
}

/// <summary>
/// The timespan to wait for an operation to complete.
/// </summary>
/// <param name="timeout">The timeout.</param>
/// <returns></returns>
public static Execute with_timeout(TimeSpan timeout)
{
return new Execute(timeout);
}

private Execute(TimeSpan timespan)
{
_timespan = timespan;
}

/// <summary>
/// Runs an operation with a timeout.
/// </summary>
/// <typeparam name="T">The type to return</typeparam>
/// <param name="function">The function to execute.</param>
/// <param name="timeoutDefaultValue">The timeout default value.</param>
/// <returns>The results of the function if completes within timespan, otherwise returns the default value.</returns>
public T command<T>(Func<T> function, T timeoutDefaultValue)
{
if (function == null) return timeoutDefaultValue;

var task = Task<T>.Factory.StartNew(function);
task.Wait(_timespan);

if (task.IsCompleted) return task.Result;
return timeoutDefaultValue;

//T result = timeoutDefaultValue;
//var thread = new Thread(() => result = function());
//thread.Start();

//bool completed = thread.Join((int)TimeSpan.FromSeconds(timeoutInSeconds).TotalMilliseconds);
//if (!completed) thread.Abort();

//return result;
}

/// <summary>
/// Calls a method with a timeout.
/// </summary>
/// <param name="action">The action to perform.</param>
/// <returns>True if it finishes executing, false otherwise.</returns>
public bool command(Action action)
{
if (action == null) return false;

var task = Task.Factory.StartNew(action);
task.Wait(_timespan);

return task.IsCompleted;
}
}
}

0 comments on commit fc32d4f

Please sign in to comment.