Пример #1
0
        public void Process(StartMongoDbArgs args)
        {
            Log.Audit($"{this} Testing mongodb connection", this);

            var connectionStringSettings = ConfigurationManager.ConnectionStrings[_connectionStringName];

            if (connectionStringSettings == null)
            {
                Log.SingleError($"{this} Unable to determine MongoDB status because connection string name '{_connectionStringName}' was not found in the web.config.", this);
                args.AbortPipeline();
                return;
            }

            var driver = new MongoDbDriver(connectionStringSettings.ConnectionString);

            try
            {
                if (driver.DatabaseAvailable)
                {
                    Log.Audit($"{this} Mongo Already running", this);
                    args.AbortPipeline();
                    return;
                }

                var match = Regex.Match(connectionStringSettings.ConnectionString, @"mongodb://.+:(?<port>\d+)/");
                if (match.Success)
                {
                    var intVal = 0;
                    if (int.TryParse(match.Groups["port"].Value, out intVal))
                    {
                        args.Port = intVal;
                    }
                }
            }
            catch {}

            Log.Audit($"{this} Mongo Not Running", this);
        }
Пример #2
0
        public void Process(StartMongoDbArgs args)
        {
            if (string.IsNullOrWhiteSpace(args.ExePath))
            {
                Log.Error($"{this} Unable to start MongoDB because exe path was not specified.", this);
                return;
            }

            args.ExePath = Environment.ExpandEnvironmentVariables(args.ExePath);
            if (!File.Exists(args.ExePath))
            {
                Log.Error($"{this} Unable to start MongoDB because exe path {args.ExePath} was not found.", this);
                return;
            }

            if (string.IsNullOrWhiteSpace(args.DbFolderPath))
            {
                Log.Error($"{this} Unable to start MongoDB because no database folder path was configured.", this);
                return;
            }

            if (!Directory.Exists(args.DbFolderPath))
            {
                Directory.CreateDirectory(args.DbFolderPath);
            }

            if (!Directory.Exists(args.DbFolderPath))
            {
                Log.Error($"{this} Unable to create configured db folder path {args.DbFolderPath}", this);
                return;
            }

            var procArgs = $"--dbpath {args.DbFolderPath}";

            if (args.Port.HasValue && args.Port.Value > 0)
            {
                procArgs += $" --port {args.Port.Value}";
            }

            if (!string.IsNullOrWhiteSpace(args.AdditionalArgs))
            {
                procArgs += $" {args.AdditionalArgs}";
            }

            var startInfo = new ProcessStartInfo
            {
                CreateNoWindow  = true,
                UseShellExecute = false,
                FileName        = args.ExePath, //@"C:\Program Files\MongoDB 2.6 Standard\bin\mongod.exe",
                Arguments       = procArgs,
                WindowStyle     = ProcessWindowStyle.Hidden
            };

            try
            {
                Log.Audit($"{this} Trying to start mongo with command line {startInfo.FileName} {startInfo.Arguments}", this);
                var pid = 0;
                using (var exeProcess = System.Diagnostics.Process.Start(startInfo))
                {
                    pid = exeProcess.Id;
                    exeProcess.WaitForExit(50);
                }
                Log.Audit($"{this} MongoDb started. PID: {pid}", this);
            }
            catch (Exception exception)
            {
                Log.Error($"{this} Could not start mongo", exception, this);
            }
        }