Ejemplo n.º 1
0
        public const string PROCPARARMS = "PROCPARARMS"; //过程参数

        public static void UpdateState(ref ValidationErrors errors, string jobName, int state, string result)
        {
            IJOB_TASKJOBSBLL taskJobsBLL = new JOB_TASKJOBSBLL()
            {
                m_Rep = new JOB_TASKJOBSRepository(new DBContainer())
            };

            if (!taskJobsBLL.UpdateState(ref errors, jobName, state, result))
            {
                Log.Write(jobName, "更新任务状态异常:" + errors.Error, "失败");
            }
        }
Ejemplo n.º 2
0
        public int ClearUnTrackTask(ref ValidationErrors errors)
        {
            try
            {
                IJOB_TASKJOBSBLL taskJobsBLL = new JOB_TASKJOBSBLL()
                {
                    m_Rep = new JOB_TASKJOBSRepository(new DBContainer())
                };
                /////////////////////////////////////////////////////////////
                // construct a scheduler factory
                ISchedulerFactory schedFact = new StdSchedulerFactory();

                // get a scheduler
                IScheduler sched = schedFact.GetScheduler();
                /////////////////////////////////////////////////////////////////////////

                List <P_JOB_GetUnTrackTask_Result> taskNameList = taskJobsBLL.GetUnTrackTask();

                int count = 0;
                foreach (var taskName in taskNameList)
                {
                    string jobName = taskName.JOB_NAME;
                    foreach (var group in sched.GetJobGroupNames())
                    {
                        var jobKeys = sched.GetJobKeys(GroupMatcher <JobKey> .GroupEquals(group));
                        foreach (var jobkey in jobKeys)
                        {
                            if (jobkey.Name.Contains(jobName))
                            {
                                sched.DeleteJob(jobkey);
                                count++;
                            }
                        }
                    }
                }
                return(count);
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                return(0);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 复杂任务
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="model"></param>
        /// <param name="errors"></param>
        /// <returns></returns>
        public int runComplex <T>(JobModel model, ref ValidationErrors errors) where T : IJob
        {
            try
            {
                //是否为简单任务
                if (model.taskType != 1)
                {
                    errors.Add("不是复杂任务");
                    return(0);
                }

                if (string.IsNullOrEmpty(model.cronExpression))
                {
                    errors.Add("任务表达式不能为空,请正确执行时间");
                    return(0);
                }

                //对表达式进行检查

                //执行前置任务
                string error = RunBefore(model);
                if (!string.IsNullOrEmpty(error))
                {
                    errors.Add(error);
                    return(0);
                }

                /////////////////////////////////////////////////////////////////
                // construct a scheduler factory
                ISchedulerFactory schedFact = new StdSchedulerFactory();

                // get a scheduler
                IScheduler sched = schedFact.GetScheduler();
                /////////////////////////////////////////////////////////////////////////

                //在开始时间的10秒后执行
                DateTimeOffset runTime = new DateTimeOffset(model.startDate).AddSeconds(10);

                string Description = model.taskName + "任务时间:" + runTime + ",创建时间:" + DateTime.Now;
                //生成作业名称,作业名称=任务名称 +任务ID+序号
                string jobName = model.taskName + "_" + model.id + "_" + NewGuid();
                //向Job_TaskJobs增加任务
                string sno = JOB_PRIFEX + jobName;

                string taskCmd;
                taskCmd = "秒:" + model.seconds + " 分:" + model.minutes + " 小时:" + model.hours +
                          " 日/月:" + model.dayOfMonth + " 月:" + model.month + " 日/周:" + model.dayOfWeek + " 指令表达式:" + model.cronExpression;

                JOB_TASKJOBSModel taskJobsModel = new JOB_TASKJOBSModel();
                taskJobsModel.sno       = sno;
                taskJobsModel.taskName  = model.taskName;
                taskJobsModel.Id        = model.id;
                taskJobsModel.taskTitle = model.taskTitle;
                taskJobsModel.taskCmd   = taskCmd;
                taskJobsModel.crtDt     = DateTime.Now;
                taskJobsModel.state     = 0;
                taskJobsModel.creator   = model.creator;
                IJOB_TASKJOBSBLL taskJobsBLL = new JOB_TASKJOBSBLL()
                {
                    m_Rep = new JOB_TASKJOBSRepository(new DBContainer())
                };
                if (!taskJobsBLL.Create(ref errors, taskJobsModel))
                {
                    return(0);
                }
                /////////////////////////////////////////////////
                IJobDetail job = JobBuilder.Create <T>()
                                 .WithIdentity(JOB_PRIFEX + jobName, "group1")
                                 .WithDescription(Description)
                                 .Build();
                //



                ICronTrigger trigger = (ICronTrigger)TriggerBuilder.Create()
                                       .WithIdentity(TRIGGER_PRIFEX + jobName, "group1")
                                       .WithCronSchedule(model.cronExpression)
                                       .WithDescription(Description)
                                       .Build();

                //如果已存在相同任务删除
                if (sched.CheckExists(job.Key))
                {
                    sched.DeleteJob(job.Key);
                }
                //传入任务关键字参数(任务ID标识、任务名称)
                job.JobDataMap.Put(TaskJob.ID, model.id);             //传入任务ID标识
                job.JobDataMap.Put(TaskJob.TASKNAME, model.taskName); //传入任务名称,带命名空间

                //传入任务其他参数列表
                if (model.param != null)
                {
                    for (int i = 0; i < model.param.GetLength(0); i++)
                    {
                        job.JobDataMap.Put(model.param[i, 0], model.param[i, 1]);
                    }
                }

                //////////////////////////////////////////////////////////
                DateTimeOffset dtoff = sched.ScheduleJob(job, trigger);
                return(1);
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                return(0);
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 简单任务
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="model"></param>
        /// <param name="errors"></param>
        /// <returns></returns>
        public int runSample <T>(JobModel model, ref ValidationErrors errors) where T : IJob
        {
            try
            {
                //是否为简单任务
                if (model.taskType != 0)
                {
                    errors.Add("不是简单任务");
                    return(0);
                }

                //间隔类型
                if (string.IsNullOrEmpty(model.intervalType))
                {
                    errors.Add("未设置间隔类型");
                    return(0);
                }
                //时间间隔
                if (model.intervalCount < 0)
                {
                    errors.Add("未设置间隔次数(0为无限次)");
                    return(0);
                }
                //如果开始时间少于当前时间,取当前时间

                if (DateTime.Now > model.startDate)
                {
                    model.startDate = DateTime.Now;
                }

                //执行前置任务
                string error = RunBefore(model);
                if (!string.IsNullOrEmpty(error))
                {
                    errors.Add(error);
                    return(0);
                }
                /////////////////////////////////////////////////////////////
                // construct a scheduler factory
                ISchedulerFactory schedFact = new StdSchedulerFactory();

                // get a scheduler
                IScheduler sched = schedFact.GetScheduler();
                /////////////////////////////////////////////////////////////////////////

                //在开始时间的10秒后执行
                DateTimeOffset runTime = new DateTimeOffset(model.startDate).AddSeconds(10);

                string Description = model.taskName + "任务时间:" + runTime + ",创建时间:" + DateTime.Now;
                //生成作业名称,作业名称=任务名称 +任务ID
                string jobName = model.taskName + "_" + model.id + "_" + NewGuid();

                //向Job_TaskJobs增加任务

                string sno           = JOB_PRIFEX + jobName;
                string intervalTitle = model.intervalType == "second" ? "秒" : model.intervalType == "minute" ? "分" : model.intervalType == "hour" ? "小时" : "天";

                string teskCmd;
                if (model.repeatForever)
                {
                    teskCmd = "start:" + model.startDate + " 重复:无限次 间隔:" + model.intervalCount + " " + intervalTitle + " ";
                }
                else
                {
                    teskCmd = "start:" + model.startDate + " 重复:" + model.repeatCount + " 间隔:" + model.intervalCount + " " + intervalTitle + " ";
                }
                JOB_TASKJOBSModel taskJobsModel = new JOB_TASKJOBSModel();
                taskJobsModel.sno        = sno;
                taskJobsModel.taskName   = model.taskName;
                taskJobsModel.Id         = model.id;
                taskJobsModel.taskTitle  = model.taskTitle;
                taskJobsModel.taskCmd    = teskCmd;
                taskJobsModel.crtDt      = DateTime.Now;
                taskJobsModel.state      = 0;
                taskJobsModel.creator    = model.creator;
                taskJobsModel.procName   = model.procName;
                taskJobsModel.procParams = model.procParams;

                IJOB_TASKJOBSBLL taskJobsBLL = new JOB_TASKJOBSBLL()
                {
                    m_Rep = new JOB_TASKJOBSRepository(new DBContainer())
                };
                if (!taskJobsBLL.Create(ref errors, taskJobsModel))
                {
                    return(0);
                }
                /////////////////////////////////////////////////

                IJobDetail job = JobBuilder.Create <T>()
                                 .WithIdentity(JOB_PRIFEX + jobName, "group1")
                                 .WithDescription(Description)
                                 .Build();
                //

                ITrigger trigger = null;
                //执行重复次数
                if (model.repeatForever)
                {
                    //无限次
                    switch (model.intervalType.ToLower())
                    {
                    case "day":    //日
                        TimeSpan ts = new TimeSpan(model.intervalCount, 0, 0, 0);

                        trigger = TriggerBuilder.Create()
                                  .WithIdentity(TRIGGER_PRIFEX + jobName, "group1")
                                  .StartAt(model.startDate)
                                  .WithSimpleSchedule(a => a.WithInterval(ts).RepeatForever())
                                  .WithDescription(Description)
                                  .Build();

                        break;

                    case "hour":    //小时
                        trigger = TriggerBuilder.Create()
                                  .WithIdentity(TRIGGER_PRIFEX + jobName, "group1")
                                  .StartAt(model.startDate)
                                  .WithSimpleSchedule(a => a.WithIntervalInHours(model.intervalCount).RepeatForever())
                                  .WithDescription(Description)
                                  .Build();
                        break;

                    case "minute":    //分
                        trigger = TriggerBuilder.Create()
                                  .WithIdentity(TRIGGER_PRIFEX + jobName, "group1")
                                  .StartAt(model.startDate)
                                  .WithSimpleSchedule(a => a.WithIntervalInMinutes(model.intervalCount).RepeatForever())
                                  .WithDescription(Description)
                                  .Build();
                        break;

                    case "second":    //秒
                        trigger = TriggerBuilder.Create()
                                  .WithIdentity(TRIGGER_PRIFEX + jobName, "group1")
                                  .StartAt(model.startDate)
                                  .WithSimpleSchedule(a => a.WithIntervalInSeconds(model.intervalCount).RepeatForever())
                                  .WithDescription(Description)
                                  .Build();
                        break;

                    default:
                        trigger = TriggerBuilder.Create()
                                  .WithIdentity(TRIGGER_PRIFEX + jobName, "group1")
                                  .StartAt(model.startDate)
                                  .WithDescription(Description)
                                  .Build();
                        break;
                    }
                }
                else
                {
                    //有限次
                    switch (model.intervalType.ToLower())
                    {
                    case "day":    //日
                        TimeSpan ts = new TimeSpan(model.intervalCount, 0, 0, 0);

                        trigger = TriggerBuilder.Create()
                                  .WithIdentity(TRIGGER_PRIFEX + jobName, "group1")
                                  .StartAt(model.startDate)
                                  .WithSimpleSchedule(a => a.WithInterval(ts).WithRepeatCount(model.repeatCount))
                                  .WithDescription(Description)
                                  .Build();

                        break;

                    case "hour":    //小时
                        trigger = TriggerBuilder.Create()
                                  .WithIdentity(TRIGGER_PRIFEX + jobName, "group1")
                                  .StartAt(model.startDate)
                                  .WithSimpleSchedule(a => a.WithIntervalInHours(model.intervalCount).WithRepeatCount(model.repeatCount))
                                  .WithDescription(Description)
                                  .Build();
                        break;

                    case "minute":    //分
                        trigger = TriggerBuilder.Create()
                                  .WithIdentity(TRIGGER_PRIFEX + jobName, "group1")
                                  .StartAt(model.startDate)
                                  .WithSimpleSchedule(a => a.WithIntervalInMinutes(model.intervalCount).WithRepeatCount(model.repeatCount))
                                  .WithDescription(Description)
                                  .Build();
                        break;

                    case "second":    //秒
                        trigger = TriggerBuilder.Create()
                                  .WithIdentity(TRIGGER_PRIFEX + jobName, "group1")
                                  .StartAt(model.startDate)
                                  .WithSimpleSchedule(a => a.WithIntervalInSeconds(model.intervalCount).WithRepeatCount(model.repeatCount))
                                  .WithDescription(Description)
                                  .Build();
                        break;

                    default:
                        trigger = TriggerBuilder.Create()
                                  .WithIdentity(TRIGGER_PRIFEX + jobName, "group1")
                                  .StartAt(model.startDate)
                                  .WithDescription(Description)
                                  .Build();
                        break;
                    }
                }

                //如果已存在相同任务删除
                if (sched.CheckExists(job.Key))
                {
                    sched.DeleteJob(job.Key);
                }
                //传入任务关键字参数(任务ID标识、任务名称)
                job.JobDataMap.Put(TaskJob.ID, model.id);             //传入任务ID标识
                job.JobDataMap.Put(TaskJob.TASKNAME, model.taskName); //传入任务名称,带命名空间

                //传入任务其他参数列表
                if (model.param != null)
                {
                    for (int i = 0; i < model.param.GetLength(0); i++)
                    {
                        job.JobDataMap.Put(model.param[i, 0], model.param[i, 1]);
                    }
                }
                //////////////////////////////////////////////////////////
                DateTimeOffset dtoff = sched.ScheduleJob(job, trigger);


                return(1);
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                return(0);
            }
        }