private IJobDetail CreateJobDetail <TJob>() where TJob : IJob { JobBuilder jobBuilder = JobBuilder.Create(); jobBuilder.OfType <TJob>(); return(jobBuilder.Build()); }
/// <summary> /// 添加远程任务 /// </summary> /// <param name="taskid">任务id</param> /// <param name="message">返回信息</param> /// <returns>是否成功</returns> public static bool AddRemoteTask(string taskid,out string message) { bool result = false; try { TaskBLL td = new TaskBLL(); var t = td.Detail(taskid); TaskRuntimeInfo trun = new TaskRuntimeInfo(); trun.parms = td.GetParms(taskid); switch (t.taskType) { case "SQL-FILE": trun.sqlFileTask = new SqlFileTask(t.taskFile, t.taskDBCon); break; case "SQL-EXP": trun.sqlExpTask = new SqlExpJob(t.taskDBCon, t.taskFile, taskid, t.taskParm, trun.parms); break; case "DLL-STD": trun.dllTask = new QMAppDomainLoader<DllTask>().Load(t.taskFile, t.taskClsType, out trun.domain); break; case "DLL-UNSTD": default: trun.unStdDllTask = new UnStdDll(t.taskFile, t.taskParm); break; } trun.task = t; if (!_taskPool.ContainsKey(taskid)) { //添加任务 JobBuilder jobBuilder = JobBuilder.Create() .WithIdentity(trun.task.idx); //.WithIdentity(taskinfo.task.idx, taskinfo.task.taskCategory); switch (trun.task.taskType) { case "SQL-FILE": jobBuilder = jobBuilder.OfType(typeof(QMSqlFileTaskJob)); break; case "SQL-EXP": case "SQL": jobBuilder = jobBuilder.OfType(typeof(QMSqlExpTaskJob)); break; case "DLL-STD": jobBuilder = jobBuilder.OfType(typeof(QMDllTaskJob)); break; case "DLL-UNSTD": default: jobBuilder = jobBuilder.OfType(typeof(QMUnStdDllTaskJob)); break; } IJobDetail jobDetail = jobBuilder.Build(); //任务Misfire处理规则 //以当前时间为触发频率立刻触发一次执行 //然后按照Cron频率依次执行 ITrigger trigger = TriggerBuilder.Create() .WithIdentity(trun.task.taskName, "Cron") .WithCronSchedule(trun.task.taskCron, x => x.WithMisfireHandlingInstructionFireAndProceed()) .ForJob(jobDetail.Key) .Build(); if (_remoteScheduler.CheckExists(jobDetail.Key).Result) { _remoteScheduler.DeleteJob(jobDetail.Key); } _remoteScheduler.ScheduleJob(jobDetail, trigger); _taskPool.Add(taskid, trun); } message = string.Format("添加远程任务成功:{0}", taskid); result = true; } catch (QMException ex) { message = string.Format("添加远程任务失败:{0},{1}", ex.Message, ex.StackTrace); log.Fatal(message); } return result; }
/// <summary> /// 添加任务到队列中 /// </summary> /// <param name="taskid"></param> /// <param name="taskinfo"></param> /// <param name="misfire"></param> /// <returns></returns> public static async Task<bool> AddTask(string taskid, TaskRuntimeInfo taskinfo, QMMisFire misfire = QMMisFire.DoNothing) { if (!_taskPool.ContainsKey(taskid)) { //添加任务 JobBuilder jobBuilder = JobBuilder.Create() .WithIdentity(taskinfo.task.idx); //.WithIdentity(taskinfo.task.idx, taskinfo.task.taskCategory); switch (taskinfo.task.taskType) { case "SQL-FILE": jobBuilder = jobBuilder.OfType(typeof(QMSqlFileTaskJob)); break; case "SQL-EXP": case "SQL": jobBuilder = jobBuilder.OfType(typeof(QMSqlExpTaskJob)); break; case "DLL-STD": jobBuilder = jobBuilder.OfType(typeof(QMDllTaskJob)); break; case "DLL-UNSTD": default: jobBuilder = jobBuilder.OfType(typeof(QMUnStdDllTaskJob)); break; } IJobDetail jobDetail = jobBuilder.Build(); //任务Misfire处理规则 ITrigger trigger; if (misfire == QMMisFire.FireAndProceed) { //以当前时间为触发频率立刻触发一次执行 //然后按照Cron频率依次执行 trigger = TriggerBuilder.Create() .WithIdentity(taskinfo.task.taskName, "Cron") .WithCronSchedule(taskinfo.task.taskCron, x => x.WithMisfireHandlingInstructionFireAndProceed()) .ForJob(jobDetail.Key) .Build(); } else if (misfire == QMMisFire.IgnoreMisfires) { //以错过的第一个频率时间立刻开始执行 //重做错过的所有频率周期后 //当下一次触发频率发生时间大于当前时间后,再按照正常的Cron频率依次执行 trigger = TriggerBuilder.Create() .WithIdentity(taskinfo.task.taskName, "Cron") .WithCronSchedule(taskinfo.task.taskCron, x => x.WithMisfireHandlingInstructionIgnoreMisfires()) .ForJob(jobDetail.Key) .Build(); } else { //不触发立即执行 //等待下次Cron触发频率到达时刻开始按照Cron频率依次执行 trigger = TriggerBuilder.Create() .WithIdentity(taskinfo.task.taskName, "Cron") .WithCronSchedule(taskinfo.task.taskCron, x => x.WithMisfireHandlingInstructionDoNothing()) .ForJob(jobDetail.Key) .Build(); } if (!await _scheduler.CheckExists(jobDetail.Key)) { //await _scheduler.DeleteJob(jobDetail.Key); await _scheduler.ScheduleJob(jobDetail, trigger); } _taskPool.Add(taskid, taskinfo); return true; } return false; }