Пример #1
0
        private static void Main(string[] args)
        {
            var Windows = Type.GetType("Mono.Runtime") == null;

            //var ci = new CultureInfo("en-GB");
            //System.Threading.Thread.CurrentThread.CurrentCulture = ci;

            if (!Windows)
            {
                // Use reflection, so no attempt to load Mono dll on Windows
                Assembly   _posixAsm;
                Type       _unixSignalType, _signumType;
                MethodInfo _unixSignalWaitAny;

                _posixAsm          = Assembly.Load("Mono.Posix, Version=4.0.0.0, Culture=neutral, PublicKeyToken=0738eb9f132ed756");
                _unixSignalType    = _posixAsm.GetType("Mono.Unix.UnixSignal");
                _unixSignalWaitAny = _unixSignalType.GetMethod("WaitAny", new[] { _unixSignalType.MakeArrayType() });
                _signumType        = _posixAsm.GetType("Mono.Unix.Native.Signum");

                Array _signals = Array.CreateInstance(_unixSignalType, 1);
                _signals.SetValue(Activator.CreateInstance(_unixSignalType, _signumType.GetField("SIGTERM").GetValue(null)), 0);

                Thread signal_thread = new Thread(delegate()
                {
                    while (!exitSystem)
                    {
                        // Wait for a signal to be delivered
                        var id = (int)_unixSignalWaitAny.Invoke(null, new object[] { _signals });

                        cumulus.LogConsoleMessage("\nExiting system due to external SIGTERM signal");

                        exitSystem = true;
                    }
                });

                signal_thread.Start();

                // Now we need to catch the console Ctrl-C
                Console.CancelKeyPress += (s, ev) =>
                {
                    cumulus.LogConsoleMessage("Ctrl+C pressed");
                    cumulus.LogConsoleMessage("\nCumulus terminating");
                    cumulus.Stop();
                    Trace.WriteLine("Cumulus has shutdown");
                    ev.Cancel  = true;
                    exitSystem = true;
                };
            }
            else
            {
                // set the working path to the exe location
                Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
            }

            int  httpport = 8998;
            bool debug    = false;

            AppDomain.CurrentDomain.UnhandledException += UnhandledExceptionTrapper;


            for (int i = 0; i < args.Length; i++)
            {
                try
                {
                    if (args[i] == "-lang" && args.Length >= i)
                    {
                        var lang = args[++i];

                        CultureInfo.DefaultThreadCurrentCulture   = new CultureInfo(lang);
                        CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo(lang);
                    }
                    else if (args[i] == "-port" && args.Length >= i)
                    {
                        httpport = Convert.ToInt32(args[++i]);
                    }
                    else if (args[i] == "-debug")
                    {
                        // Switch on debug and and data logging from the start
                        debug = true;
                    }
                    else if (args[i] == "-wsport")
                    {
                        i++;
                        Console.WriteLine("The use of the -wsport command line parameter is deprecated");
                    }
                    else if (args[i] == "-install")
                    {
                        if (Windows)
                        {
                            if (SelfInstaller.InstallMe())
                            {
                                Console.WriteLine("Cumulus MX is now installed to run as service");
                                Environment.Exit(0);
                            }
                            else
                            {
                                Console.WriteLine("Cumulus MX failed to install as service");
                                Environment.Exit(1);
                            }
                        }
                        else
                        {
                            Console.WriteLine("You can only install Cumulus MX as a service in Windows");
                            Environment.Exit(1);
                        }
                    }
                    else if (args[i] == "-uninstall")
                    {
                        if (Windows)
                        {
                            if (SelfInstaller.UninstallMe())
                            {
                                Console.WriteLine("Cumulus MX is no longer installed to run as service");
                                Environment.Exit(0);
                            }
                            else
                            {
                                Console.WriteLine("Cumulus MX failed uninstall itself as service");
                                Environment.Exit(1);
                            }
                        }
                        else
                        {
                            Console.WriteLine("You can only uninstall Cumulus MX as a service in Windows");
                            Environment.Exit(1);
                        }
                    }
                    else if (args[i] == "-service")
                    {
                        service = true;
                    }
                    else
                    {
                        Console.WriteLine($"Invalid command line argument \"{args[i]}\"");
                        usage();
                    }
                }
                catch
                {
                    usage();
                }
            }

#if DEBUG
            debug = true;
            //Debugger.Launch();
#endif

            using (appMutex = new Mutex(false, "Global\\" + appGuid))
            {
                // Interactive seems to be always false under mono :(
                // So we need the no service flag & mono
                if (Environment.UserInteractive || (!service && !Windows))
                {
                    service = false;
                    RunAsAConsole(httpport, debug);
                }
                else
                {
                    var logfile = "MXdiags" + Path.DirectorySeparatorChar + "ServiceConsoleLog.txt";
                    svcTextListener = new TextWriterTraceListener(logfile);
                    service         = true;
                    if (File.Exists(logfile))
                    {
                        File.Delete(logfile);
                    }
                    svcTextListener = new TextWriterTraceListener(logfile);
                    RunAsAService();
                }

                while (!exitSystem)
                {
                    Thread.Sleep(500);
                }

                Environment.Exit(0);
            }
        }
