public bool ScriptCancel(ref ErrorInfo err, int scriptID) { try { var qrs = ProInterface.JSON.EncodeToEntity <IList <QuartzRunStatus> >(System.IO.File.ReadAllText(statusLogPath)); var nowQrs = qrs.SingleOrDefault(x => x.JobName == "ScriptID_" + scriptID); qrs.Remove(nowQrs); Fun.WriteAllText(statusLogPath, ProInterface.JSON.DecodeToStr(qrs)); return(true); } catch (Exception e) { err.IsError = true; err.Message = e.Message; return(false); } }
private string down_db(string sql, string tableName, int pageSize = 1000000) { if (!IsRun()) { return(null); } DbDataReader dr = null; object conn = new object(); try { switch (_dbType) { case "ORACLE": conn = new OracleConnection(_connStr); dr = DbHelper.OracleHelper.ExecuteReader((OracleConnection)conn, CommandType.Text, sql); break; } log(string.Format("执行查询语句成功【{0}】", sql)); } catch (Exception e) { ErrorScriptTask(e); log(string.Format("执行查询语句失败【{0}】", sql)); log(string.Format("错误原因【{0}】", e.Message)); return(""); } var nowPath = _downDbPath + tableName + "\\"; if (!Directory.Exists(nowPath)) { Directory.CreateDirectory(nowPath); } foreach (var t in Directory.GetFiles(nowPath)) { File.Delete(t); } var tableFiled = new List <TableFiled>(); for (int i = 0; i < dr.FieldCount; i++) { tableFiled.Add(new TableFiled { Code = dr.GetName(i), DataType = dr.GetDataTypeName(i), CSharpType = dr.GetFieldType(i).Name }); } Fun.WriteAllText(nowPath + tableName + "_Create.txt", MakeCreateTableSql(tableFiled, tableName, _dbType)); Fun.WriteAllText(nowPath + tableName + "_TableFiled.txt", JSON.DecodeToStr(tableFiled)); IList <string> allData = new List <string>(); int rowNum = 0; int pageNum = 1; log(string.Format("开始下载数据")); while (dr.Read()) { rowNum++; IList <string> rowData = new List <string>(); for (int i = 0; i < dr.FieldCount; i++) { if (dr[i] == null || dr[i] == DBNull.Value) { rowData.Add(null); } else { rowData.Add(dr[i].ToString().Replace(",", ",,")); } } allData.Add(string.Join(",", rowData)); if (rowNum == pageSize) { File.WriteAllLines(nowPath + tableName + "_" + pageNum + ".txt", allData); log(string.Format("下载完成第【{0}】页数据", pageNum)); allData = new List <string>(); rowNum = 0; pageNum++; } } log(string.Format("结束下载,共【{0}】页,【{1}】条数据", pageNum, (pageNum - 1) * pageSize + rowNum)); if (rowNum != 0) { File.WriteAllLines(nowPath + tableName + "_" + pageNum + ".txt", allData); allData = new List <string>(); } dr.Close(); dr.Dispose(); switch (_dbType) { case "ORACLE": var oralceConn = (OracleConnection)conn; oralceConn.Close(); oralceConn.Dispose(); break; } return(nowPath); }
public void Execute(IJobExecutionContext context) { #region 添加脚本任务 ISchedulerFactory schedulerFactory = new StdSchedulerFactory(); IScheduler scheduler = schedulerFactory.GetScheduler(); string jobGroupName = "ScriptJobGroup"; string triGroupName = "ScriptTriGroup"; string jobNamePex = "ScriptJob_"; string triNamePex = "ScriptTri_"; //所有需要运行的脚本 var allScript = AllScript(); var triKeyArr = scheduler.GetTriggerKeys(GroupMatcher <TriggerKey> .GroupEquals("ScriptTriGroup")); //删除触发器,删除这个触发器没有在运行的脚本里 foreach (var t in triKeyArr) { var trigger = scheduler.GetTrigger(t); IJobDetail job = scheduler.GetJobDetail(trigger.JobKey); var tmp = allScript.SingleOrDefault(x => t.Name == triNamePex + x.ID.ToString()); if (tmp == null) { StopTask(Convert.ToInt32(t.Name.Replace(triNamePex, ""))); scheduler.DeleteJob(trigger.JobKey); logger.InfoFormat("脚本服务 移除触发器ID{0}", t.Name); } } foreach (var t in allScript) { try { //新任务 if (triKeyArr.SingleOrDefault(x => x.Name == triNamePex + t.ID.ToString()) == null) { IJobDetail job = JobBuilder.Create <ScriptTaskAddQuartz>() .WithIdentity(new JobKey(jobNamePex + t.ID.ToString(), jobGroupName)) .StoreDurably() .Build(); ICronTrigger trigger = (ICronTrigger)TriggerBuilder.Create() .WithIdentity(new TriggerKey(triNamePex + t.ID.ToString(), triGroupName)) .ForJob(job) .StartNow().WithCronSchedule(t.RUN_WHEN) .Build(); logger.InfoFormat("脚本服务 添加脚本触发器ID{0}", trigger.Key.Name); scheduler.ScheduleJob(job, trigger); } else { ICronTrigger trigger = (ICronTrigger)scheduler.GetTrigger(new TriggerKey(triNamePex + t.ID.ToString(), triGroupName)); IJobDetail job = scheduler.GetJobDetail(trigger.JobKey); if (trigger.CronExpressionString != t.RUN_WHEN) { logger.InfoFormat("脚本服务 修改触发器【{0}】的时间表达式【{1}】为【{2}】", trigger.Key.Name, trigger.CronExpressionString, t.RUN_WHEN); trigger.CronExpressionString = t.RUN_WHEN; scheduler.DeleteJob(trigger.JobKey); scheduler.ScheduleJob(job, trigger); } } } catch (Exception e) { Dictionary <string, object> dicStart = new Dictionary <string, object>(); dicStart.Add("STATUS", "禁用"); FunSqlToClass.UpData <SCRIPT>( ConfigurationManager.AppSettings["dbType"], ConfigurationManager.AppSettings["dbConnSt"], dicStart, string.Format("where ID={0} ", t.ID), ConfigurationManager.AppSettings["dbPrefix"]); logger.InfoFormat("脚本服务 添加脚本触发器任务【{0}】,失败【{1}】", t.CODE, e.Message); } } #endregion #region 运行脚本任务 //获取正在等待的任务 foreach (var task in AllScriptTask()) { try { #region 检测运算实例是否存在,并把结束了的线程终止 QuartzRunStatus nowQrs = new QuartzRunStatus(); IList <QuartzRunStatus> qrs = new List <QuartzRunStatus>(); try { qrs = ProInterface.JSON.EncodeToEntity <IList <QuartzRunStatus> >(System.IO.File.ReadAllText(statusLogPath)); } catch { } //清理2小时还没有远行完的口径 foreach (var t in qrs.Where(x => x.StatusTime.AddHours(2) < DateTime.Now).ToList()) { qrs.Remove(t); } if (qrs == null) { qrs = new List <QuartzRunStatus>(); } nowQrs = qrs.SingleOrDefault(x => x.JobName == "ScriptID_" + task.SCRIPT_ID); if (nowQrs == null) { nowQrs = new QuartzRunStatus(); } //表示该脚本正在运行,则退出 if (nowQrs.IsRun) { continue; } if (qrs.Count > ProInterface.AppSet.ScriptRunMaxNum) { logger.InfoFormat("执行脚本数【{0}】已经超过最大任务数【{1}】了", qrs.Count, ProInterface.AppSet.ScriptRunMaxNum); return; } nowQrs.IsRun = true; nowQrs.JobName = "ScriptID_" + task.SCRIPT_ID; nowQrs.StatusTime = DateTime.Now; if (qrs.SingleOrDefault(x => x.JobName == "ScriptID_" + task.SCRIPT_ID) == null) { qrs.Add(nowQrs); } Fun.WriteAllText(statusLogPath, ProInterface.JSON.DecodeToStr(qrs)); #endregion logger.InfoFormat("执行脚本 开始脚本【{0}】,任务ID【{1}】", task.SCRIPT_ID, task.ID); Dictionary <string, object> dicStart = new Dictionary <string, object>(); dicStart.Add("RUN_STATE", "运行"); dicStart.Add("START_TIME", DateTime.Now); dicStart.Add("RETURN_CODE", null); dicStart.Add("END_TIME", null); dicStart.Add("DISABLE_DATE", null); dicStart.Add("DISABLE_REASON", null); FunSqlToClass.UpData <SCRIPT_TASK>( ConfigurationManager.AppSettings["dbType"], ConfigurationManager.AppSettings["dbConnSt"], dicStart, string.Format("where ID={0} ", task.ID), ConfigurationManager.AppSettings["dbPrefix"]); FunSqlToClass.NonQuery("DELETE FROM YL_SCRIPT_TASK_LOG where SCRIPT_TASK_ID=" + task.ID, ConfigurationManager.AppSettings["dbType"], ConfigurationManager.AppSettings["dbConnSt"], ConfigurationManager.AppSettings["dbPrefix"]); ProServer.Service db = new Service(); ProInterface.ErrorInfo error = new ProInterface.ErrorInfo(); object obj = db.ScriptTaskStart(ref error, Convert.ToInt32(task.ID)); if (error.IsError) { Dictionary <string, object> dic = new Dictionary <string, object>(); dic.Add("RUN_STATE", "停止"); dic.Add("RETURN_CODE", "失败"); dic.Add("END_TIME", DateTime.Now); dic.Add("DISABLE_DATE", DateTime.Now); if (error.Message != null) { if (error.Message.Length > 25) { dic.Add("DISABLE_REASON", error.Message.Substring(0, 25)); } else { dic.Add("DISABLE_REASON", error.Message); } } FunSqlToClass.UpData <SCRIPT_TASK>( ConfigurationManager.AppSettings["dbType"], ConfigurationManager.AppSettings["dbConnSt"], dic, string.Format("where ID={0} ", task.ID), ConfigurationManager.AppSettings["dbPrefix"]); logger.InfoFormat("执行脚本 脚本【{0}】,任务ID【{1}】 出错:{2}", task.SCRIPT_ID, task.ID, error.Message); } else { logger.InfoFormat("执行脚本 结束脚本【{0}】,任务ID【{1}】", task.SCRIPT_ID, task.ID); } } catch (Exception e) { logger.InfoFormat("分析 结束脚本【{0}】,任务ID【{1}】,出错:{2}", task.SCRIPT_ID, task.ID, e.ToString()); } var qrsEnd = ProInterface.JSON.EncodeToEntity <IList <QuartzRunStatus> >(System.IO.File.ReadAllText(statusLogPath)); var nowQrsEnd = qrsEnd.SingleOrDefault(x => x.JobName == "ScriptID_" + task.SCRIPT_ID); if (nowQrsEnd != null) { qrsEnd.Remove(nowQrsEnd); Fun.WriteAllText(statusLogPath, ProInterface.JSON.DecodeToStr(qrsEnd)); } } #endregion }
public void Execute(IJobExecutionContext context) { #region 检测运算实例是否存在,并把结束了的线程终止 QuartzRunStatus nowQrs = new QuartzRunStatus(); IList <QuartzRunStatus> qrs = new List <QuartzRunStatus>(); try { qrs = ProInterface.JSON.EncodeToEntity <IList <QuartzRunStatus> >(System.IO.File.ReadAllText(statusLogPath)); } catch { } foreach (var t in qrs.Where(x => x.StatusTime.AddHours(2) < DateTime.Now).ToList()) { qrs.Remove(t); } if (qrs == null) { qrs = new List <QuartzRunStatus>(); } nowQrs = qrs.SingleOrDefault(x => x.JobName == context.JobDetail.Key.Name); if (nowQrs == null) { nowQrs = new QuartzRunStatus(); } if (nowQrs.IsRun) { return; } nowQrs.IsRun = true; nowQrs.JobName = context.JobDetail.Key.Name; nowQrs.StatusTime = DateTime.Now; if (qrs.SingleOrDefault(x => x.JobName == context.JobDetail.Key.Name) == null) { qrs.Add(nowQrs); } Fun.WriteAllText(statusLogPath, ProInterface.JSON.DecodeToStr(qrs)); #endregion try { #region 添加发送的短信 //添加数据 string sql = @" INSERT INTO YL_SMS_SEND( KEY, MESSAGE_ID, PHONE_NO, ADD_TIME, CONTENT, STAUTS) SELECT SYS_GUID() KEY, A.MESSAGE_ID, A.PHONE_NO, sysdate ADD_TIME, B.CONTENT, '等待' STAUTS FROM YL_USER_MESSAGE A,YL_MESSAGE B WHERE A.MESSAGE_ID=B.ID AND A.STATUS='等待' AND PHONE_NO IS NOT NULL AND ((CEIL(((SYSDATE -CAST(A.STATUS_TIME AS DATE) )) * 24 * 60)>30 AND B.PUSH_TYPE='智能推送') OR B.PUSH_TYPE='短信推送') "; //更新状态 string sqlUpdate = "UPDATE YL_USER_MESSAGE SET STATUS='已推送',PUSH_TYPE='短信推送' WHERE STATUS='等待' AND PHONE_NO IS NOT NULL AND ((CEIL(((SYSDATE -CAST(STATUS_TIME AS DATE) )) * 24 * 60)>30 AND PUSH_TYPE='智能推送') OR PUSH_TYPE='短信推送')"; FunSqlToClass.NonQuery(sql, ConfigurationManager.AppSettings["dbType"], ConfigurationManager.AppSettings["dbConnSt"], ConfigurationManager.AppSettings["dbPrefix"]); FunSqlToClass.NonQuery(sqlUpdate, ConfigurationManager.AppSettings["dbType"], ConfigurationManager.AppSettings["dbConnSt"], ConfigurationManager.AppSettings["dbPrefix"]); #endregion IList <ProInterface.Models.SMS_SEND> reList = new List <ProInterface.Models.SMS_SEND>(); reList = FunSqlToClass.SqlToList <ProInterface.Models.SMS_SEND>("SELECT * FROM YL_SMS_SEND WHERE STAUTS='等待' OR (STAUTS='失败' AND TRY_NUM<5)", ConfigurationManager.AppSettings["dbType"], ConfigurationManager.AppSettings["dbConnSt"]); if (reList != null && reList.Count() > 0) { reList = reList.OrderBy(x => x.ADD_TIME).ToList(); ProInterface.ErrorInfo error = new ErrorInfo(); int succNum = 0; FunSqlToClass.NonQuery("UPDATE YL_SMS_SEND SET TRY_NUM=TRY_NUM+1 WHERE STAUTS='等待' OR (STAUTS='失败' AND TRY_NUM<5)", ConfigurationManager.AppSettings["dbType"], ConfigurationManager.AppSettings["dbConnSt"], ConfigurationManager.AppSettings["dbPrefix"]); foreach (var t in reList.GroupBy(x => x.CONTENT).ToList()) { var conten = t.Key; var phoneList = t.Select(x => x.PHONE_NO).ToArray(); Dictionary <string, object> dic = new Dictionary <string, object>(); dic.Add("SEND_TIME", DateTime.Now); dic.Add("STAUTS", "成功"); var allPage = t.Count() / 1000; if (t.Count() % 1000 != 0) { allPage++; } for (var i = 0; i < allPage; i++) { FunSqlToClass.UpData <SMS_SEND>( ConfigurationManager.AppSettings["dbType"], ConfigurationManager.AppSettings["dbConnSt"], dic, string.Format(" where KEY IN ('{0}') ", string.Join("','", t.Skip(i * 1000).Take(1000).Select(x => x.KEY))), ConfigurationManager.AppSettings["dbPrefix"]); } foreach (var phone in phoneList) { DbHelper.MasHelper.Send(phone, conten); } succNum += phoneList.Count(); } //sms.SmsConnection("", ref error); //foreach (var t in reList) //{ // Dictionary<string, object> dic = new Dictionary<string, object>(); // dic.Add("SEND_TIME", DateTime.Now); // string[] listPhoneNo = new string[] { t.PHONE_NO }; // if (sms.SmsSend("", ref error, t.PHONE_NO, t.CONTENT)) // //if (SMSHandle.sendSms(listPhoneNo, t.CONTENT) == "0") // { // succNum++; // dic.Add("STAUTS", "成功"); // } // else // { // dic.Add("STAUTS", "失败"); // } // FunSqlToClass.UpData<SMS_SEND>( // ConfigurationManager.AppSettings["dbType"], // ConfigurationManager.AppSettings["dbConnSt"], // dic, // string.Format(" where KEY='{0}' ", t.KEY), // ConfigurationManager.AppSettings["dbPrefix"]); //} //sms.SmsDisconnection("", ref error); //if (error.IsError) //{ // logger.Info(string.Format("断开短信猫失败【{0}】", error.Message)); //} logger.Info(string.Format("成功发送【{0}】条短信", succNum)); } qrs.Remove(nowQrs); Fun.WriteAllText(statusLogPath, ProInterface.JSON.DecodeToStr(qrs)); } catch (Exception ex) { qrs.Remove(nowQrs); Fun.WriteAllText(statusLogPath, ProInterface.JSON.DecodeToStr(qrs)); logger.Error("发送短信 运行异常", ex); } }