Ejemplo n.º 1
0
        /// <summary>
        /// 运行出错日志
        /// </summary>
        /// <param name="err"></param>
        public void ErrorScriptTask(Exception err)
        {
            var message = err.Message;

            if (message.Length > 40)
            {
                message = message.Substring(0, 40);
            }
            _Error = err.Message;

            log(err.Message, err.ToString(), 1);

            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);
            dic.Add("DISABLE_REASON", message);
            FunSqlToClass.UpData <SCRIPT_TASK>(
                ConfigurationManager.AppSettings["dbType"],
                ConfigurationManager.AppSettings["dbConnSt"],
                dic,
                string.Format("where ID={0} ", _taskId),
                ConfigurationManager.AppSettings["dbPrefix"]);
            _isRun = false;
        }
Ejemplo n.º 2
0
        public void Execute(IJobExecutionContext context)
        {
            var scriptId = Convert.ToInt32(context.JobDetail.Key.Name.Replace("ScriptJob_", ""));
            var taskEnt  = FunSqlToClass.ClassSingle <SCRIPT_TASK>(
                ConfigurationManager.AppSettings["dbType"],
                ConfigurationManager.AppSettings["dbConnSt"],
                "where RUN_STATE='运行' AND SCRIPT_ID = " + scriptId,
                ConfigurationManager.AppSettings["dbPrefix"]
                );

            //如果有任务没有结束,则不添加任务
            if (taskEnt != null && taskEnt.ID != 0)
            {
                if (taskEnt.START_TIME != null && taskEnt.START_TIME.Value.AddHours(2) < DateTime.Now)
                {
                    Dictionary <string, object> dicStart = new Dictionary <string, object>();
                    dicStart.Add("RUN_STATE", "失败");
                    dicStart.Add("DISABLE_REASON", "运行超时");
                    FunSqlToClass.UpData <SCRIPT_TASK>(
                        ConfigurationManager.AppSettings["dbType"],
                        ConfigurationManager.AppSettings["dbConnSt"],
                        dicStart,
                        string.Format("where ID={0} ", scriptId),
                        ConfigurationManager.AppSettings["dbPrefix"]);
                }
                else
                {
                    return;
                }
            }


            var scriptEnt = FunSqlToClass.ClassSingle <SCRIPT>(
                ConfigurationManager.AppSettings["dbType"],
                ConfigurationManager.AppSettings["dbConnSt"],
                "where ID = " + scriptId,
                ConfigurationManager.AppSettings["dbPrefix"]
                );


            ScriptExt ext = new ScriptExt();

            if (scriptEnt.IS_GROUP == (short)1)//表示是任务组
            {
                IList <ProInterface.Models.SCRIPT_GROUP_LIST> reList = new List <ProInterface.Models.SCRIPT_GROUP_LIST>();
                reList = FunSqlToClass.SqlToList <ProInterface.Models.SCRIPT_GROUP_LIST>(string.Format("SELECT * FROM YL_SCRIPT_GROUP_LIST WHERE GROUP_ID={0} ORDER BY ORDER_INDEX", scriptId), ConfigurationManager.AppSettings["dbType"], ConfigurationManager.AppSettings["dbConnSt"]);
                if (reList.Count() > 0)
                {
                    long taskId = 0;
                    ext.AddScriptTask(reList[0].SCRIPT_ID, scriptId, ref taskId);
                    logger.InfoFormat("执行脚本 添加脚本【{0}】任务ID【{1}】", scriptId, taskId);
                }
            }
            else//普通脚本
            {
                long taskId = 0;
                ext.AddScriptTask(scriptId, null, ref taskId);
                logger.InfoFormat("执行脚本 添加脚本【{0}】任务ID【{1}】", scriptId, taskId);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 取消任务
        /// </summary>
        public void ScriptTaskCancel()
        {
            log(string.Format("取消任务【{0}】", _taskId));
            Dictionary <string, object> dic = new Dictionary <string, object>();

            dic.Add("RUN_STATE", "停止");
            dic.Add("RETURN_CODE", "取消");
            FunSqlToClass.UpData <SCRIPT_TASK>(
                ConfigurationManager.AppSettings["dbType"],
                ConfigurationManager.AppSettings["dbConnSt"],
                dic,
                string.Format("where ID={0} ", _taskId),
                ConfigurationManager.AppSettings["dbPrefix"]);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 结束任务
        /// </summary>
        public void CompleteScriptTask()
        {
            Dictionary <string, object> dic = new Dictionary <string, object>();

            dic.Add("RUN_STATE", "停止");
            dic.Add("END_TIME", DateTime.Now);
            if (IsRun())
            {
                dic.Add("RETURN_CODE", "成功");
            }
            else
            {
                dic.Add("RETURN_CODE", "失败");
            }
            FunSqlToClass.UpData <SCRIPT_TASK>(
                ConfigurationManager.AppSettings["dbType"],
                ConfigurationManager.AppSettings["dbConnSt"],
                dic,
                string.Format("where ID={0} ", _taskId),
                ConfigurationManager.AppSettings["dbPrefix"]);
        }
Ejemplo n.º 5
0
        public void ErrorScriptTask(string message, int taskId)
        {
            if (message.Length > 40)
            {
                message = message.Substring(0, 40);
            }
            log(message, taskId);
            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);
            dic.Add("DISABLE_REASON", message);
            FunSqlToClass.UpData <SCRIPT_TASK>(
                ConfigurationManager.AppSettings["dbType"],
                ConfigurationManager.AppSettings["dbConnSt"],
                dic,
                string.Format("where ID={0} ", taskId),
                ConfigurationManager.AppSettings["dbPrefix"]);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// 运行脚本任务
        /// </summary>
        /// <param name="scriptId"></param>
        public void ScriptTaskRun(Int64 scriptTaskId)
        {
            var entTask = FunSqlToClass.ClassSingle <SCRIPT_TASK>(
                ConfigurationManager.AppSettings["dbType"],
                ConfigurationManager.AppSettings["dbConnSt"],
                "where ID = " + scriptTaskId,
                ConfigurationManager.AppSettings["dbPrefix"]
                );

            Dictionary <string, object> dic = new Dictionary <string, object>();

            dic.Add("RUN_STATE", "运行");
            dic.Add("START_TIME", DateTime.Now);
            FunSqlToClass.UpData <SCRIPT_TASK>(
                ConfigurationManager.AppSettings["dbType"],
                ConfigurationManager.AppSettings["dbConnSt"],
                dic,
                string.Format("where ID={0} ", _taskId),
                ConfigurationManager.AppSettings["dbPrefix"]);

            _taskId   = entTask.ID;
            _scriptId = entTask.SCRIPT_ID;
            runData   = new Service().AnalysisRunDate(entTask.RUN_DATA);
        }
Ejemplo n.º 7
0
        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
        }
Ejemplo n.º 8
0
        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);
            }
        }