123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- using Flyinsono.DBCopy.Tool.RpcService;
- using JsonRpcLite.Network;
- using JsonRpcLite.Rpc;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Net;
- using System.Text.Json;
- using WingInterfaceLibrary.Enum;
- using WingInterfaceLibrary.Interface;
- using WingInterfaceLibrary.Request.User;
- using MongoDB.Bson;
- using MongoDB.Driver;
- using Flyinsono.DBCopy.Tool.Utilities;
- using System.Reflection.Metadata;
- using MongoDB.Bson.IO;
- using Flyinsono.DBCopy.Tool.Entities;
- using System.Threading.Tasks;
- using Flyinsono.DBCopy.Tool.Service;
- namespace Flyinsono.DBCopy.Tool
- {
- internal interface IClientTestManager : IClientManager
- {
-
-
-
- void ConnectDb(string dbPath);
- }
- internal class ClientTestManager : IClientTestManager
- {
- public void Dispose()
- {
-
- }
- public void ConnectDb(string dbPath)
- {
- Console.WriteLine($"Database path: {dbPath}");
-
- string mongodPath = @"DBCopy.Packages\3.6.20\mongod_3.6.20.exe";
- int port = 8333;
- StartLocalMongoDbServer(mongodPath, dbPath, port);
- CommonConfigManager.IsLocalHost = true;
- MongoDbClientSingle.Instance = new MongoDbClient();
- }
-
- private void StartLocalMongoDbServer(string mongodPath, string dbPath, int port, float cacheSize = 0.5f)
- {
- try
- {
- bool res = false;
- Process[] processes = Process.GetProcessesByName("mongod_4.2");
- if (processes.Length != 0)
- {
- res = true;
- return;
- }
- Logger.WriteLineInfo($"Mongodb server starting");
- var wiredTigerCacheSizeGB = CommonConfigManager.FilterGeneral.WiredTigerCacheSizeGB;
-
-
-
-
- var arguments = $"--dbpath \"{dbPath}\" --port {port} --maxConns 2000 --noIndexBuildRetry --nojournal";
- if (wiredTigerCacheSizeGB > 0)
- {
- arguments += " --wiredTigerCacheSizeGB " + wiredTigerCacheSizeGB;
- }
- var startInfo = new ProcessStartInfo
- {
- FileName = mongodPath,
- Arguments = arguments,
- WindowStyle = ProcessWindowStyle.Hidden,
- RedirectStandardInput = true,
- RedirectStandardOutput = true,
- UseShellExecute = false,
- CreateNoWindow = true
- };
- using (var process = Process.Start(startInfo))
- {
- int readCount = 100;
- string info = string.Empty;
- while (readCount > 0)
- {
- info = process.StandardOutput.ReadLine();
- if (!string.IsNullOrWhiteSpace(info) && info.IndexOf($"waiting for connections on port {port}") > -1)
- {
- Logger.WriteLineInfo($"Mongodb server started.{readCount}");
-
- break;
- }
-
- readCount--;
- }
- Logger.WriteLineInfo("Mongodb server started.");
- }
- }
- catch (Exception exception)
- {
- Logger.WriteLineError($"Start mongod server process filed,error:{exception}");
- throw;
- }
- }
- }
- }
|