/// <summary> /// 定时器回调 /// </summary> private void OneRunTimerCallBack() { if (!IsMqAlive()) { if (_isFirstTimeToStart) { _isFirstTimeToStart = false; NLogHelper.Debug($"首次尝试启动mq"); StartMq(ex => { NLogHelper.Error($"首次尝试启动mq失败:{ex}"); }); } else { bool needToStartMq = false; _timerNonAliveCount++; if (_timerNonAliveCount > 1 || _rmqConfigList.Count == 1) { //count == 1表示单机版 if (_timerNonAliveCount > 1000000) { //避免溢出 _timerNonAliveCount = 100; } needToStartMq = true; } if (needToStartMq) { RabbitMqConfig oldConfig = GetCurrentUsedRmq(); //取下一个机器 lock (_locker) { _currentUsedRmqIndex++; } RabbitMqConfig newConfig = GetCurrentUsedRmq(); //第二次才尝试启动 StartMq(ex => { NLogHelper.Error($"尝试启动mq失败:{ex}"); }); //mq切机 _mqChangeCallback?.Invoke(oldConfig, newConfig); } } } else { _timerNonAliveCount = 0; } }
public static void Main(string[] args) { //红黑树 SortedDictionary <string, string> rbTree = new SortedDictionary <string, string>(); DirectoryHold.ResetCurrentDir(); NLogHelper.Info(GuidUtils.GetGuid32()); NLogHelper.Trace("Trace"); NLogHelper.Debug("Debug"); NLogHelper.Info("Info"); NLogHelper.Warn("Warn"); NLogHelper.Error("Error"); NLogHelper.Fatal("Fatal"); }
/// <summary> /// MQ回调 /// </summary> /// <param name="state"></param> private static void MQTimerCallBack(object state) { lock (locker) { if (_isRunning) { return; } _isRunning = true; } try { if (!MQConsumer.IsAlive()) { Thread.Sleep(5500); //延迟5s,使其能够自动恢复 if (!MQConsumer.IsAlive()) { if (_isFirstTimeToStartMq) { _isFirstTimeToStartMq = false; NLogHelper.Debug("首次尝试启动MQConsumer"); MQConsumer.InitConsumer(); } else { NLogHelper.Warn("MQ异常,尝试重启 MQConsumer"); MQConsumer.InitConsumer(); } } } else { NLogHelper.Trace("检测MQ连接正常"); } } catch (Exception ex) { NLogHelper.Error("MQ定时器异常:" + ex); } finally { lock (locker) { _isRunning = false; } } }
/// <summary> /// 关闭Producer /// </summary> public static void CloseProducer() { try { NLogHelper.Debug($"开始关闭MQ Producer:{MqUri}"); if (producer != null) { producer.Close(); producer = null; } } catch (Exception e) { NLogHelper.Error($"开始关闭MQ Producer失败:{e}"); } try { if (session != null) { session.Close(); session = null; } } catch (Exception e) { NLogHelper.Error($"关闭MQ Session失败:{e}"); } try { if (connection != null) { connection.Stop(); connection.Close(); connection = null; } } catch (Exception e) { NLogHelper.Error($"关闭MQ连接connection失败:{e}"); } lock (locker) { _isAlive = false; } }
public ActionResult Index() { Trace.WriteLine("一般信息"); Trace.TraceInformation("告知性信息"); Trace.TraceWarning("警告信息"); Trace.TraceError("错误信息"); // NLogHelper.Debug("log信息"); NLogHelper.Trace("Trace Message"); NLogHelper.Debug("Debug Message"); NLogHelper.Info("Info Message"); NLogHelper.Error("Error Message"); NLogHelper.Fatal("Fatal Message"); IQueryable <User> user = _userService.Select(c => true); return(View(user)); }
/// <summary> /// MQ回调 /// </summary> /// <param name="state"></param> private static void MQTimerCallBack(object state) { lock (locker) { if (isRunning) { return; } isRunning = true; } try { if (!MQLongConnectionProducer.IsAlive()) { //延迟5s,使其能够自动恢复 Thread.Sleep(5500); if (!MQLongConnectionProducer.IsAlive()) { if (_isFirstTimeToStartMq) { _isFirstTimeToStartMq = false; NLogHelper.Debug("首次尝试启动MQProducer"); MQLongConnectionProducer.StartProducer(); } else { NLogHelper.Warn("重启MQProducer"); MQLongConnectionProducer.StartProducer(); } } } } catch (Exception ex) { NLogHelper.Error("MQProducer定时器异常:" + ex); } finally { lock (locker) { isRunning = false; } } }
/// <summary> /// 实际的消息处理 /// </summary> /// <param name="message"></param> private static void HandleMessage(String message) { #region 实际消息处理 if (String.IsNullOrWhiteSpace(message)) { NLogHelper.Debug("消息为空,不做处理"); return; } NLogHelper.Debug($"开始处理消息:{message}"); //TODO: 实际的消息处理代码 #endregion }
/// <summary> /// MQ回调 /// </summary> /// <param name="state"></param> private static void MqTimerCallBack(object state) { lock (Slocker) { if (_isRunning) { return; } _isRunning = true; } try { if (!LongRunRabbitProducer.IsAlive()) { Thread.Sleep(5500); //延迟,使其能够自动恢复 if (!LongRunRabbitProducer.IsAlive()) { if (_isFirstTimeToStartMq) { _isFirstTimeToStartMq = false; NLogHelper.Debug("首次尝试启动MQConsumer"); LongRunRabbitProducer.Start(ex => { NLogHelper.Error($"开启MQ失败:{ex}"); }); } else { _restartCount++; if (_restartCount > 10000000) { _restartCount = 10000; } NLogHelper.Warn($"MQ异常,第{_restartCount}次尝试重启 MQConsumer"); LongRunRabbitProducer.Start(ex => { NLogHelper.Error($"开启MQ失败:{ex}"); }); } } } else { NLogHelper.Trace("检测MQ连接正常"); } } catch (Exception ex) { NLogHelper.Error("MQ定时器异常:" + ex); } finally { lock (Slocker) { _isRunning = false; } } }
public static void EmployeeUpdateForDingTalk(SqlSugarClient Edb, SqlSugarClient Ddb, List <V_EmployeeToDingTalk> ESB_EmployeeList) { NLogHelper log = NLogFactory.GetLogger("EmployeeUpdateForDingTalk"); List <V_EmployeeToDingTalk> EmpList = new List <V_EmployeeToDingTalk>(); //获取ESB中目前已经更新的数据 List <V_OperationObject> OperationList = Tbiz_OperationTempBll.GetOperationList(Edb, 1, 0); //根据ESB操作记录中更新的ID,查出目前的用户信息 List <V_EmployeeToDingTalk> EmployeeList = ESB_EmployeeList.Where(p => OperationList.Exists(q => q.UserId == p.UserId)).ToList(); log.Info("\r\n------------------------------------------------根据ESB更新钉钉中的人员信息------------------------------------------------\r\n"); #region 循环更新到钉钉 foreach (var item in EmployeeList) { if (item.Enabled == 1) { string EmployeeJson = GetEmployee(item.UserId); //用户ID在钉钉中不存在,即:离职用户重新录用时,应将该用户重新添加进钉钉 if (EmployeeJson.Equals("-1")) { AddEmployee(Edb, Ddb, item); EmpList.Add(item); continue; } else { EmployeeEntity model = Newtonsoft.Json.JsonConvert.DeserializeObject <EmployeeEntity>(EmployeeJson); string oldMobile = model.mobile; string DD_DepartmentId = "1"; DepartmentResult DD_DepModel = Ddb.Queryable <DepartmentResult>().With(SqlWith.NoLock).Where(it => it.ESB_DepartmentID.Equals(item.ESB_DepartmentId)).First(); if (DD_DepModel == null) { try { DD_DepartmentId = DepartmentForDingTalkBll.DD_DepartmentIsNullForDingTalk(Edb, Ddb, item.ESB_DepartmentId); } catch (Exception ex) { DD_DepartmentId = DepartmentForDingTalkBll.DD_DepartmentIsNullForDingTalk(Edb, Ddb, item.ESB_DepartmentId); } } else { DD_DepartmentId = DD_DepModel.id; } model.userid = item.UserId; model.name = item.Name; model.department = new List <int>(new int[] { Convert.ToInt32(DD_DepartmentId) }); model.position = item.PositionName; model.mobile = item.Mobile; model.tel = item.Telephone; model.email = item.Email; model.jobnumber = item.UserId; string param = JsonConvert.SerializeObject(model); Result Result = EmployeeBll.Update(param); if (Result != null) { if (Result.errcode == "0") { EmpList.Add(item); //Console.Write("更新成功," + Result.errmsg); } else { //UserID不存在 if (Result.errcode == "60111") { AddEmployee(Edb, Ddb, item); EmpList.Add(item); continue; } //更新手机号出错时 else if (Result.errcode == "40022" || Result.errcode == "40021" || Result.errcode == "60104" || Result.errcode == "60121") { //40021 更换的号码已注册过钉钉,无法使用该号码 //40022 企业中的手机号码和登陆钉钉的手机号码不一致,暂时不支持修改用户信息,可以删除后重新添加 //60104 手机号码在公司中已存在 //60121 找不到该用户 string Deletecode = EmployeeBll.Delete(model.userid).errcode; string Createcode = EmployeeBll.Create(JsonConvert.SerializeObject(model)).errcode; if (Createcode != "0") { Createcode = EmployeeBll.Create(JsonConvert.SerializeObject(model)).errcode; if (Createcode != "0" && Createcode != "40021") { log.Error("\r\n EmployeeUpdateForDingTalk - 行号135 更新钉钉人员信息时,成功删除员工信息,但是创建员工信息时报错,错误编码如下:" + Createcode + ", 员工编号为:" + model.userid); } } EmpList.Add(item); Task.Factory.StartNew(() => { InsertErroUpdateEmployee(model.userid, oldMobile, item.Mobile, Result.errcode); if (Deletecode != "0") { InsertErroUpdateEmployee(model.userid, oldMobile, item.Mobile, "删除失败,错误编号:" + Deletecode); } if (Createcode != "0") { InsertErroUpdateEmployee(model.userid, oldMobile, item.Mobile, "执行删除后创建失败,错误编号:" + Createcode); } }); //Console.Write("更新成功\r\n"); } else { //手机号码不合法 if (Result.errcode == "60103") { model.mobile = oldMobile; Result = EmployeeBll.Update(JsonConvert.SerializeObject(model)); if (Result.errcode == "0") { EmpList.Add(item); } else { log.Error("\r\n EmployeeForDingTalkBll-EmployeeUpdateForDingTalk() 失败后不更新手机号,还是失败,具体信息: " + Result.errmsg + "; UserId=" + item.UserId); } } //部门在钉钉中不存在的时候 else if (Result.errcode == "60003") { model.department = new List <int>(new int[] { 1 }); param = JsonConvert.SerializeObject(model); Result r = EmployeeBll.Update(param); if (r.errcode == "0") { EmpList.Add(item); } else if (r.errcode == "40022" || r.errcode == "40021" || r.errcode == "60104" || r.errcode == "60121") { //40021 更换的号码已注册过钉钉,无法使用该号码 //40022 企业中的手机号码和登陆钉钉的手机号码不一致,暂时不支持修改用户信息,可以删除后重新添加 //60104 手机号码在公司中已存在 //60121 找不到该用户 string Deletecode = EmployeeBll.Delete(model.userid).errcode; string Createcode = EmployeeBll.Create(JsonConvert.SerializeObject(model)).errcode; if (Createcode != "0") { Createcode = EmployeeBll.Create(JsonConvert.SerializeObject(model)).errcode; if (Createcode != "0" && Createcode != "40021") { log.Error("\r\n EmployeeUpdateForDingTalk - 行号199 更新钉钉人员信息时,成功删除员工信息,但是创建员工信息时报错,错误编码如下:" + Createcode); } } EmpList.Add(item); Task.Factory.StartNew(() => { InsertErroUpdateEmployee(model.userid, oldMobile, item.Mobile, r.errcode); if (Deletecode != "0") { InsertErroUpdateEmployee(model.userid, oldMobile, item.Mobile, "删除失败,错误编号:" + Deletecode); } if (Createcode != "0") { InsertErroUpdateEmployee(model.userid, oldMobile, item.Mobile, "执行删除后创建失败,错误编号:" + Createcode); } }); //Console.Write("更新成功\r\n"); } else { log.Debug("\r\n EmployeeForDingTalkBll-EmployeeUpdateForDingTalk() 钉钉中部,部门id=" + DD_DepartmentId + ",已将该人员挂在公司下," + Result.errmsg + "; UserId=" + item.UserId); Task.Factory.StartNew(() => { InsertErroUpdateEmployee(model.userid, oldMobile, item.Mobile, "更新用户时失败,错误编号:" + Result.errcode); }); } } else { log.Error("\r\n EmployeeForDingTalkBll-EmployeeUpdateForDingTalk() " + Result.errmsg + "; UserId=" + item.UserId); Task.Factory.StartNew(() => { InsertErroUpdateEmployee(model.userid, oldMobile, item.Mobile, "更新用户时失败,错误编号:" + Result.errcode); }); } } } } else { //Console.Write("无返回数据"); } } } else { Result Result = EmployeeBll.Delete(item.UserId); if (Result != null) { //找不到该用户 if (Result.errcode == "0" || Result.errcode == "60121") { EmpList.Add(item); //Console.Write("删除成功," + Result.errmsg + "\r\n"); } else { log.Error("\r\n EmployeeForDingTalkBll-EmployeeUpdateForDingTalk() " + Result.errmsg + "; UserId=" + item.UserId); //Console.Write("\r\n" + Result.errmsg + "; UserId=" + item.UserId); } } else { //Console.Write("无返回数据"); } } } int d = 0; foreach (var item in EmpList) { d = Edb.Deleteable <Tbiz_OperationTemp>().Where(it => it.ObjectId.Equals(item.UserId)).ExecuteCommand(); } #endregion }
public static bool AddEmployee(SqlSugarClient Edb, SqlSugarClient Ddb, V_EmployeeToDingTalk item) { bool result = true; NLogHelper log = NLogFactory.GetLogger("AddEmployee"); try { EmployeeEntity model = new EmployeeEntity(); string DD_DepartmentId = "1"; if (!item.ESB_DepartmentId.Equals("1000000001")) { DepartmentResult DD_DepModel = Ddb.Queryable <DepartmentResult>().With(SqlWith.NoLock).Where(it => it.ESB_DepartmentID.Equals(item.ESB_DepartmentId)).First(); if (DD_DepModel == null) { DD_DepartmentId = DepartmentForDingTalkBll.DD_DepartmentIsNullForDingTalk(Edb, Ddb, item.ESB_DepartmentId); } else { DD_DepartmentId = DD_DepModel.id; } } model.userid = item.UserId; model.name = item.Name; model.department = new List <int>(new int[] { Convert.ToInt32(DD_DepartmentId) }); model.position = item.PositionName; model.mobile = item.Mobile; model.tel = item.Telephone; model.workPlace = ""; model.remark = ""; model.email = item.Email; model.jobnumber = item.UserId; model.isSenior = false; string param = JsonConvert.SerializeObject(model); EmployeeResult Result = EmployeeBll.Create(param); if (Result != null) { if (Result.errcode == "0") { //Console.Write("创建成功,UserId=" + Result.userid); } //该外部联系人已存在 || 手机号码在公司中已存在 else if (Result.errcode == "40026" || Result.errcode == "60104") { string res = EmployeeBll.Delete(model.userid).errcode; if (res != "0") { log.Error("\r\n EmployeeForDingTalkBll-AddEmployee() 手机号码在公司中已存在删除时报错,错误编号:" + res); } EmployeeResult Result2 = EmployeeBll.Create(JsonConvert.SerializeObject(model)); if (Result2.errcode != "0") { if (Result2.errcode == "40022") { string Deletecode = EmployeeBll.Delete(model.userid).errcode; string Createcode = EmployeeBll.Create(JsonConvert.SerializeObject(model)).errcode; if (Createcode != "0") { Createcode = EmployeeBll.Create(JsonConvert.SerializeObject(model)).errcode; if (Createcode != "0" && Createcode != "40021") { log.Error("\r\n AddEmployee - 行号507 成功删除员工信息,但是创建员工信息时报错,错误编码如下:" + Createcode); result = false; } } //手机号码在公司中已存在 if (Result2.errcode == "60104") { log.Debug("\r\n EmployeeForDingTalkBll-AddEmployee() 手机号码在公司中已存在 时报错,信息如下" + Result2.errmsg + ",错误编码为:" + Result2.errcode + ", Json参数为:" + param); } else { log.Error("\r\n EmployeeForDingTalkBll-AddEmployee() 该外部联系人已存在 || 手机号码在公司中已存在 时报错,信息如下"+ Result2.errmsg + ",错误编码为:" + Result2.errcode + ", Json参数为:" + param); result = false; } Task.Factory.StartNew(() => { InsertErroUpdateEmployee(model.userid, "", item.Mobile, Result.errcode); if (Deletecode != "0") { InsertErroUpdateEmployee(model.userid, "", item.Mobile, "新增用户时,删除失败,错误编号:" + Deletecode); } if (Createcode != "0") { InsertErroUpdateEmployee(model.userid, "", item.Mobile, "新增用户时,执行删除后创建失败,错误编号:" + Createcode); } }); } else { log.Error("\r\n EmployeeForDingTalkBll-AddEmployee() " + Result.errmsg + ",错误编码为:" + Result.errcode); result = false; } } } else { if (Result.errcode == "60103") { log.Debug("\r\n EmployeeForDingTalkBll-AddEmployee() " + Result.errmsg + ",错误编码为:" + Result.errcode + ",手机号为" + item.Mobile + ",用户id为" + item.UserId + " Json参数为:" + param); result = true; } //UserID在公司中已存在 else if (Result.errcode == "60102") { log.Info("\r\n EmployeeForDingTalkBll-AddEmployee() " + Result.errmsg + ",错误编码为:" + Result.errcode); result = true; } else { log.Error("\r\n EmployeeForDingTalkBll-AddEmployee() " + Result.errmsg + ",错误编码为:" + Result.errcode); result = false; } Task.Factory.StartNew(() => { InsertErroUpdateEmployee(model.userid, "", item.Mobile, "新增用户时创建失败," + Result.errmsg + ",错误编号:" + Result.errcode); }); } } else { result = false; } } catch (Exception ex) { result = false; log.Error("\r\n EmployeeForDingTalkBll-AddEmployee() " + ex + "\r\n"); } return(result); }
public ResponseModel Post(string signature, string timestamp, string nonce, [FromBody] RequestModel model) { ResponseModel result = new ResponseModel(); var Ddb = DBHelper.Ddb; var Edb = DBHelper.Edb; _log.Info("请求参数为: signature=" + signature + "×tamp=" + timestamp + "&nonce=" + nonce + ", FromBody=" + JsonHelper.JsonSerializer(model) + "\n"); DingTalkCrypt dingTalkCrypt = new DingTalkCrypt(JsonConfigurationHelper.GetAppSettings("DingTalkSettings", "CallBack_Token"), JsonConfigurationHelper.GetAppSettings("DingTalkSettings", "CallBack_SuiteKey"), JsonConfigurationHelper.GetAppSettings("DingTalkSettings", "CorpID")); int decryptMsgcount = 1; int encryptMsgCount = 1; string dd_result = ""; string sEncryptMsg = ""; string msg_signature = ""; decryptMsgcount = dingTalkCrypt.DecryptMsg(signature, timestamp, nonce, model.encrypt, ref dd_result); result.timeStamp = timestamp; result.nonce = nonce; try { if (decryptMsgcount == 0) { ContactsEventModel contactsEventModel = JsonConvert.DeserializeObject <ContactsEventModel>(dd_result); if (contactsEventModel.EventType == "check_url") { encryptMsgCount = dingTalkCrypt.EncryptMsg("success", timestamp, nonce, ref sEncryptMsg, ref msg_signature); result.encrypt = sEncryptMsg; result.msg_signature = msg_signature; _log.Error(JsonConvert.SerializeObject(result)); return(result); } List <DingTalkCallBackLog> LogList = new List <DingTalkCallBackLog>(); int sqlExeCount = 0; if (contactsEventModel.UserId != null) { foreach (var item in contactsEventModel.UserId) { DingTalkCallBackLog log = new DingTalkCallBackLog { UserId = item, EventType = contactsEventModel.EventType, TimeStamp = contactsEventModel.TimeStamp }; LogList.Add(log); } if (LogList.Count > 0) { sqlExeCount = Ddb.Insertable(LogList).ExecuteCommand(); } if (sqlExeCount > 0) { encryptMsgCount = dingTalkCrypt.EncryptMsg("success", timestamp, nonce, ref sEncryptMsg, ref msg_signature); if (encryptMsgCount == 0) { List <DingTalkCallBackOperation> userList = new List <DingTalkCallBackOperation>(); //日志记录成功后,根据钉钉回调的员工,检查hr中该员工状态为在职的人员 var user = Edb.Queryable <V_EmployeeToDingTalk>().Where(it => contactsEventModel.UserId.Contains(it.UserId) && it.Enabled == 1).ToList(); foreach (var item in user) { //将员工为在职的人员重新添加到钉钉中 bool b = EmployeeForDingTalkBll.AddEmployee(Edb, Ddb, item); DingTalkCallBackOperation DD_User = new DingTalkCallBackOperation(); DD_User.EventType = contactsEventModel.EventType; DD_User.TimeStamp = contactsEventModel.TimeStamp; DD_User.UserId = item.UserId; DD_User.CreateDate = DateTime.Now; if (b) { DD_User.IsOperation = 1; } userList.Add(DD_User); } if (userList.Count > 0) { sqlExeCount = Ddb.Insertable(userList).ExecuteCommand(); _log.Debug(sqlExeCount + "条数据执行成功\n"); } result.encrypt = sEncryptMsg; result.msg_signature = msg_signature; } else { _log.Error("将消息加密,返回加密后字符串失败,返回加密前的参数为:" + dd_result + ",返回编码为: " + encryptMsgCount + "\n"); } } } else { _log.Error("钉钉传递的UserList为空,返回解密前的参数为:" + JsonHelper.JsonSerializer(model) + ",返回编码为: " + decryptMsgcount + "\n"); } } else { _log.Error("回调失败,请求参数为: signature = " + signature + " & timestamp = " + timestamp + " & nonce = " + nonce + ", FromBody = " + JsonHelper.JsonSerializer(model) + "\n错误详情如下:\n"); } } catch (Exception ex) { _log.Error("回调失败,请求参数为: signature = " + signature + " & timestamp = " + timestamp + " & nonce = " + nonce + ", FromBody = " + JsonHelper.JsonSerializer(model) + "\n错误详情如下:\n" + ex); } return(result); }
/// <summary> /// 开启连接 /// </summary> /// <returns></returns> public static bool StartProducer() { try { lock (locker) { if (connection != null) { CloseProducer(); } } NLogHelper.Debug("开始初始化MQ Producer"); //通过工厂构建连接 connection = factory.CreateConnection(); //设置唯一的客户端id connection.ClientId = Guid.NewGuid().ToString("N"); //错误事件 connection.ExceptionListener += ConnectionOnExceptionListener; //连接中断事件 connection.ConnectionInterruptedListener += ConnectionOnConnectionInterruptedListener; //连接重建事件 connection.ConnectionResumedListener += ConnectionOnConnectionResumedListener; //超时时间 connection.RequestTimeout = new TimeSpan(0, 0, 30); if (!connection.IsStarted) { connection.Start(); } //创建会话 session = connection.CreateSession(); session.RequestTimeout = new TimeSpan(0, 0, 30); //创建生产者 producer = session.CreateProducer(session.GetDestination(TopicOrQueueName, IsTopic ? DestinationType.Topic : DestinationType.Queue)); //最近一次通信时间 _lastCommunicateTime = DateTime.Now; lock (locker) { _isAlive = true; } NLogHelper.Debug($"MQ Producer初始化成功,{(IsTopic?"主题":"队列")}的名字为{TopicOrQueueName}"); return(true); } catch (Exception e) { lock (locker) { _isAlive = false; } NLogHelper.Error($"MQ Producer初始化失败:{e}"); return(false); } }