Exemple #1
0
        /// <summary>
        /// Starts a new process.
        /// </summary>
        public IMongoDbProcess Start(string binariesDirectory, string dataDirectory, int port, bool doNotKill, bool singleNodeReplSet, string additionalMongodArguments, ushort singleNodeReplSetWaitTimeout = 5)
        {
            string fileName = @"{0}{1}{2}".Formatted(binariesDirectory, System.IO.Path.DirectorySeparatorChar.ToString(), MongoDbDefaults.MongodExecutable);

            string arguments = (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) ?
                               @"--dbpath ""{0}"" --port {1} --bind_ip 127.0.0.1".Formatted(dataDirectory, port) :
                               @"--sslMode disabled --dbpath ""{0}"" --port {1} --bind_ip 127.0.0.1".Formatted(dataDirectory, port);

            arguments  = singleNodeReplSet ? arguments + Space + "--replSet" + Space + ReplicaSetName : arguments;
            arguments += MongodArguments.GetValidAdditionalArguments(arguments, additionalMongodArguments);

            WrappedProcess wrappedProcess = ProcessControl.ProcessFactory(fileName, arguments);

            wrappedProcess.DoNotKill = doNotKill;

            string windowTitle = "mongod | port: {0}".Formatted(port);

            ProcessOutput output = ProcessControl.StartAndWaitForReady(wrappedProcess, 5, ProcessReadyIdentifier, windowTitle);

            if (singleNodeReplSet)
            {
                var replicaSetReady = false;

                // subscribe to output from mongod process and check for replica set ready message
                wrappedProcess.OutputDataReceived += (_, args) => replicaSetReady |= !string.IsNullOrWhiteSpace(args.Data) && args.Data.Contains(ReplicaSetReadyIdentifier);

                MongoClient client     = new MongoClient("mongodb://127.0.0.1:{0}/?connect=direct;replicaSet={1}".Formatted(port, ReplicaSetName));
                var         admin      = client.GetDatabase("admin");
                var         replConfig = new BsonDocument(new List <BsonElement>()
                {
                    new BsonElement("_id", ReplicaSetName),
                    new BsonElement("members",
                                    new BsonArray {
                        new BsonDocument {
                            { "_id", 0 }, { "host", "127.0.0.1:{0}".Formatted(port) }
                        }
                    })
                });
                var command = new BsonDocument("replSetInitiate", replConfig);
                admin.RunCommand <BsonDocument>(command);

                // wait until replica set is ready or until the timeout is reached
                SpinWait.SpinUntil(() => replicaSetReady, TimeSpan.FromSeconds(singleNodeReplSetWaitTimeout));

                if (!replicaSetReady)
                {
                    throw new TimeoutException($"Replica set initialization took longer than the specified timeout of {singleNodeReplSetWaitTimeout} seconds. Please consider increasing the value of {nameof(singleNodeReplSetWaitTimeout)}.");
                }
            }

            MongoDbProcess mongoDbProcess = new MongoDbProcess(wrappedProcess)
            {
                ErrorOutput    = output.ErrorOutput,
                StandardOutput = output.StandardOutput
            };

            return(mongoDbProcess);
        }
Exemple #2
0
        /// <summary>
        /// Output File: Absolute path stays unchanged, relative path will be relative to current executing directory (usually the /bin folder)
        /// </summary>
        public static ProcessOutput Export(string binariesDirectory, int port, string database, string collection, string outputFile)
        {
            string finalPath = FolderSearch.FinalizePath(outputFile);

            string fileName  = @"{0}\{1}".Formatted(binariesDirectory, MongoDbDefaults.MongoExportExecutable);
            string arguments = @"--host localhost --port {0} --db {1} --collection {2} --out ""{3}""".Formatted(port, database, collection, finalPath);

            Process process = ProcessControl.ProcessFactory(fileName, arguments);

            string windowTitle = "mongoexport | port: {0} db: {1} collection: {2} file {3}".Formatted(port, database, collection, outputFile);

            return(ProcessControl.StartAndWaitForExit(process, windowTitle));
        }