Пример #2
0
        private static void Main(string[] args)
        {
            StartTime = DateTime.Now;
            var windows = Type.GetType("Mono.Runtime") == null;

            //var ci = new CultureInfo("en-GB");
            //System.Threading.Thread.CurrentThread.CurrentCulture = ci;

            if (windows)
            {
                // set the working path to the exe location
                Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
            }


            var logfile    = "MXdiags" + Path.DirectorySeparatorChar + "ServiceConsoleLog.txt";
            var logfileOld = "MXdiags" + Path.DirectorySeparatorChar + "ServiceConsoleLog-Old.txt";

            if (File.Exists(logfileOld))
            {
                File.Delete(logfileOld);
            }

            if (File.Exists(logfile))
            {
                File.Move(logfile, logfileOld);
            }

            svcTextListener = new TextWriterTraceListener(logfile);
            svcTextListener.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff ") + "Starting on " + (windows ? "Windows" : "Linux"));
            svcTextListener.Flush();

            if (!windows)
            {
                // Use reflection, so no attempt to load Mono dll on Windows
                svcTextListener.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff ") + "Creating SIGTERM monitor");
                svcTextListener.Flush();

                var posixAsm          = Assembly.Load("Mono.Posix, Version=4.0.0.0, Culture=neutral, PublicKeyToken=0738eb9f132ed756");
                var unixSignalType    = posixAsm.GetType("Mono.Unix.UnixSignal");
                var unixSignalWaitAny = unixSignalType.GetMethod("WaitAny", new[] { unixSignalType.MakeArrayType() });
                var signumType        = posixAsm.GetType("Mono.Unix.Native.Signum");

                var signals = Array.CreateInstance(unixSignalType, 1);
                signals.SetValue(Activator.CreateInstance(unixSignalType, signumType.GetField("SIGTERM").GetValue(null)), 0);

                Thread signalThread = new Thread(delegate()
                {
                    while (!exitSystem)
                    {
                        // Wait for a signal to be delivered
                        unixSignalWaitAny?.Invoke(null, new object[] { signals });

                        var msg = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff ") + "Exiting system due to external SIGTERM signal";
                        Console.WriteLine(msg);
                        svcTextListener.WriteLine(msg);

                        if (cumulus != null)
                        {
                            msg = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff ") + "Cumulus terminating";
                            Console.WriteLine(msg);
                            svcTextListener.WriteLine(msg);
                            cumulus.LogMessage("Exiting system due to external SIGTERM signal");
                            cumulus.LogMessage("Cumulus terminating");
                            cumulus.Stop();
                            //allow main to run off
                            Thread.Sleep(500);
                            msg = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff ") + "Cumulus has shutdown";
                            Console.WriteLine(msg);
                            svcTextListener.WriteLine(msg);
                        }

                        exitSystem = true;
                    }
                });

                signalThread.Start();

                // Now we need to catch the console Ctrl-C
                Console.CancelKeyPress += (s, ev) =>
                {
                    if (cumulus != null)
                    {
                        cumulus.LogConsoleMessage("Ctrl+C pressed");
                        cumulus.LogConsoleMessage("\nCumulus terminating");
                        cumulus.Stop();
                        //allow main to run off
                        Thread.Sleep(500);
                    }
                    Trace.WriteLine("Cumulus has shutdown");
                    ev.Cancel  = true;
                    exitSystem = true;
                };
            }

            AppDomain.CurrentDomain.UnhandledException += UnhandledExceptionTrapper;

#if DEBUG
            debug = true;
            //Debugger.Launch();
#endif

            for (int i = 0; i < args.Length; i++)
            {
                try
                {
                    switch (args[i])
                    {
                    case "-lang" when args.Length >= i:
                    {
                        var lang = args[++i];

                        CultureInfo.DefaultThreadCurrentCulture   = new CultureInfo(lang);
                        CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo(lang);
                        break;
                    }

                    case "-port" when args.Length >= i:
                        httpport = Convert.ToInt32(args[++i]);
                        break;

                    case "-debug":
                        // Switch on debug and and data logging from the start
                        debug = true;
                        break;

                    case "-wsport":
                        i++;
                        Console.WriteLine("The use of the -wsport command line parameter is deprecated");
                        break;

                    case "-install" when windows:
                    {
                        if (SelfInstaller.InstallMe())
                        {
                            Console.WriteLine("Cumulus MX is now installed to run as service");
                            Environment.Exit(0);
                        }
                        else
                        {
                            Console.WriteLine("Cumulus MX failed to install as service");
                            Environment.Exit(1);
                        }

                        break;
                    }

                    case "-install":
                        Console.WriteLine("You can only install Cumulus MX as a service in Windows");
                        Environment.Exit(1);
                        break;

                    case "-uninstall" when windows:
                    {
                        if (SelfInstaller.UninstallMe())
                        {
                            Console.WriteLine("Cumulus MX is no longer installed to run as service");
                            Environment.Exit(0);
                        }
                        else
                        {
                            Console.WriteLine("Cumulus MX failed uninstall itself as service");
                            Environment.Exit(1);
                        }

                        break;
                    }

                    case "-uninstall":
                        Console.WriteLine("You can only uninstall Cumulus MX as a service in Windows");
                        Environment.Exit(1);
                        break;

                    case "-service":
                        service = true;
                        break;

                    default:
                        Console.WriteLine($"Invalid command line argument \"{args[i]}\"");
                        Usage();
                        break;
                    }
                }
                catch
                {
                    Usage();
                }
            }

            using (appMutex = new Mutex(false, "Global\\" + AppGuid))
            {
                // Interactive seems to be always false under mono :(

                if (windows && !Environment.UserInteractive)
                {
                    // Windows and not interactive - must be a service
                    svcTextListener.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff ") + "Running as a Windows service");
                    svcTextListener.Flush();
                    service = true;
                    // Launch as a Windows Service
                    ServiceBase.Run(new CumulusService());
                }
                else
                {
                    if (Environment.UserInteractive || (!windows && !service))
                    {
                        // Windows interactive or Linux and no service flag
                        svcTextListener.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff ") + "Running interactively");
                        service = false;
                    }
                    else
                    {
                        // Must be a Linux service
                        svcTextListener.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff ") + "Running as a Linux service");
                        service = true;
                    }
                    svcTextListener.Flush();
                    // Lauch normally - Linux Service runs like this too
                    RunAsAConsole(httpport, debug);
                }

                while (!exitSystem)
                {
                    Thread.Sleep(500);
                }

                Environment.Exit(0);
            }
        }