Files
UnrealEngineUWP/Engine/Source/Programs/AutomationTool/AutomationUtils/WatchdogTimer.cs
Wes Hunt 506f7e64a4 UEB-260 - Break AutomationTool into AutomationUtils that all automation projects depend on, and AutomationTool, which essentially only contains the startup code.
* Remove ErrorReporter.Error, replace with AutomationException with Error Code.
* Move ErrorCodes to AutomationException.
* Don't return exit codes. Solely rely on exceptions to propagate exit codes.
* Remove MainProc delegate
* Remove setting of Environment.ExitCode as it is ignored when main returns an int.
* ShutdownLogging is nothrow, as all exceptions would be ignored anyway.
* Wrap all shutdown steps so further ones get a chance to run.
* Move HostPlatform.Initialize into the global try/catch block
#codereview:ben.marsh

[CL 2605826 by Wes Hunt in Main branch]
2015-06-30 11:40:05 -04:00

88 lines
2.0 KiB
C#

// Copyright 1998-2015 Epic Games, Inc. All Rights Reserved.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;
using System.Runtime.CompilerServices;
using UnrealBuildTool;
namespace AutomationTool
{
/// <summary>
/// Timer that exits the application of the execution of the command takes too long.
/// </summary>
public class WatchdogTimer : IDisposable, IProcess
{
private Timer CountownTimer;
private System.Diagnostics.StackFrame TimerFrame;
private object SyncObject = new object();
private bool bTicking = false;
[MethodImplAttribute(MethodImplOptions.NoInlining)]
public WatchdogTimer(int Seconds)
{
TimerFrame = new System.Diagnostics.StackFrame(1);
CountownTimer = new Timer(Seconds * 1000.0);
CountownTimer.Elapsed += Elapsed;
CountownTimer.Start();
bTicking = true;
ProcessManager.AddProcess(this);
}
void Elapsed(object sender, ElapsedEventArgs e)
{
lock (SyncObject)
{
bTicking = false;
}
int Interval = (int)(CountownTimer.Interval * 0.001);
Dispose();
var Method = TimerFrame.GetMethod();
Log.TraceError("BUILD FAILED: WatchdogTimer timed out after {0}s in {1}.{2}", Interval, Method.DeclaringType.Name, Method.Name);
Environment.Exit(1);
}
public void Dispose()
{
Log.TraceInformation("WatchdogTimer.Dispose()");
lock (SyncObject)
{
if (CountownTimer != null)
{
CountownTimer.Stop();
CountownTimer.Dispose();
CountownTimer = null;
}
bTicking = false;
}
ProcessManager.RemoveProcess(this);
}
public void StopProcess(bool KillDescendants = true)
{
Dispose();
}
public bool HasExited
{
get
{
bool bResult = true;
lock (SyncObject)
{
bResult = bTicking;
}
return bResult;
}
}
public string GetProcessName()
{
var Method = TimerFrame.GetMethod();
return String.Format("WatchdogTimer_{0}.{1}", Method.DeclaringType.Name, Method.Name);
}
}
}