Exemple #1
0
        static void ConfigureLogging(ServerOptions options)
        {
            var layout = new PatternLayout("%date %level - %message%newline");
            layout.ActivateOptions();

            var appenders = new List<IAppender>();
            if (!string.IsNullOrEmpty(options.LogFileDirectory))
            {
                var fileAppender = new FileAppender
                {
                    Layout = layout,
                    Encoding = Encoding.UTF8,
                    File = Path.Combine(options.LogFileDirectory, "orchestrion.log"),
                    AppendToFile = true,
                    LockingModel = new FileAppender.MinimalLock(),
                    ImmediateFlush = true,

                };
                fileAppender.ActivateOptions();
                appenders.Add(fileAppender);
            }

            if (options.ConsoleOutput)
            {
                var consoleAppender = new ConsoleAppender
                {
                    Layout = layout
                };
                consoleAppender.ActivateOptions();
                appenders.Add(consoleAppender);
            }

            BasicConfigurator.Configure(appenders.ToArray());
        }
Exemple #2
0
        private static void WatchProcess(ServerOptions serverOptions)
        {
            var logger = LogManager.GetLogger(typeof (Program));
            try
            {
                Debug.Assert(serverOptions.ParentProcessId != null, "serverOptions.ParentProcessId != null");
                var parent = Process.GetProcessById(serverOptions.ParentProcessId.Value);
                parent.WaitForExit();
            }
            catch (Exception e)
            {
                logger.Error("Can't get parent process. Stopped watching." + e.Message);
                return;
            }

            try
            {
                // Parent process exited. Killing this process
                logger.Info("Parent process exited. Quitting..");
                var request =
                    WebRequest.Create(string.Format("http://{0}:{1}/?command=quit", serverOptions.Host,
                                                    serverOptions.Port));
                var response = (HttpWebResponse) request.GetResponse();
                if (response.StatusCode != HttpStatusCode.OK)
                {
                    logger.ErrorFormat("Normal exit failed. Server returned - {0}", response.StatusCode);
                    Environment.Exit(1);
                }
            }
            catch (Exception e)
            {
                logger.ErrorFormat("Normal exit failed. {0}. Force killing...", e.Message);
                Environment.Exit(1);
            }
        }
Exemple #3
0
        static string[] SetPort(string currentOption, string[] args, ServerOptions options)
        {
            if (!args.Any())
                throw new InvalidOptionValueException(currentOption, string.Empty, "Expected a port to be present");

            int port;
            if (!int.TryParse(args.First(), out port))
                throw new InvalidOptionValueException(currentOption, args.First(), "Port should be a number");

            options.Port = port;

            return args.Skip(1).ToArray();
        }
Exemple #4
0
        static void WatchParentProcess(ServerOptions serverOptions)
        {
            if (!serverOptions.ParentProcessId.HasValue)
                return;

            var watcher = new Thread(() => WatchProcess(serverOptions)){IsBackground = true};
            watcher.Start();
        }
Exemple #5
0
        static string[] SetParent(string currentOption, string[] args, ServerOptions options)
        {
            if (args.Length == 0)
                throw new InvalidOptionValueException(currentOption, string.Empty, "Expected a process id");

            int id;
            if (!int.TryParse(args.First(), out id))
                throw new InvalidOptionValueException(currentOption, args.First(), "Process id is not a number");

            options.ParentProcessId = id;

            return args.Skip(1).ToArray();
        }
Exemple #6
0
        static string[] SetLogsDirectory(string currentOption, string[] args, ServerOptions options)
        {
            if (!args.Any())
                throw new InvalidOptionValueException(currentOption, string.Empty, "Expected a logs directory present");

            string logsDir = args.First();
            if (!Directory.Exists(logsDir))
                throw new InvalidOptionValueException(currentOption, logsDir, "Directory doesn't exists");

            if (!IsDirectoryWritable(logsDir))
                throw new InvalidOptionValueException(currentOption, logsDir, "Directory is not writable");

            options.LogFileDirectory = args.First();

            return args.Skip(1).ToArray();
        }
Exemple #7
0
        static string[] SetHost(string currentOption, string[] args, ServerOptions options)
        {
            if (!args.Any())
                throw new InvalidOptionValueException(currentOption, string.Empty, "Expected a host name to be present");

            options.Host = args.First();

            return args.Skip(1).ToArray();
        }
Exemple #8
0
        static string[] SetConsoleOutput(string currentOption, string[] args, ServerOptions options)
        {
            if (args.Length == 0)
                throw new InvalidOptionValueException(currentOption, string.Empty, "Expected a boolean value");

            options.ConsoleOutput = args.First() == Boolean.TrueString;

            return args.Skip(1).ToArray();
        }
Exemple #9
0
        static ServerOptions ProcessArgs(string[] args)
        {
            string[] toprocess = args;
            var serverOptions = new ServerOptions();
            while (toprocess.Length > 0)
            {
                string option = toprocess.First();
                if (!commandLineOptions.ContainsKey(option))
                    throw new UnknownOptionException(option);

                toprocess = commandLineOptions[option](option, toprocess.Skip(1).ToArray(), serverOptions);
            }

            return serverOptions;
        }