Another restart fix and invoke tweaks

This commit is contained in:
John Gross
2017-11-23 10:46:28 -08:00
parent 25e6f27854
commit fe5dfa0ea7
5 changed files with 23 additions and 13 deletions

View File

@@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Runtime.CompilerServices;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Torch.API.Managers; using Torch.API.Managers;
@@ -65,18 +66,18 @@ namespace Torch.API
/// <summary> /// <summary>
/// Invoke an action on the game thread. /// Invoke an action on the game thread.
/// </summary> /// </summary>
void Invoke(Action action); void Invoke(Action action, [CallerMemberName] string caller = "");
/// <summary> /// <summary>
/// Invoke an action on the game thread and block until it has completed. /// Invoke an action on the game thread and block until it has completed.
/// If this is called on the game thread the action will execute immediately. /// If this is called on the game thread the action will execute immediately.
/// </summary> /// </summary>
void InvokeBlocking(Action action); void InvokeBlocking(Action action, [CallerMemberName] string caller = "");
/// <summary> /// <summary>
/// Invoke an action on the game thread asynchronously. /// Invoke an action on the game thread asynchronously.
/// </summary> /// </summary>
Task InvokeAsync(Action action); Task InvokeAsync(Action action, [CallerMemberName] string caller = "");
/// <summary> /// <summary>
/// Start the Torch instance. /// Start the Torch instance.

View File

@@ -183,14 +183,14 @@ quit";
LogException(ex); LogException(ex);
Console.WriteLine("Exiting in 5 seconds."); Console.WriteLine("Exiting in 5 seconds.");
Thread.Sleep(5000); Thread.Sleep(5000);
LogManager.Flush();
if (_config.RestartOnCrash) if (_config.RestartOnCrash)
{ {
var exe = typeof(Program).Assembly.Location; var exe = typeof(Program).Assembly.Location;
_config.WaitForPID = Process.GetCurrentProcess().Id.ToString(); _config.WaitForPID = Process.GetCurrentProcess().Id.ToString();
Process.Start(exe, _config.ToString()); Process.Start(exe, _config.ToString());
} }
//1627 = Function failed during execution. Process.GetCurrentProcess().Kill();
Environment.Exit(1627);
} }
} }
} }

View File

@@ -14,6 +14,7 @@ using System.Text;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Xml.Serialization.GeneratedAssembly; using Microsoft.Xml.Serialization.GeneratedAssembly;
using NLog;
using Sandbox.Engine.Analytics; using Sandbox.Engine.Analytics;
using Sandbox.Game.Multiplayer; using Sandbox.Game.Multiplayer;
using Sandbox.ModAPI; using Sandbox.ModAPI;
@@ -297,9 +298,11 @@ namespace Torch.Server
MySandboxGame.Static.Exit(); MySandboxGame.Static.Exit();
Log.Info("Server stopped."); Log.Info("Server stopped.");
LogManager.Flush();
_stopHandle.Set(); _stopHandle.Set();
State = ServerState.Stopped; State = ServerState.Stopped;
IsRunning = false; IsRunning = false;
Process.GetCurrentProcess().Kill();
} }
/// <summary> /// <summary>
@@ -309,9 +312,12 @@ namespace Torch.Server
{ {
var exe = Assembly.GetExecutingAssembly().Location; var exe = Assembly.GetExecutingAssembly().Location;
((TorchConfig)Config).WaitForPID = Process.GetCurrentProcess().Id.ToString(); ((TorchConfig)Config).WaitForPID = Process.GetCurrentProcess().Id.ToString();
Config.Autostart = true;
Process.Start(exe, Config.ToString()); Process.Start(exe, Config.ToString());
Save(0).Wait();
Stop(); Stop();
Environment.Exit(0); LogManager.Flush();
Process.GetCurrentProcess().Kill();
} }
/// <inheritdoc/> /// <inheritdoc/>

View File

@@ -151,7 +151,6 @@ namespace Torch.Commands
{ {
Context.Torch.Invoke(() => Context.Torch.Invoke(() =>
{ {
Context.Torch.Save(0).Wait();
Context.Torch.Restart(); Context.Torch.Restart();
}); });
yield break; yield break;

View File

@@ -9,6 +9,7 @@ using System.Text;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using NLog; using NLog;
using ProtoBuf.Meta;
using Sandbox; using Sandbox;
using Sandbox.Engine.Multiplayer; using Sandbox.Engine.Multiplayer;
using Sandbox.Game; using Sandbox.Game;
@@ -191,16 +192,18 @@ namespace Torch
/// Invokes an action on the game thread. /// Invokes an action on the game thread.
/// </summary> /// </summary>
/// <param name="action"></param> /// <param name="action"></param>
public void Invoke(Action action) [MethodImpl(MethodImplOptions.NoInlining)]
public void Invoke(Action action, [CallerMemberName] string caller = "")
{ {
MySandboxGame.Static.Invoke(action, "Torch"); MySandboxGame.Static.Invoke(action, caller);
} }
/// <summary> /// <summary>
/// Invokes an action on the game thread asynchronously. /// Invokes an action on the game thread asynchronously.
/// </summary> /// </summary>
/// <param name="action"></param> /// <param name="action"></param>
public Task InvokeAsync(Action action) [MethodImpl(MethodImplOptions.NoInlining)]
public Task InvokeAsync(Action action, [CallerMemberName] string caller = "")
{ {
if (Thread.CurrentThread == MySandboxGame.Static.UpdateThread) if (Thread.CurrentThread == MySandboxGame.Static.UpdateThread)
{ {
@@ -209,14 +212,15 @@ namespace Torch
return Task.CompletedTask; return Task.CompletedTask;
} }
return Task.Run(() => InvokeBlocking(action)); return Task.Run(() => InvokeBlocking(action, caller));
} }
/// <summary> /// <summary>
/// Invokes an action on the game thread and blocks until it is completed. /// Invokes an action on the game thread and blocks until it is completed.
/// </summary> /// </summary>
/// <param name="action"></param> /// <param name="action"></param>
public void InvokeBlocking(Action action) [MethodImpl(MethodImplOptions.NoInlining)]
public void InvokeBlocking(Action action, [CallerMemberName] string caller = "")
{ {
if (action == null) if (action == null)
return; return;
@@ -240,7 +244,7 @@ namespace Torch
{ {
e.Set(); e.Set();
} }
}, "Torch"); }, caller);
if (!e.WaitOne(60000)) if (!e.WaitOne(60000))
throw new TimeoutException("The game action timed out."); throw new TimeoutException("The game action timed out.");