diff --git a/Backend/Backend.csproj b/Backend/Backend.csproj index 79098f80..819839dc 100644 --- a/Backend/Backend.csproj +++ b/Backend/Backend.csproj @@ -77,7 +77,7 @@ - + diff --git a/Backend/Program.cs b/Backend/BackendLoop.cs similarity index 97% rename from Backend/Program.cs rename to Backend/BackendLoop.cs index 86f33fa6..a955156b 100644 --- a/Backend/Program.cs +++ b/Backend/BackendLoop.cs @@ -1,795 +1,795 @@ -using System; -using System.Diagnostics; -using System.Globalization; -using System.Threading; -using Mono.Unix; -using Mono.Unix.Native; -using Swarmops.Database; -using Swarmops.Logic.Communications; -using Swarmops.Logic.Communications.Payload; -using Swarmops.Logic.Communications.Resolution; -using Swarmops.Logic.Financial; -using Swarmops.Logic.Structure; -using Swarmops.Logic.Support; -using Swarmops.Utility; -using Swarmops.Utility.BotCode; -using Swarmops.Utility.Communications; -using Swarmops.Utility.Mail; - -namespace Swarmops.Backend -{ - internal class Program - { - private const string heartbeatFile = "/var/run/swarmops/backend/heartbeat.txt"; - private static bool testMode; - - private static void Main (string[] args) - { - // Checking for schemata upgrade first of all - - DatabaseMaintenance.UpgradeSchemata(); - - testMode = false; - - UnixSignal[] killSignals = null; - - if (!Debugger.IsAttached) - { - killSignals = new UnixSignal[] {new UnixSignal (Signum.SIGINT), new UnixSignal (Signum.SIGTERM)}; - } - - BotLog.Write (0, "MainCycle", string.Empty); - BotLog.Write (0, "MainCycle", "-----------------------------------------------"); - BotLog.Write (0, "MainCycle", string.Empty); - - if (args.Length > 0) - { - if (args[0].ToLower() == "test") - { -/* - BotLog.Write(0, "MainCycle", "Running self-tests"); - HeartBeater.Instance.Beat(heartbeatFile); // Otherwise Heartbeater.Beat() will fail in various places - - testMode = true; - Console.WriteLine("Testing All Maintenance Processes (except membership-changing ones)."); - PWLog.Write(PWLogItem.None, 0, PWLogAction.SystemTest, string.Empty, string.Empty); - - Console.WriteLine("\r\n10-second intervals:"); - OnEveryTenSeconds(); - Console.WriteLine("\r\nEvery minute:"); - OnEveryMinute(); - Console.WriteLine("\r\nEvery five minutes:"); - OnEveryFiveMinutes(); - Console.WriteLine("\r\nEvery hour:"); - OnEveryHour(); - Console.WriteLine("\r\nNoon:"); - OnNoon(); - Console.WriteLine("\r\nMidnight:"); - OnMidnight(); - */ - - Console.WriteLine ("Testing database access..."); - - Console.WriteLine (SwarmDb.GetDatabaseForReading().GetPerson (1).Name); - Console.WriteLine (SwarmDb.GetDatabaseForReading().GetPerson (1).PasswordHash); - - Console.WriteLine ("Creating OutboundComm..."); - - OutboundComm.CreateNotification (null, NotificationResource.System_Startup_Backend); - - Console.WriteLine ("Transmitting..."); - - OutboundComms comms = OutboundComms.GetOpen(); - - Console.WriteLine ("{0} open items in outbound comms.", comms.Count); - - foreach (OutboundComm comm in comms) - { - if (comm.TransmitterClass != "Swarmops.Utility.Communications.CommsTransmitterMail") - { - throw new NotImplementedException(); - } - - ICommsTransmitter transmitter = new CommsTransmitterMail(); - - OutboundCommRecipients recipients = comm.Recipients; - PayloadEnvelope envelope = PayloadEnvelope.FromXml (comm.PayloadXml); - - foreach (OutboundCommRecipient recipient in recipients) - { - transmitter.Transmit (envelope, recipient.Person); - } - } - - - Console.Write ("\r\nAll tests run. Waiting for mail queue to flush... "); - while (!MailTransmitter.CanExit) - { - Thread.Sleep (50); - } - - Console.WriteLine ("done."); - BotLog.Write (0, "MainCycle", "Exiting self-tests"); - return; - } - - if (args[0].ToLower() == "console") - { - Console.WriteLine ("\r\nRunning Swarmops-Backend in CONSOLE mode.\r\n"); - - // ------------------------------------------------------------------------------------- - // ------------------------------------------------------------------------------------- - // ------------------------------------------------------------------------------------- - - // ----------------------- INSERT ANY ONE-OFF ACTIONS HERE ------------------------- - - - Console.Write ("\r\nWaiting for mail queue to flush... "); - - while (!MailTransmitter.CanExit) - { - Thread.Sleep (50); - } - - Console.WriteLine ("done."); - - return; - } - - if (args[0].ToLower() == "rsm") - { - Console.WriteLine ("Testing character encoding: räksmörgås RÄKSMÖRGÅS"); - return; - } - } - - /* - MailMessage message = new MailMessage(); - message.From = new MailAddress(Strings.MailSenderAddress, Strings.MailSenderName); - message.To.Add (new MailAddress ("rick@piratpartiet.se", "Rick Falkvinge (Piratpartiet)")); - message.Subject = "Räksmörgåsarnas ékÖNÖMÏåvdëlnïng"; - message.Body = "Hejsan hoppsan Räksmörgåsar."; - message.BodyEncoding = Encoding.Default; - message.SubjectEncoding = Encoding.Default; - - SmtpClient smtpClient = new SmtpClient ("localhost"); - smtpClient.Credentials = null; // mono bug - smtpClient.Send (message);*/ - - Console.WriteLine (" * Swarmops Backend starting"); - - BotLog.Write(0, "MainCycle", "Backend STARTING"); - - // Disable certificate checking due to Mono not installing with a certificate repository - this is UTTERLY broken - - SupportFunctions.DisableSslCertificateChecks(); // MONO BUG/MISFEATURE: Mono has no root certificates, so can't verify cert - - // Tell sysop we're starting - - OutboundComm.CreateNotification (null, NotificationResource.System_Startup_Backend); - - // Check for existence of installation ID. If not, create one. Warning: has privacy implications when communicated. - - if (Persistence.Key["SwarmopsInstallationId"] == string.Empty) - { - Persistence.Key["SwarmopsInstallationId"] = Guid.NewGuid().ToString(); - } - - // Check for existence of bitcoin hotwallet root - - BitcoinUtility.VerifyBitcoinHotWallet(); - - // Begin maintenance loop - - DateTime cycleStartTime = DateTime.UtcNow; - DateTime cycleEndTime; - - int lastSecond = cycleStartTime.Second; - int lastMinute = cycleStartTime.Minute; - int lastHour = cycleStartTime.Hour; - - bool exitFlag = false; - - while (!exitFlag) // exit is handled by signals handling at end of loop - { - BotLog.Write (0, "MainCycle", "Cycle Start"); - - cycleStartTime = DateTime.UtcNow; - cycleEndTime = cycleStartTime.AddSeconds(10); - - try - { - OnEveryTenSeconds(); - - if (cycleStartTime.Second < lastSecond) - { - OnEveryMinute(); - - if (cycleStartTime.Minute%5 == 0) - { - OnEveryFiveMinutes(); - } - } - - if (cycleStartTime.Minute < lastMinute) - { - OnEveryHour(); - - if (DateTime.Now.Hour == 10 && DateTime.Today.DayOfWeek == DayOfWeek.Tuesday) - { - OnTuesdayMorning(); - } - - if (DateTime.Now.Hour == 7 && DateTime.Today.DayOfWeek == DayOfWeek.Monday) - { - OnMondayMorning(); - } - } - - if (cycleStartTime.Hour >= 12 && lastHour < 12) - { - OnNoon(); - } - - if (cycleStartTime.Hour < lastHour) - { - OnMidnight(); - } - } - - catch (Exception e) - { - // Note each "OnEvery..." catches its own errors and sends Exception mails, - // so that failure in one should not stop the others from running. This particular - // code should never run. - - ExceptionMail.Send (new Exception ("Failed in swarmops-backend main loop", e), true); - } - - lastSecond = cycleStartTime.Second; - lastMinute = cycleStartTime.Minute; - lastHour = cycleStartTime.Hour; - - // Wait for a maximum of ten seconds (the difference between cycleStartTime and cycleEndTime) - - DateTime utcNow = DateTime.UtcNow; - while (utcNow < cycleEndTime && !exitFlag) - { - int signalIndex = 250; - - // 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; - } - } - - Console.WriteLine (" * Swarmops Backend stopping"); - BotLog.Write (0, "MainCycle", "BACKEND EXITING, sending backend-termination notices"); - - /* - if (HeartBeater.Instance.WasKilled) - { - // removed unconditional delete, cron job that restarts bot uses it to know that it is intentionally down. - ExceptionMail.Send(new Exception("HeartBeater triggered restart of Swarmops Backend. Will commence after 800 seconds."), false); - }*/ - - BotLog.Write (0, "MainCycle", "...done"); - - /* - while (!MailTransmitter.CanExit) - { - System.Threading.Thread.Sleep(50); - }*/ - - Thread.Sleep (2000); - } - - private static void OnEveryTenSeconds() - { - try - { - BotLog.Write (0, "MainCycle", "Ten-second entry"); - - CommsTransmitter.Run(); - - try - { - /*TestTrace("Running EventProcessor.Run()..."); - EventProcessor.Run(); - TestTrace(" done.\r\n");*/ - } - catch (Exception e) - { - TraceAndReport (e); - } - - try - { - /*TestTrace("Running MailResolver.Run()..."); - MailResolver.Run(); - TestTrace(" done.\r\n");*/ - } - catch (Exception e) - { - TraceAndReport (e); - } - - try - { - /*TestTrace("Running MailProcessor.Run()..."); - MailProcessor.Run(); - TestTrace(" done.\r\n");*/ - } - catch (Exception e) - { - TraceAndReport (e); - } - - BotLog.Write (0, "MainCycle", "Ten-second exit"); - } - catch (Exception e) - { - ExceptionMail.Send (e, true); - } - } - - private static void OnEveryMinute() - { - try - { - BotLog.Write (0, "MainCycle", "One-minute process (empty)"); - } - catch (Exception e) - { - TraceAndReport (e); - } - // empty - } - - - private static void OnEveryFiveMinutes() - { - try - { - BotLog.Write (0, "MainCycle", "Five-minute entry"); - - try - { - BotLog.Write(1, "FiveMinute", "Starting automated payout processing"); - Payouts.PerformAutomated(); - } - catch (Exception e) - { - TraceAndReport (e); - } - - try - { - /*BotLog.Write(1, "FiveMinute", "Starting newsletter checker"); - TestTrace("Running NewsletterChecker.Run()..."); - NewsletterChecker.Run(); - TestTrace(" done.\r\n");*/ - } - catch (Exception e) - { - TraceAndReport (e); - } - - try - { - /*BotLog.Write(1, "FiveMinute", "Starting turnaround tracker"); - TestTrace("Running TurnaroundTracker.Run()..."); - TurnaroundTracker.Run(); - TestTrace(" done.\r\n");*/ - } - catch (Exception e) - { - TraceAndReport (e); - } - - try - { - // TestTrace(" done.\r\nRunning UpdateStatsCache.Run()..."); - // UpdateStatsCache.Run(); - /*TestTrace(" done.\r\nRunning TranslateUrls()..."); - TranslateUrls();*/ - - // Added during election rush 2010: clean up support database every five minutes instead of once a day - // SMS notifications for bounces are turned off, so people don't get SMS notifications in the middle of the night - } - catch (Exception e) - { - TraceAndReport (e); - } - - try - { - /*BotLog.Write(1, "FiveMinute", "Closing delay warnings"); - TestTrace("Running SupportDatabase.CloseDelayWarnings()..."); - SupportDatabase.CloseDelayWarnings(); - TestTrace(" done.\r\n");*/ - } - catch (Exception e) - { - TraceAndReport (e); - } - - try - { - /*TestTrace("Running SupportDatabase.NotifyBouncingEmails()..."); - BotLog.Write(1, "FiveMinute", "Notifying bouncing emails"); - SupportDatabase.NotifyBouncingEmails(); - TestTrace(" done.\r\n");*/ - } - catch (Exception e) - { - TraceAndReport (e); - } - - try - { - /*TestTrace("Running SupportMailReview.Run()..."); - BotLog.Write(1, "FiveMinute", "Running support mail review"); - SupportMailReview.Run(); - TestTrace(" done.\r\n");*/ - } - catch (Exception e) - { - TraceAndReport (e); - } - - - BotLog.Write (0, "MainCycle", "Five-minute exit"); - } - catch (Exception e) - { - ExceptionMail.Send (e, true); - TestTrace (e.ToString()); - } - } - - - private static void OnEveryHour() - { - try - { - BotLog.Write (0, "MainCycle", "One-hour entry"); - - try - { - // This will upgrade the database schema if and only if we failed to do so on entry. - - if (SwarmDb.DbVersion < SwarmDb.DbVersionExpected) - { - BotLog.Write (1, "OneHour", "Entering DbUpgrade"); - DatabaseMaintenance.UpgradeSchemata(); - BotLog.Write (1, "OneHour", "Exited DbUpgrade"); - } - } - catch (Exception e) - { - TraceAndReport (e); - } - - - try - { - BotLog.Write(1, "OneHour", "Entering Automatch"); - Organizations organizations = Organizations.GetAll(); - foreach (Organization organization in organizations) - { - if (organization.IsEconomyEnabled) - { - BotLog.Write(1, "OneHour", "Automatching org #" + organization.Identity.ToString(CultureInfo.InvariantCulture)); - Payouts.AutomatchAgainstUnbalancedTransactions(organization); - } - } - BotLog.Write(1, "OneHour", "Exited Automatch"); - - /*TestTrace("Running PaymentGroupMapper.Run()..."); - PaymentGroupMapper.Run(); - TestTrace(" done.\r\n");*/ - } - catch (Exception e) - { - TraceAndReport (e); - } - - try - { - /*TestTrace("Running BlogTop50Scraper.Run()..."); - BlogTop50Scraper.Run(); - TestTrace(" done.\r\n");*/ - } - catch (Exception e) - { - TraceAndReport (e); - } - - try - { - /*TestTrace("Running Mappery.CreatePiratpartietOrganizationStrengthCircuitMap()..."); - Mappery.CreatePiratpartietOrganizationStrengthCircuitMap(); - TestTrace(" done.\r\n");*/ - } - catch (Exception e) - { - TraceAndReport (e); - } - - try - { - /*TestTrace("Running Mappery.CreatePiratpartietOrganizationStrengthCityMap()..."); - Mappery.CreatePiratpartietOrganizationStrengthCityMap(); - TestTrace(" done.\r\n");*/ - } - catch (Exception e) - { - TraceAndReport (e); - } - - BotLog.Write (0, "MainCycle", "One-hour exit"); - } - catch (Exception e) - { - TraceAndReport (e); - } - } - - private static void OnMidnight() - { - try - { - BotLog.Write (0, "MainCycle", "Midnight entry"); - - try - { - if (!testMode) - { - /*TestTrace("Running RosterHousekeeping.ChurnExpiredMembers()..."); - RosterHousekeeping.ChurnExpiredMembers(); - TestTrace(" done.\r\n");*/ - } - - ExchangeRateSnapshot.Create(); - } - catch (Exception e) - { - TraceAndReport (e); - } - - try - { - BotLog.DeleteOld (14); // delete logs older than 14 days - } - catch (Exception e) - { - TraceAndReport (e); - } - - try - { - /*TestTrace("Running SwedishForumMemberCheck.Run()..."); - SwedishForumMemberCheck.Run(); - TestTrace(" done.\r\n");*/ - } - catch (Exception e) - { - TraceAndReport (e); - } - - try - { - /*TestTrace("Running SalaryProcessor.Run()..."); - SalaryProcessor.Run(); - TestTrace(" done.\r\n");*/ - } - catch (Exception e) - { - TraceAndReport (e); - } - - try - { - /*TestTrace("Running TurnaroundTracker.Housekeeping()..."); - TurnaroundTracker.Housekeeping(); - TestTrace(" done.\r\n");*/ - } - catch (Exception e) - { - TraceAndReport (e); - } - - try - { - /*TestTrace("Running Mappery.CreateUngPiratUptakeMap()..."); - Mappery.CreateUngPiratUptakeMap(); - TestTrace(" done.\r\n");*/ - } - catch (Exception e) - { - TraceAndReport (e); - } - - try - { - /*TestTrace("Running RosterHousekeeping.TimeoutVolunteers()..."); - RosterHousekeeping.TimeoutVolunteers(); - TestTrace(" done.\r\n");*/ - } - catch (Exception e) - { - TraceAndReport (e); - } - - BotLog.Write (0, "MainCycle", "Midnight exit"); - } - catch (Exception e) - { - ExceptionMail.Send (e, true); - TestTrace (e.ToString()); - } - } - - private static void OnNoon() - { - BotLog.Write (0, "MainCycle", "Noon entry"); - - try - { - if (!PilotInstallationIds.IsPilot (PilotInstallationIds.PiratePartySE)) - { - // PPSE is still running PW4 code for this, so don't run for PPSE - - Payroll.ProcessMonthly(); // will only actually run on the 1st, but no harm in testing every noon - } - - // Check all bitcoin accounts for previously-unseen transactions once a day - - Organizations allOrganizations = Organizations.GetAll(); - foreach (Organization organization in allOrganizations) - { - // this actually checks hot storage too, but that's supposed - // to be up to date since we're the ones handling it - BitcoinUtility.CheckColdStorageForOrganization (organization); - } - - if (!testMode) - { - /*TestTrace("Running RosterHousekeeping.RemindAllExpiries()..."); - RosterHousekeeping.RemindAllExpiries(); - TestTrace(" done.\r\n");*/ - } - } - catch (Exception e) - { - TraceAndReport (e); - } - - try - { - if (!testMode) - { - /*TestTrace("Running SupportDatabase.NotifyBouncingEmails()..."); - SupportDatabase.NotifyBouncingEmails(); - TestTrace(" done.\r\n");*/ - } - } - catch (Exception e) - { - TraceAndReport (e); - } - - try - { - /*TestTrace("Running SupportDatabase.CloseDelayWarnings()..."); - SupportDatabase.CloseDelayWarnings(); - TestTrace(" done.\r\n");*/ - } - catch (Exception e) - { - TraceAndReport (e); - } - - try - { - /*TestTrace("Running SupportMailReview.Run()..."); - SupportMailReview.Run(); - TestTrace(" done.\r\n");*/ - } - catch (Exception e) - { - TraceAndReport (e); - } - - - BotLog.Write (0, "MainCycle", "Noon exit"); - } - - private static void OnMondayMorning() - { - try - { - // Detect and log any forex difference exceeding 100 cents. - - FinancialAccounts allAccounts = FinancialAccounts.GetAll(); // across ALL ORGS! - - foreach (FinancialAccount account in allAccounts) - { - // For every account, if it's based on foreign currency, check for forex gains/losses - - if (account.ForeignCurrency != null) - { - account.CheckForexProfitLoss(); - } - } - } - catch (Exception e) - { - ExceptionMail.Send(e, true); - if (testMode) - { - Console.WriteLine(e.ToString()); - } - } - } - - private static void OnTuesdayMorning() - { - try - { - //TestTrace("Running ActivityMailer.Run()..."); - // PirateWeb.Utility.BotCode.ActivityMailer.Run(); - //TestTrace(" done.\r\n"); - } - catch (Exception e) - { - ExceptionMail.Send (e, true); - if (testMode) - { - Console.WriteLine (e.ToString()); - } - } - } - - private static void TraceAndReport (Exception e) - { - TestTrace (" failed.\r\n"); - ExceptionMail.Send (e, true); - TestTrace (e.ToString()); - } - - private static void TestTrace (string message) - { - if (testMode) - { - Console.Write (message); - } - } - } +using System; +using System.Diagnostics; +using System.Globalization; +using System.Threading; +using Mono.Unix; +using Mono.Unix.Native; +using Swarmops.Database; +using Swarmops.Logic.Communications; +using Swarmops.Logic.Communications.Payload; +using Swarmops.Logic.Communications.Resolution; +using Swarmops.Logic.Financial; +using Swarmops.Logic.Structure; +using Swarmops.Logic.Support; +using Swarmops.Utility; +using Swarmops.Utility.BotCode; +using Swarmops.Utility.Communications; +using Swarmops.Utility.Mail; + +namespace Swarmops.Backend +{ + internal class BackendLoop + { + private const string heartbeatFile = "/var/run/swarmops/backend/heartbeat.txt"; + private static bool testMode; + + private static void Main (string[] args) + { + // Checking for schemata upgrade first of all + + DatabaseMaintenance.UpgradeSchemata(); + + testMode = false; + + UnixSignal[] killSignals = null; + + if (!Debugger.IsAttached) + { + killSignals = new UnixSignal[] {new UnixSignal (Signum.SIGINT), new UnixSignal (Signum.SIGTERM)}; + } + + BotLog.Write (0, "MainCycle", string.Empty); + BotLog.Write (0, "MainCycle", "-----------------------------------------------"); + BotLog.Write (0, "MainCycle", string.Empty); + + if (args.Length > 0) + { + if (args[0].ToLower() == "test") + { +/* + BotLog.Write(0, "MainCycle", "Running self-tests"); + HeartBeater.Instance.Beat(heartbeatFile); // Otherwise Heartbeater.Beat() will fail in various places + + testMode = true; + Console.WriteLine("Testing All Maintenance Processes (except membership-changing ones)."); + PWLog.Write(PWLogItem.None, 0, PWLogAction.SystemTest, string.Empty, string.Empty); + + Console.WriteLine("\r\n10-second intervals:"); + OnEveryTenSeconds(); + Console.WriteLine("\r\nEvery minute:"); + OnEveryMinute(); + Console.WriteLine("\r\nEvery five minutes:"); + OnEveryFiveMinutes(); + Console.WriteLine("\r\nEvery hour:"); + OnEveryHour(); + Console.WriteLine("\r\nNoon:"); + OnNoon(); + Console.WriteLine("\r\nMidnight:"); + OnMidnight(); + */ + + Console.WriteLine ("Testing database access..."); + + Console.WriteLine (SwarmDb.GetDatabaseForReading().GetPerson (1).Name); + Console.WriteLine (SwarmDb.GetDatabaseForReading().GetPerson (1).PasswordHash); + + Console.WriteLine ("Creating OutboundComm..."); + + OutboundComm.CreateNotification (null, NotificationResource.System_Startup_Backend); + + Console.WriteLine ("Transmitting..."); + + OutboundComms comms = OutboundComms.GetOpen(); + + Console.WriteLine ("{0} open items in outbound comms.", comms.Count); + + foreach (OutboundComm comm in comms) + { + if (comm.TransmitterClass != "Swarmops.Utility.Communications.CommsTransmitterMail") + { + throw new NotImplementedException(); + } + + ICommsTransmitter transmitter = new CommsTransmitterMail(); + + OutboundCommRecipients recipients = comm.Recipients; + PayloadEnvelope envelope = PayloadEnvelope.FromXml (comm.PayloadXml); + + foreach (OutboundCommRecipient recipient in recipients) + { + transmitter.Transmit (envelope, recipient.Person); + } + } + + + Console.Write ("\r\nAll tests run. Waiting for mail queue to flush... "); + while (!MailTransmitter.CanExit) + { + Thread.Sleep (50); + } + + Console.WriteLine ("done."); + BotLog.Write (0, "MainCycle", "Exiting self-tests"); + return; + } + + if (args[0].ToLower() == "console") + { + Console.WriteLine ("\r\nRunning Swarmops-Backend in CONSOLE mode.\r\n"); + + // ------------------------------------------------------------------------------------- + // ------------------------------------------------------------------------------------- + // ------------------------------------------------------------------------------------- + + // ----------------------- INSERT ANY ONE-OFF ACTIONS HERE ------------------------- + + + Console.Write ("\r\nWaiting for mail queue to flush... "); + + while (!MailTransmitter.CanExit) + { + Thread.Sleep (50); + } + + Console.WriteLine ("done."); + + return; + } + + if (args[0].ToLower() == "rsm") + { + Console.WriteLine ("Testing character encoding: räksmörgås RÄKSMÖRGÅS"); + return; + } + } + + /* + MailMessage message = new MailMessage(); + message.From = new MailAddress(Strings.MailSenderAddress, Strings.MailSenderName); + message.To.Add (new MailAddress ("rick@piratpartiet.se", "Rick Falkvinge (Piratpartiet)")); + message.Subject = "Räksmörgåsarnas ékÖNÖMÏåvdëlnïng"; + message.Body = "Hejsan hoppsan Räksmörgåsar."; + message.BodyEncoding = Encoding.Default; + message.SubjectEncoding = Encoding.Default; + + SmtpClient smtpClient = new SmtpClient ("localhost"); + smtpClient.Credentials = null; // mono bug + smtpClient.Send (message);*/ + + Console.WriteLine (" * Swarmops Backend starting"); + + BotLog.Write(0, "MainCycle", "Backend STARTING"); + + // Disable certificate checking due to Mono not installing with a certificate repository - this is UTTERLY broken + + SupportFunctions.DisableSslCertificateChecks(); // MONO BUG/MISFEATURE: Mono has no root certificates, so can't verify cert + + // Tell sysop we're starting + + OutboundComm.CreateNotification (null, NotificationResource.System_Startup_Backend); + + // Check for existence of installation ID. If not, create one. Warning: has privacy implications when communicated. + + if (Persistence.Key["SwarmopsInstallationId"] == string.Empty) + { + Persistence.Key["SwarmopsInstallationId"] = Guid.NewGuid().ToString(); + } + + // Check for existence of bitcoin hotwallet root + + BitcoinUtility.VerifyBitcoinHotWallet(); + + // Begin maintenance loop + + DateTime cycleStartTime = DateTime.UtcNow; + DateTime cycleEndTime; + + int lastSecond = cycleStartTime.Second; + int lastMinute = cycleStartTime.Minute; + int lastHour = cycleStartTime.Hour; + + bool exitFlag = false; + + while (!exitFlag) // exit is handled by signals handling at end of loop + { + BotLog.Write (0, "MainCycle", "Cycle Start"); + + cycleStartTime = DateTime.UtcNow; + cycleEndTime = cycleStartTime.AddSeconds(10); + + try + { + OnEveryTenSeconds(); + + if (cycleStartTime.Second < lastSecond) + { + OnEveryMinute(); + + if (cycleStartTime.Minute%5 == 0) + { + OnEveryFiveMinutes(); + } + } + + if (cycleStartTime.Minute < lastMinute) + { + OnEveryHour(); + + if (DateTime.Now.Hour == 10 && DateTime.Today.DayOfWeek == DayOfWeek.Tuesday) + { + OnTuesdayMorning(); + } + + if (DateTime.Now.Hour == 7 && DateTime.Today.DayOfWeek == DayOfWeek.Monday) + { + OnMondayMorning(); + } + } + + if (cycleStartTime.Hour >= 12 && lastHour < 12) + { + OnNoon(); + } + + if (cycleStartTime.Hour < lastHour) + { + OnMidnight(); + } + } + + catch (Exception e) + { + // Note each "OnEvery..." catches its own errors and sends Exception mails, + // so that failure in one should not stop the others from running. This particular + // code should never run. + + ExceptionMail.Send (new Exception ("Failed in swarmops-backend main loop", e), true); + } + + lastSecond = cycleStartTime.Second; + lastMinute = cycleStartTime.Minute; + lastHour = cycleStartTime.Hour; + + // Wait for a maximum of ten seconds (the difference between cycleStartTime and cycleEndTime) + + DateTime utcNow = DateTime.UtcNow; + while (utcNow < cycleEndTime && !exitFlag) + { + int signalIndex = 250; + + // 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; + } + } + + Console.WriteLine (" * Swarmops Backend stopping"); + BotLog.Write (0, "MainCycle", "BACKEND EXITING, sending backend-termination notices"); + + /* + if (HeartBeater.Instance.WasKilled) + { + // removed unconditional delete, cron job that restarts bot uses it to know that it is intentionally down. + ExceptionMail.Send(new Exception("HeartBeater triggered restart of Swarmops Backend. Will commence after 800 seconds."), false); + }*/ + + BotLog.Write (0, "MainCycle", "...done"); + + /* + while (!MailTransmitter.CanExit) + { + System.Threading.Thread.Sleep(50); + }*/ + + Thread.Sleep (2000); + } + + private static void OnEveryTenSeconds() + { + try + { + BotLog.Write (0, "MainCycle", "Ten-second entry"); + + CommsTransmitter.Run(); + + try + { + /*TestTrace("Running EventProcessor.Run()..."); + EventProcessor.Run(); + TestTrace(" done.\r\n");*/ + } + catch (Exception e) + { + TraceAndReport (e); + } + + try + { + /*TestTrace("Running MailResolver.Run()..."); + MailResolver.Run(); + TestTrace(" done.\r\n");*/ + } + catch (Exception e) + { + TraceAndReport (e); + } + + try + { + /*TestTrace("Running MailProcessor.Run()..."); + MailProcessor.Run(); + TestTrace(" done.\r\n");*/ + } + catch (Exception e) + { + TraceAndReport (e); + } + + BotLog.Write (0, "MainCycle", "Ten-second exit"); + } + catch (Exception e) + { + ExceptionMail.Send (e, true); + } + } + + private static void OnEveryMinute() + { + try + { + BotLog.Write (0, "MainCycle", "One-minute process (empty)"); + } + catch (Exception e) + { + TraceAndReport (e); + } + // empty + } + + + private static void OnEveryFiveMinutes() + { + try + { + BotLog.Write (0, "MainCycle", "Five-minute entry"); + + try + { + BotLog.Write(1, "FiveMinute", "Starting automated payout processing"); + Payouts.PerformAutomated(); + } + catch (Exception e) + { + TraceAndReport (e); + } + + try + { + /*BotLog.Write(1, "FiveMinute", "Starting newsletter checker"); + TestTrace("Running NewsletterChecker.Run()..."); + NewsletterChecker.Run(); + TestTrace(" done.\r\n");*/ + } + catch (Exception e) + { + TraceAndReport (e); + } + + try + { + /*BotLog.Write(1, "FiveMinute", "Starting turnaround tracker"); + TestTrace("Running TurnaroundTracker.Run()..."); + TurnaroundTracker.Run(); + TestTrace(" done.\r\n");*/ + } + catch (Exception e) + { + TraceAndReport (e); + } + + try + { + // TestTrace(" done.\r\nRunning UpdateStatsCache.Run()..."); + // UpdateStatsCache.Run(); + /*TestTrace(" done.\r\nRunning TranslateUrls()..."); + TranslateUrls();*/ + + // Added during election rush 2010: clean up support database every five minutes instead of once a day + // SMS notifications for bounces are turned off, so people don't get SMS notifications in the middle of the night + } + catch (Exception e) + { + TraceAndReport (e); + } + + try + { + /*BotLog.Write(1, "FiveMinute", "Closing delay warnings"); + TestTrace("Running SupportDatabase.CloseDelayWarnings()..."); + SupportDatabase.CloseDelayWarnings(); + TestTrace(" done.\r\n");*/ + } + catch (Exception e) + { + TraceAndReport (e); + } + + try + { + /*TestTrace("Running SupportDatabase.NotifyBouncingEmails()..."); + BotLog.Write(1, "FiveMinute", "Notifying bouncing emails"); + SupportDatabase.NotifyBouncingEmails(); + TestTrace(" done.\r\n");*/ + } + catch (Exception e) + { + TraceAndReport (e); + } + + try + { + /*TestTrace("Running SupportMailReview.Run()..."); + BotLog.Write(1, "FiveMinute", "Running support mail review"); + SupportMailReview.Run(); + TestTrace(" done.\r\n");*/ + } + catch (Exception e) + { + TraceAndReport (e); + } + + + BotLog.Write (0, "MainCycle", "Five-minute exit"); + } + catch (Exception e) + { + ExceptionMail.Send (e, true); + TestTrace (e.ToString()); + } + } + + + private static void OnEveryHour() + { + try + { + BotLog.Write (0, "MainCycle", "One-hour entry"); + + try + { + // This will upgrade the database schema if and only if we failed to do so on entry. + + if (SwarmDb.DbVersion < SwarmDb.DbVersionExpected) + { + BotLog.Write (1, "OneHour", "Entering DbUpgrade"); + DatabaseMaintenance.UpgradeSchemata(); + BotLog.Write (1, "OneHour", "Exited DbUpgrade"); + } + } + catch (Exception e) + { + TraceAndReport (e); + } + + + try + { + BotLog.Write(1, "OneHour", "Entering Automatch"); + Organizations organizations = Organizations.GetAll(); + foreach (Organization organization in organizations) + { + if (organization.IsEconomyEnabled) + { + BotLog.Write(1, "OneHour", "Automatching org #" + organization.Identity.ToString(CultureInfo.InvariantCulture)); + Payouts.AutomatchAgainstUnbalancedTransactions(organization); + } + } + BotLog.Write(1, "OneHour", "Exited Automatch"); + + /*TestTrace("Running PaymentGroupMapper.Run()..."); + PaymentGroupMapper.Run(); + TestTrace(" done.\r\n");*/ + } + catch (Exception e) + { + TraceAndReport (e); + } + + try + { + /*TestTrace("Running BlogTop50Scraper.Run()..."); + BlogTop50Scraper.Run(); + TestTrace(" done.\r\n");*/ + } + catch (Exception e) + { + TraceAndReport (e); + } + + try + { + /*TestTrace("Running Mappery.CreatePiratpartietOrganizationStrengthCircuitMap()..."); + Mappery.CreatePiratpartietOrganizationStrengthCircuitMap(); + TestTrace(" done.\r\n");*/ + } + catch (Exception e) + { + TraceAndReport (e); + } + + try + { + /*TestTrace("Running Mappery.CreatePiratpartietOrganizationStrengthCityMap()..."); + Mappery.CreatePiratpartietOrganizationStrengthCityMap(); + TestTrace(" done.\r\n");*/ + } + catch (Exception e) + { + TraceAndReport (e); + } + + BotLog.Write (0, "MainCycle", "One-hour exit"); + } + catch (Exception e) + { + TraceAndReport (e); + } + } + + private static void OnMidnight() + { + try + { + BotLog.Write (0, "MainCycle", "Midnight entry"); + + try + { + if (!testMode) + { + /*TestTrace("Running RosterHousekeeping.ChurnExpiredMembers()..."); + RosterHousekeeping.ChurnExpiredMembers(); + TestTrace(" done.\r\n");*/ + } + + ExchangeRateSnapshot.Create(); + } + catch (Exception e) + { + TraceAndReport (e); + } + + try + { + BotLog.DeleteOld (14); // delete logs older than 14 days + } + catch (Exception e) + { + TraceAndReport (e); + } + + try + { + /*TestTrace("Running SwedishForumMemberCheck.Run()..."); + SwedishForumMemberCheck.Run(); + TestTrace(" done.\r\n");*/ + } + catch (Exception e) + { + TraceAndReport (e); + } + + try + { + /*TestTrace("Running SalaryProcessor.Run()..."); + SalaryProcessor.Run(); + TestTrace(" done.\r\n");*/ + } + catch (Exception e) + { + TraceAndReport (e); + } + + try + { + /*TestTrace("Running TurnaroundTracker.Housekeeping()..."); + TurnaroundTracker.Housekeeping(); + TestTrace(" done.\r\n");*/ + } + catch (Exception e) + { + TraceAndReport (e); + } + + try + { + /*TestTrace("Running Mappery.CreateUngPiratUptakeMap()..."); + Mappery.CreateUngPiratUptakeMap(); + TestTrace(" done.\r\n");*/ + } + catch (Exception e) + { + TraceAndReport (e); + } + + try + { + /*TestTrace("Running RosterHousekeeping.TimeoutVolunteers()..."); + RosterHousekeeping.TimeoutVolunteers(); + TestTrace(" done.\r\n");*/ + } + catch (Exception e) + { + TraceAndReport (e); + } + + BotLog.Write (0, "MainCycle", "Midnight exit"); + } + catch (Exception e) + { + ExceptionMail.Send (e, true); + TestTrace (e.ToString()); + } + } + + private static void OnNoon() + { + BotLog.Write (0, "MainCycle", "Noon entry"); + + try + { + if (!PilotInstallationIds.IsPilot (PilotInstallationIds.PiratePartySE)) + { + // PPSE is still running PW4 code for this, so don't run for PPSE + + Payroll.ProcessMonthly(); // will only actually run on the 1st, but no harm in testing every noon + } + + // Check all bitcoin accounts for previously-unseen transactions once a day + + Organizations allOrganizations = Organizations.GetAll(); + foreach (Organization organization in allOrganizations) + { + // this actually checks hot storage too, but that's supposed + // to be up to date since we're the ones handling it + BitcoinUtility.CheckColdStorageForOrganization (organization); + } + + if (!testMode) + { + /*TestTrace("Running RosterHousekeeping.RemindAllExpiries()..."); + RosterHousekeeping.RemindAllExpiries(); + TestTrace(" done.\r\n");*/ + } + } + catch (Exception e) + { + TraceAndReport (e); + } + + try + { + if (!testMode) + { + /*TestTrace("Running SupportDatabase.NotifyBouncingEmails()..."); + SupportDatabase.NotifyBouncingEmails(); + TestTrace(" done.\r\n");*/ + } + } + catch (Exception e) + { + TraceAndReport (e); + } + + try + { + /*TestTrace("Running SupportDatabase.CloseDelayWarnings()..."); + SupportDatabase.CloseDelayWarnings(); + TestTrace(" done.\r\n");*/ + } + catch (Exception e) + { + TraceAndReport (e); + } + + try + { + /*TestTrace("Running SupportMailReview.Run()..."); + SupportMailReview.Run(); + TestTrace(" done.\r\n");*/ + } + catch (Exception e) + { + TraceAndReport (e); + } + + + BotLog.Write (0, "MainCycle", "Noon exit"); + } + + private static void OnMondayMorning() + { + try + { + // Detect and log any forex difference exceeding 100 cents. + + FinancialAccounts allAccounts = FinancialAccounts.GetAll(); // across ALL ORGS! + + foreach (FinancialAccount account in allAccounts) + { + // For every account, if it's based on foreign currency, check for forex gains/losses + + if (account.ForeignCurrency != null) + { + account.CheckForexProfitLoss(); + } + } + } + catch (Exception e) + { + ExceptionMail.Send(e, true); + if (testMode) + { + Console.WriteLine(e.ToString()); + } + } + } + + private static void OnTuesdayMorning() + { + try + { + //TestTrace("Running ActivityMailer.Run()..."); + // PirateWeb.Utility.BotCode.ActivityMailer.Run(); + //TestTrace(" done.\r\n"); + } + catch (Exception e) + { + ExceptionMail.Send (e, true); + if (testMode) + { + Console.WriteLine (e.ToString()); + } + } + } + + private static void TraceAndReport (Exception e) + { + TestTrace (" failed.\r\n"); + ExceptionMail.Send (e, true); + TestTrace (e.ToString()); + } + + private static void TestTrace (string message) + { + if (testMode) + { + Console.Write (message); + } + } + } } \ No newline at end of file diff --git a/FrontSocket/Frontend-Socket.csproj b/FrontSocket/Frontend-Socket.csproj index 5f7b994a..b0580c6a 100644 --- a/FrontSocket/Frontend-Socket.csproj +++ b/FrontSocket/Frontend-Socket.csproj @@ -57,7 +57,7 @@ - + diff --git a/FrontSocket/Program.cs b/FrontSocket/FrontendLoop.cs similarity index 95% rename from FrontSocket/Program.cs rename to FrontSocket/FrontendLoop.cs index 1b2d4835..990c7826 100644 --- a/FrontSocket/Program.cs +++ b/FrontSocket/FrontendLoop.cs @@ -1,5 +1,6 @@ using System; using System.Diagnostics; +using System.Globalization; using System.IO; using System.Net; using System.Net.Security; @@ -11,14 +12,22 @@ using Newtonsoft.Json.Linq; using Swarmops.Logic.Communications; using Swarmops.Logic.Communications.Payload; using Swarmops.Logic.ExtensionMethods; +using Swarmops.Logic.Support; using WebSocketSharp.Server; namespace Swarmops.Frontend.Socket { - internal class Program + internal class FrontendLoop { static void Main(string[] args) { + // Check if we're Sandbox + + if (PilotInstallationIds.IsPilot(PilotInstallationIds.DevelopmentSandbox)) + { + _isSandbox = true; + } + // Disable SSL cert checking (because Mono doesn't have a cert repo, dammit) Swarmops.Logic.Support.SupportFunctions.DisableSslCertificateChecks(); @@ -147,11 +156,27 @@ namespace Swarmops.Frontend.Socket Console.WriteLine(" * Swarmops Frontend Socket Server exiting"); } - private static WebSocketServer _socketServer; + private static WebSocketServer _socketServer; + private static bool _isSandbox = false; + private static int _sandboxDummy1 = 500; + private static int _sandboxDummy2 = 50000; private static void OnEveryTenSeconds() { _socketServer.WebSocketServices.Broadcast ("{\"messageType\":\"Heartbeat\"}"); + + if (_isSandbox) + { + _sandboxDummy1 += new Random().Next(10) - 3; + _sandboxDummy1 += new Random().Next(1000) - 200; + + JObject data = new JObject(); + data["messageType"] = "SandboxUpdate"; + data["local"] = _sandboxDummy1.ToString(CultureInfo.InvariantCulture); + data["profit"] = _sandboxDummy2.ToString(CultureInfo.InvariantCulture); + + _socketServer.WebSocketServices.Broadcast(data.ToString()); + } } diff --git a/FrontSocket/MasterServices.cs b/FrontSocket/MasterServices.cs index 68ed3a71..8cb93985 100644 --- a/FrontSocket/MasterServices.cs +++ b/FrontSocket/MasterServices.cs @@ -34,7 +34,7 @@ namespace Swarmops.Frontend.Socket switch (serverRequest) { case "UpdateQueueCounts": - Sessions.Broadcast (Program.GetQueueInfoJson()); + Sessions.Broadcast (FrontendLoop.GetQueueInfoJson()); break; case "Ping": Sessions.Broadcast("{\"messageType\":\"Heartbeat\"}"); @@ -62,7 +62,7 @@ namespace Swarmops.Frontend.Socket { Sessions.Broadcast ("{\"messageType\":\"EditorCount\"," + String.Format("\"editorCount\":\"{0}\"", Sessions.ActiveIDs.ToArray().Length) + '}'); - Send(Program.GetQueueInfoJson()); + Send(FrontendLoop.GetQueueInfoJson()); } } diff --git a/Site/Scripts/Swarmops-v5.js b/Site/Scripts/Swarmops-v5.js index a6435c53..d335e5d6 100644 --- a/Site/Scripts/Swarmops-v5.js +++ b/Site/Scripts/Swarmops-v5.js @@ -10,7 +10,7 @@ function _masterInitializeSocket(authenticationTicket) { _masterSocket = null; } - alertify.log("Opening socket " + getMasterSocketAddress() + "..."); + alertify.log("DEBUG: Opening socket " + getMasterSocketAddress() + "..."); var socketUrl = getMasterSocketAddress() + "?Auth=" + authenticationTicket; @@ -37,6 +37,12 @@ function _masterInitializeSocket(authenticationTicket) { if (message.messageType == "Heartbeat") { alertify.log("Master socket heartbeat"); + } + else if (message.messageType == "SandboxUpdate") { + if (odoLocalParticipation != undefined) { // Real ugly accessing specific page elements here, but it's temporary + odoLocalParticipation.innerHTML = message.local; + odoProfitLossToDate.innerHTML = (message.profit / 100.0) + 0.001; + } } }; } diff --git a/Site/apache-default-config.txt b/Site/apache-default-config.txt index b648d99e..3b17c817 100644 --- a/Site/apache-default-config.txt +++ b/Site/apache-default-config.txt @@ -2,11 +2,11 @@ ServerName swarmops-frontend ServerAdmin swarmops-frontend@swarmops.com DocumentRoot /usr/share/swarmops/frontend/ -# Redirect websocket requests to Swarmops FrontSocket server +# With mod_proxy enabled, kill the ability to make unsolicited proxy requests ProxyRequests Off -ProxyPass "/ws/" "ws://localhost:12172/" -# Explicitly specify Mono version 4 +# Point out application folder, explicitly specify Mono version 4 +MonoApplications swarmops-frontend "/:/usr/share/swarmops/frontend/" MonoServerPath swarmops-frontend "/usr/bin/mod-mono-server4" # Uncomment the line below to enable debugging: @@ -15,16 +15,30 @@ MonoServerPath swarmops-frontend "/usr/bin/mod-mono-server4" # Uncomment the line below and enter FQDNs (one or more) for Swarmops if # running virtual hosts: # ServerAlias FQDN1 FQDN2 FQDN3 ... - -MonoApplications swarmops-frontend "/:/usr/share/swarmops/frontend/" - + + +# Redirect websocket requests to Swarmops FrontSocket server + + Allow from all - Order allow,deny - SetHandler mono - MonoSetServerAlias swarmops-frontend - SetOutputFilter DEFLATE - SetEnvIfNoCase Request_URI "\.(?:gif|jpe?g|png)$" no-gzip dont-vary + ProxyPass "ws://localhost:12172/" + ProxyPassReverse "ws://localhost:12172/" + +# Trigger main handler only if Upgrade header is not [Ww]ebsocket +# (because in that particular case, we're passing to socket above) + + + + Allow from all + Order allow,deny + SetHandler mono + MonoSetServerAlias swarmops-frontend + SetOutputFilter DEFLATE + SetEnvIfNoCase Request_URI "\.(?:gif|jpe?g|png)$" no-gzip dont-vary + + + AddOutputFilterByType DEFLATE text/html text/plain text/xml text/javascript