초기커밋
This commit is contained in:
61
ServerBase/DB/MongoDb/MongoDbConfig.cs
Normal file
61
ServerBase/DB/MongoDb/MongoDbConfig.cs
Normal file
@@ -0,0 +1,61 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
|
||||
using ServerCore;
|
||||
|
||||
|
||||
namespace ServerBase;
|
||||
|
||||
public partial class MongoDbConnector : MongoDbConnectorBase, IModule, IInitializer
|
||||
{
|
||||
public class ConfigParam : IConfigParam
|
||||
{
|
||||
public string ServiceType { get; set; } = string.Empty;
|
||||
public string DatabaseName { get; set; } = string.Empty;
|
||||
public string ConnectionString { get; set; } = string.Empty;
|
||||
public int MinConnectionPoolSize { get; set; } = 0;
|
||||
public int MaxConnectionPoolSize { get; set; } = 100;
|
||||
public int WaitQueueTimeoutSecs { get; set; } = 120;
|
||||
|
||||
public Result tryReadFromJsonOrDefault(JObject jObject)
|
||||
{
|
||||
var result = new Result();
|
||||
var err_msg = string.Empty;
|
||||
|
||||
try
|
||||
{
|
||||
ServiceType = jObject["ServiceType"]?.Value<string>() ?? ServiceType;
|
||||
|
||||
var mongo_db = jObject["MongoDb"];
|
||||
NullReferenceCheckHelper.throwIfNull(mongo_db, () => $"mongo_db is null !!!");
|
||||
ConnectionString = mongo_db["ConnectionString"]?.Value<string>() ?? ConnectionString;
|
||||
DatabaseName = mongo_db["DatabaseName"]?.Value<string>() ?? DatabaseName;
|
||||
MinConnectionPoolSize = mongo_db["MinConnectionPoolSize"]?.Value<int>() ?? MinConnectionPoolSize;
|
||||
MaxConnectionPoolSize = mongo_db["MaxConnectionPoolSize"]?.Value<int>() ?? MaxConnectionPoolSize;
|
||||
WaitQueueTimeoutSecs = mongo_db["WaitQueueTimeoutSecs"]?.Value<int>() ?? WaitQueueTimeoutSecs;
|
||||
|
||||
return result;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
var error_code = ServerErrorCode.TryCatchException;
|
||||
err_msg = $"Exception !!!, Failed to perform in tryReadFromJsonOrDefault() !!! : errorCode:{error_code}, exception:{e} - {this.getTypeName()}";
|
||||
result.setFail(error_code, err_msg);
|
||||
Log.getLogger().error(err_msg);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public string toBasicString()
|
||||
{
|
||||
return $"ConfigParam: ServiceType:{ServiceType}, ConnectionStringOfMongoDb:{ConnectionString}, MinConnectionPoolSize:{MinConnectionPoolSize}, MaxConnectionPoolSize:{MaxConnectionPoolSize}, WaitQueueTimeoutSecs:{WaitQueueTimeoutSecs}";
|
||||
}
|
||||
};
|
||||
}
|
||||
62
ServerBase/DB/MongoDb/MongoDbConnector.cs
Normal file
62
ServerBase/DB/MongoDb/MongoDbConnector.cs
Normal file
@@ -0,0 +1,62 @@
|
||||
using MongoDB.Driver;
|
||||
|
||||
|
||||
using ServerCore;
|
||||
|
||||
|
||||
namespace ServerBase;
|
||||
|
||||
public partial class MongoDbConnector : MongoDbConnectorBase, IModule, IInitializer
|
||||
{
|
||||
private readonly ModuleContext m_module_context;
|
||||
|
||||
public MongoDbConnector(ModuleContext moduleContext)
|
||||
: base()
|
||||
{
|
||||
m_module_context = moduleContext;
|
||||
}
|
||||
|
||||
public async Task<Result> onInit()
|
||||
{
|
||||
var err_msg = string.Empty;
|
||||
var result = new Result();
|
||||
|
||||
var module_context = getModuleContext();
|
||||
var config_param = module_context.getConfigParam() as ConfigParam;
|
||||
NullReferenceCheckHelper.throwIfNull(config_param, () => $"config_param is null !!!");
|
||||
|
||||
(var is_success, var db_list) = await initAndVerifyDb( config_param.ConnectionString
|
||||
, config_param.MinConnectionPoolSize
|
||||
, config_param.MaxConnectionPoolSize
|
||||
, config_param.WaitQueueTimeoutSecs );
|
||||
if(false == is_success)
|
||||
{
|
||||
err_msg = $"Failed to initAndVerifyDb() !!! : {config_param.toBasicString()}";
|
||||
result.setFail(ServerErrorCode.MongoDbInitAndVefifyDbFailed, err_msg);
|
||||
Log.getLogger().error(result.toBasicString());
|
||||
return result;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public async Task<Result> startModule()
|
||||
{
|
||||
var result = new Result();
|
||||
return await Task.FromResult(result);
|
||||
}
|
||||
|
||||
public async Task<Result> stopModule()
|
||||
{
|
||||
var result = new Result();
|
||||
return await Task.FromResult(result);
|
||||
}
|
||||
|
||||
|
||||
public ModuleContext getModuleContext() => m_module_context;
|
||||
|
||||
public string toBasicString()
|
||||
{
|
||||
return $"{m_module_context.toBasicString()} - {this.getTypeName()}";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user