Exemple #3
0
        /// <summary>
        /// Starts a new process.
        /// </summary>
        public IMongoDbProcess Start(string binariesDirectory, string dataDirectory, int port, bool doNotKill, bool singleNodeReplSet, string additionalMongodArguments)
        {
            string fileName = @"{0}{1}{2}".Formatted(binariesDirectory, System.IO.Path.DirectorySeparatorChar.ToString(), MongoDbDefaults.MongodExecutable);

            string arguments = (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) ?
                               @"--dbpath ""{0}"" --port {1} --bind_ip 127.0.0.1".Formatted(dataDirectory, port) :
                               @"--sslMode disabled --dbpath ""{0}"" --port {1} --bind_ip 127.0.0.1".Formatted(dataDirectory, port);

            arguments  = singleNodeReplSet ? arguments + Space + "--replSet" + Space + ReplicaSetName : arguments;
            arguments += MongodArguments.GetValidAdditionalArguments(arguments, additionalMongodArguments);

            WrappedProcess wrappedProcess = ProcessControl.ProcessFactory(fileName, arguments);

            wrappedProcess.DoNotKill = doNotKill;

            string windowTitle = "mongod | port: {0}".Formatted(port);

            ProcessOutput output = ProcessControl.StartAndWaitForReady(wrappedProcess, 5, ProcessReadyIdentifier, windowTitle);

            if (singleNodeReplSet)
            {
                MongoClient client     = new MongoClient("mongodb://127.0.0.1:{0}/?connect=direct;replicaSet={1}".Formatted(port, ReplicaSetName));
                var         admin      = client.GetDatabase("admin");
                var         replConfig = new BsonDocument(new List <BsonElement>()
                {
                    new BsonElement("_id", ReplicaSetName),
                    new BsonElement("members",
                                    new BsonArray {
                        new BsonDocument {
                            { "_id", 0 }, { "host", "127.0.0.1:{0}".Formatted(port) }
                        }
                    })
                });
                var command = new BsonDocument("replSetInitiate", replConfig);
                admin.RunCommand <BsonDocument>(command);
                //Need to sleep here so the replica set initialization is complete
                Thread.Sleep(5000);
            }

            MongoDbProcess mongoDbProcess = new MongoDbProcess(wrappedProcess)
            {
                ErrorOutput    = output.ErrorOutput,
                StandardOutput = output.StandardOutput
            };

            return(mongoDbProcess);
        }
Exemple #4
0
        /// <summary>
        /// Input File: Absolute path stays unchanged, relative path will be relative to current executing directory (usually the /bin folder)
        /// </summary>
        public static ProcessOutput Import(string binariesDirectory, int port, string database, string collection, string inputFile, bool drop)
        {
            string finalPath = FolderSearch.FinalizePath(inputFile);

            if (!File.Exists(finalPath))
            {
                throw new FileNotFoundException("File not found", finalPath);
            }

            string fileName  = @"{0}\{1}".Formatted(binariesDirectory, MongoDbDefaults.MongoImportExecutable);
            string arguments = @"--host localhost --port {0} --db {1} --collection {2} --file ""{3}""".Formatted(port, database, collection, finalPath);

            if (drop)
            {
                arguments += " --drop";
            }

            Process process = ProcessControl.ProcessFactory(fileName, arguments);

            string windowTitle = "mongoimport | port: {0} db: {1} collection: {2} file {3}".Formatted(port, database, collection, inputFile);

            return(ProcessControl.StartAndWaitForExit(process, windowTitle));
        }
Exemple #5
0
        /// <summary>
        /// Starts a new process.
        /// </summary>
        public IMongoDbProcess Start(string binariesDirectory, string dataDirectory, int port, bool doNotKill)
        {
            string fileName = @"{0}{1}{2}".Formatted(binariesDirectory, System.IO.Path.DirectorySeparatorChar.ToString(), MongoDbDefaults.MongodExecutable);

            string arguments = (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) ?
                               @"--dbpath ""{0}"" --port {1} --nojournal --bind_ip 127.0.0.1".Formatted(dataDirectory, port) :
                               @"--sslMode disabled --dbpath ""{0}"" --port {1} --nojournal --bind_ip 127.0.0.1".Formatted(dataDirectory, port);

            WrappedProcess wrappedProcess = ProcessControl.ProcessFactory(fileName, arguments);

            wrappedProcess.DoNotKill = doNotKill;

            string windowTitle = "mongod | port: {0}".Formatted(port);

            ProcessOutput output = ProcessControl.StartAndWaitForReady(wrappedProcess, 5, ProcessReadyIdentifier, windowTitle);

            MongoDbProcess mongoDbProcess = new MongoDbProcess(wrappedProcess)
            {
                ErrorOutput    = output.ErrorOutput,
                StandardOutput = output.StandardOutput
            };

            return(mongoDbProcess);
        }