Ejemplo n.º 1
0
        /// <summary>
        /// 启动任务
        /// </summary>
        /// <param name="processId">任务进程主键</param>
        public void EnAbleJob(string processId)
        {
            try
            {
                TSProcessEntity tSProcessEntity = tSProcessIBLL.GetProcessEntity(processId);
                TSSchemeEntity  tSSchemeEntity  = GetSchemeEntityByInfo(tSProcessEntity.F_SchemeInfoId);

                TSSchemeModel tSSchemeModel = tSSchemeEntity.F_Scheme.ToObject <TSSchemeModel>();
                if (tSProcessEntity.F_SchemeId != tSSchemeEntity.F_Id)
                {
                    tSProcessEntity.F_State = 10;
                    tSProcessIBLL.SaveEntity(tSProcessEntity.F_Id, tSProcessEntity);

                    // 如果模板更改需要重新创建一个任务进程
                    tSProcessEntity = new TSProcessEntity()
                    {
                        F_SchemeId     = tSSchemeEntity.F_Id,
                        F_SchemeInfoId = tSProcessEntity.F_SchemeInfoId,
                        F_State        = 2,
                        F_EndType      = tSSchemeModel.endType,
                        F_EndTime      = tSSchemeModel.endTime
                    };
                    if (tSSchemeModel.startType == 1)
                    {
                        tSProcessEntity.F_BeginTime = DateTime.Now;
                    }
                    else
                    {
                        tSProcessEntity.F_BeginTime = tSSchemeModel.startTime;
                    }
                    if (tSSchemeModel.endType == 1)
                    {
                        tSProcessEntity.F_EndTime = DateTime.MaxValue;
                    }

                    tSProcessIBLL.SaveEntity("", tSProcessEntity);
                }
                else
                {
                    tSProcessEntity.F_State = 2;
                    tSProcessIBLL.SaveEntity(tSProcessEntity.F_Id, tSProcessEntity);
                }
                QuartzHelper.AddJob(tSProcessEntity.F_SchemeInfoId, tSProcessEntity.F_Id, tSSchemeModel);
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowBusinessException(ex);
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 任务执行方法
        /// </summary>
        /// <param name="context"></param>
        public void Execute(IJobExecutionContext context)
        {
            try
            {
                JobDataMap dataMap  = context.JobDetail.JobDataMap;
                string     keyValue = dataMap.GetString("keyValue");

                TSProcessEntity tSProcessEntity = null;
                TSSchemeEntity  tSSchemeEntity  = null;

                if (!_Execute(keyValue))   // 如果异常,需要重新执行一次
                {
                    tSProcessEntity = tSProcessIBLL.GetProcessEntity(keyValue);
                    if (tSProcessEntity != null)
                    {
                        tSSchemeEntity = tSSchemeIBLL.GetSchemeEntity(tSProcessEntity.F_SchemeId);
                        if (tSSchemeEntity != null)
                        {
                            TSSchemeModel tSSchemeModel = tSSchemeEntity.F_Scheme.ToObject <TSSchemeModel>();
                            if (tSSchemeModel.isRestart == 1)
                            {
                                for (int i = 0; i < tSSchemeModel.restartNum; i++)
                                {
                                    Thread.Sleep(60 * 1000 * tSSchemeModel.restartMinute);  // 停顿1000毫秒
                                    if (_Execute(keyValue))
                                    {
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception)
            {
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 任务执行方法
        /// </summary>
        /// <param name="keyValue">任务进程主键</param>
        /// <returns></returns>
        private bool _Execute(string keyValue)
        {
            bool   isOk = true;
            string msg  = "执行成功";

            TSProcessEntity tSProcessEntity = null;
            TSSchemeEntity  tSSchemeEntity  = null;

            // 获取一个任务进程
            try
            {
                tSProcessEntity = tSProcessIBLL.GetProcessEntity(keyValue);
            }
            catch (Exception ex)
            {
                isOk = false;
                msg  = "获取任务进程异常:" + ex.Message;
            }

            if (tSProcessEntity != null && tSProcessEntity.F_State != 1 && tSProcessEntity.F_State != 2)
            {
                return(true);
            }

            // 获取对应的任务模板
            if (isOk)
            {
                try
                {
                    tSSchemeEntity = tSSchemeIBLL.GetSchemeEntity(tSProcessEntity.F_SchemeId);
                }
                catch (Exception ex)
                {
                    isOk = false;
                    msg  = "获取任务模板异常:" + ex.Message;
                }
            }

            bool flag = false;

            // 执行任务
            if (isOk)
            {
                try
                {
                    TSSchemeModel tSSchemeModel = tSSchemeEntity.F_Scheme.ToObject <TSSchemeModel>();
                    switch (tSSchemeModel.methodType)
                    {
                    case 1:    // sql
                        databaseLinkIBLL.ExecuteBySql(tSSchemeModel.dbId, tSSchemeModel.strSql);
                        break;

                    case 2:    // 存储过程
                        databaseLinkIBLL.ExecuteByProc(tSSchemeModel.dbId, tSSchemeModel.procName);
                        break;

                    case 3:    // 接口
                        if (tSSchemeModel.urlType == "1")
                        {
                            HttpMethods.Get(tSSchemeModel.url);
                        }
                        else
                        {
                            HttpMethods.Post(tSSchemeModel.url);
                        }
                        break;

                    case 4:    // 依赖注入
                        if (!string.IsNullOrEmpty(tSSchemeModel.iocName) && UnityIocHelper.TsInstance.IsResolve <ITsMethod>(tSSchemeModel.iocName))
                        {
                            ITsMethod iTsMethod = UnityIocHelper.TsInstance.GetService <ITsMethod>(tSSchemeModel.iocName);
                            iTsMethod.Execute();
                        }
                        break;
                    }

                    if (tSSchemeModel.executeType == 1)
                    {
                        flag = true;
                    }
                }
                catch (Exception ex)
                {
                    isOk = false;
                    msg  = "执行方法出错:" + ex.Message;
                }
            }

            try
            {
                // 新增一条任务日志
                TSLogEntity logEntity = new TSLogEntity()
                {
                    F_ExecuteResult = isOk ? 1 : 2,
                    F_Des           = msg,
                    F_ProcessId     = keyValue
                };
                logEntity.Create();
                tSLogIBLL.SaveEntity("", logEntity);

                if (tSProcessEntity.F_State == 1)
                {
                    tSProcessEntity.F_State = 2;
                    if (flag)
                    {
                        tSProcessEntity.F_State = 4;
                    }

                    tSProcessIBLL.SaveEntity(tSProcessEntity.F_Id, tSProcessEntity);
                }
            }
            catch (Exception)
            {
            }


            return(isOk);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 添加任务
        /// </summary>
        /// <param name="schemeInfoId">模板信息主键</param>
        /// <param name="processId">任务进程主键</param>
        /// <param name="scheme">任务模板</param>
        public static void AddJob(string schemeInfoId, string processId, TSSchemeModel scheme)
        {
            string startTime = "";
            string endTime   = "";

            if (scheme.startType == 2 && scheme.startTime != null)
            {
                startTime = ((DateTime)scheme.startTime).ToString("yyyy-MM-dd hh:mm:ss");
            }
            if (scheme.endType == 2 && scheme.endTime != null)
            {
                endTime = ((DateTime)scheme.endTime).ToString("yyyy-MM-dd hh:mm:ss");
            }


            switch (scheme.executeType)
            {
            case 1:                                                           // 只执行一次
                AddRepeatOneJob(schemeInfoId, startTime, endTime, processId); //加入只执行一次的任务
                break;

            case 2:    // 简单重复执行
                AddRepeatJob(schemeInfoId, startTime, endTime, processId, scheme.simpleValue, scheme.simpleType);
                break;

            case 3:    // 明细频率执行
                List <string> cornlist = new List <string>();
                foreach (var fre in scheme.frequencyList)
                {
                    string cron = "0 ";
                    cron += fre.minute + " " + fre.hour + " ";
                    switch (fre.type)
                    {
                    case "day":
                        cron += "* ";
                        break;

                    case "week":
                        cron += "? ";
                        break;

                    case "month":
                        cron += fre.carryDate + " ";
                        break;
                    }
                    cron += fre.carryMounth + " ";
                    if (fre.type == "week")
                    {
                        cron += fre.carryDate + " ";
                    }
                    else
                    {
                        cron += "? ";
                    }
                    cron += "*";
                    cornlist.Add(cron);
                }
                AddListCronJob(schemeInfoId, startTime, endTime, processId, cornlist);
                break;

            case 4:    // corn表达式
                AddCronJob(schemeInfoId, startTime, endTime, processId, scheme.cornValue);
                break;
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 保存实体数据(新增、修改)
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        public TSProcessEntity SaveEntity(string keyValue, TSSchemeInfoEntity schemeInfoEntity, TSSchemeEntity schemeEntity)
        {
            TSSchemeModel   tSSchemeModel   = schemeEntity.F_Scheme.ToObject <TSSchemeModel>();
            TSProcessEntity tSProcessEntity = null;


            TSSchemeEntity schemeEntity2 = null;

            if (!string.IsNullOrEmpty(keyValue))
            {
                schemeEntity2 = this.BaseRepository().FindEntity <TSSchemeEntity>(t => t.F_IsActive == 1 && t.F_SchemeInfoId == keyValue);
            }
            var db = this.BaseRepository().BeginTrans();

            try
            {
                if (!string.IsNullOrEmpty(keyValue))
                {
                    schemeInfoEntity.Modify(keyValue);
                    db.Update(schemeInfoEntity);

                    if (schemeEntity2 == null || schemeEntity2.F_Scheme != schemeEntity.F_Scheme)
                    {
                        schemeEntity.Create();
                        schemeEntity.F_SchemeInfoId = schemeInfoEntity.F_Id;
                        schemeEntity.F_IsActive     = 1;
                        db.Insert(schemeEntity);

                        if (schemeEntity2 != null)
                        {
                            schemeEntity2.F_IsActive = 2;
                            db.Update(schemeEntity2);
                        }

                        // 关闭老的任务进程
                        TSProcessEntity tSProcessOldEntity = this.BaseRepository().FindEntity <TSProcessEntity>(t => t.F_SchemeInfoId == keyValue && t.F_State != 10);
                        if (tSProcessOldEntity.F_State != 3)
                        {
                            if (tSProcessOldEntity.F_State == 1 || tSProcessOldEntity.F_State == 2)
                            {
                                tSProcessOldEntity.F_State = 10;
                                db.Update(tSProcessOldEntity);
                            }

                            // 新增一个任务进程
                            tSProcessEntity = new TSProcessEntity()
                            {
                                F_State        = 1,
                                F_SchemeId     = schemeEntity.F_Id,
                                F_SchemeInfoId = schemeInfoEntity.F_Id,
                                F_EndType      = tSSchemeModel.endType,
                                F_EndTime      = tSSchemeModel.endTime
                            };
                            tSProcessEntity.Create();

                            if (tSSchemeModel.startType == 1)
                            {
                                tSProcessEntity.F_BeginTime = DateTime.Now;
                            }
                            else
                            {
                                tSProcessEntity.F_BeginTime = tSSchemeModel.startTime;
                            }

                            if (tSSchemeModel.endType == 1)
                            {
                                tSProcessEntity.F_EndTime = DateTime.MaxValue;
                            }

                            db.Insert(tSProcessEntity);
                        }
                    }
                }
                else
                {
                    schemeInfoEntity.Create();
                    db.Insert(schemeInfoEntity);

                    schemeEntity.Create();
                    schemeEntity.F_SchemeInfoId = schemeInfoEntity.F_Id;
                    schemeEntity.F_IsActive     = 1;
                    db.Insert(schemeEntity);

                    // 新增一个任务进程
                    tSProcessEntity = new TSProcessEntity()
                    {
                        F_State        = 1,
                        F_SchemeId     = schemeEntity.F_Id,
                        F_SchemeInfoId = schemeInfoEntity.F_Id,
                        F_EndType      = tSSchemeModel.endType,
                        F_EndTime      = tSSchemeModel.endTime
                    };
                    tSProcessEntity.Create();

                    if (tSSchemeModel.startType == 1)
                    {
                        tSProcessEntity.F_BeginTime = DateTime.Now;
                    }
                    else
                    {
                        tSProcessEntity.F_BeginTime = tSSchemeModel.startTime;
                    }

                    if (tSSchemeModel.endType == 1)
                    {
                        tSProcessEntity.F_EndTime = DateTime.MaxValue;
                    }

                    db.Insert(tSProcessEntity);
                }

                db.Commit();

                return(tSProcessEntity);
            }
            catch (Exception ex)
            {
                db.Rollback();

                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }