diff --git a/Backend/Program.cs b/Backend/Program.cs index 28859013..e617aa71 100644 --- a/Backend/Program.cs +++ b/Backend/Program.cs @@ -1,4 +1,6 @@ using System; +using System.Diagnostics; +using System.Globalization; using System.Threading; using Mono.Unix; using Mono.Unix.Native; @@ -23,11 +25,12 @@ namespace Swarmops.Backend { testMode = false; - UnixSignal[] killSignals = + UnixSignal[] killSignals = null; + + if (!Debugger.IsAttached) { - new UnixSignal (Signum.SIGINT), - new UnixSignal (Signum.SIGTERM) - }; + killSignals = new UnixSignal[] {new UnixSignal (Signum.SIGINT), new UnixSignal (Signum.SIGTERM)}; + } BotLog.Write (0, "MainCycle", string.Empty); BotLog.Write (0, "MainCycle", "-----------------------------------------------"); @@ -165,7 +168,7 @@ namespace Swarmops.Backend } DateTime cycleStartTime = DateTime.UtcNow; - DateTime cycleEndTime = cycleStartTime.AddSeconds (10); + DateTime cycleEndTime; int lastSecond = cycleStartTime.Second; int lastMinute = cycleStartTime.Minute; @@ -178,6 +181,7 @@ namespace Swarmops.Backend BotLog.Write (0, "MainCycle", "Cycle Start"); cycleStartTime = DateTime.UtcNow; + cycleEndTime = cycleStartTime.AddSeconds(10); try { @@ -227,20 +231,41 @@ namespace Swarmops.Backend lastMinute = cycleStartTime.Minute; lastHour = cycleStartTime.Hour; - // Wait for a maximum of ten seconds (the difference between cycleStartTime and cycleEndTime + // Wait for a maximum of ten seconds (the difference between cycleStartTime and cycleEndTime) - while (DateTime.UtcNow < cycleEndTime && !exitFlag) + DateTime utcNow = DateTime.UtcNow; + while (utcNow < cycleEndTime && !exitFlag) { - // block until a SIGINT or SIGTERM signal is generated, or 1/4 second has passed. - int signalIndex = UnixSignal.WaitAny (killSignals, 250); + int signalIndex = 250; - if (signalIndex < 1000) + // Block until a SIGINT or SIGTERM signal is generated, or 1/4 second has passed. + // However, we can't do that in a development environment - it won't have the + // Mono.Posix assembly, and won't understand UnixSignals. So people running this in + // a dev environment will need to stop it manually. + + if (!Debugger.IsAttached) + { + signalIndex = UnixSignal.WaitAny(killSignals, 250); + } + else + { + TimeSpan timeLeft = (cycleEndTime - utcNow); + + BotLog.Write(0, "MainCycle Debug", + string.Format(CultureInfo.InvariantCulture, "Waiting for {0:F2} more seconds for cycle end", + timeLeft.TotalMilliseconds / 1000.0)); + Thread.Sleep(250); + } + + if (signalIndex < 250) { exitFlag = true; Console.WriteLine ("Caught signal " + killSignals[signalIndex].Signum + ", exiting"); BotLog.Write (0, "MainCycle", "EXIT SIGNAL (" + killSignals[signalIndex].Signum + "), terminating backend"); } + + utcNow = DateTime.UtcNow; } } diff --git a/Database/Configuration.cs b/Database/Configuration.cs index d084322b..ab17c5a9 100644 --- a/Database/Configuration.cs +++ b/Database/Configuration.cs @@ -134,9 +134,26 @@ namespace Swarmops.Database } if (Debugger.IsAttached) { - // Dev web process. This will throw if we're not in a HttpContext and trying to debug something else. + if (HttpContext.Current != null) + { + // Dev web process. This will throw if we're not in a HttpContext and trying to debug something else. + return HttpContext.Current.Server.MapPath ("~/database.config"); + } + else + { + // Dev console process. - return HttpContext.Current.Server.MapPath ("~/database.config"); + string configLocation = "../Site5/database.config"; + + // however, we don't know exactly how deep in the directory structure we are, so keep adding ../ until we + // correct level. We may hit the root if the config doesn't exist and that'll throw us out. + + while (!File.Exists (configLocation)) + { + configLocation = "../" + configLocation; + } + return configLocation; + } } throw new NotImplementedException ("Invalid state"); diff --git a/Utility/BotCode/BotLog.cs b/Utility/BotCode/BotLog.cs index fb747a41..d81d5706 100644 --- a/Utility/BotCode/BotLog.cs +++ b/Utility/BotCode/BotLog.cs @@ -1,4 +1,5 @@ using System; +using System.Diagnostics; using System.IO; using System.Text; @@ -34,9 +35,17 @@ namespace Swarmops.Utility.BotCode string logFileName = "/var/log/swarmops/backend-" + now.ToString ("yyyy-MM-dd-HH") + ".log"; - using (StreamWriter writer = new StreamWriter (logFileName, true, Encoding.GetEncoding (1252))) + if (!Debugger.IsAttached) { - writer.WriteLine ("[" + now.ToString ("HH:mm:ss.fff") + "] " + message); + using (StreamWriter writer = new StreamWriter (logFileName, true, Encoding.GetEncoding (1252))) + { + writer.WriteLine ("[" + now.ToString ("HH:mm:ss.fff") + "] " + message); + } + } + else + { + // If we're in a development environment, write the log entry to the Output window instead. + Debug.WriteLine("[" + now.ToString("HH:mm:ss.fff") + "] " + message); } }