public Task Execute(IJobExecutionContext context) { return(Task.Run(() => { IJobDetail job = context.JobDetail; JobDataMap datas = job.JobDataMap; SchedTask schedTask = (SchedTask)datas["schedTask"]; if (schedTask != null) { var ssss = EnableTaskModule.GetAllTask(); ++schedTask.InvokeNum; _schedTask = schedTask; if (schedTask.Request.IsCancel) // 取消请求 { Del(); return; } // 启动爬取 schedTask.Action?.Invoke(schedTask.Request, null); if (!schedTask.Request.IsCycle && schedTask.InvokeNum >= schedTask.Request.CycleNum)// 是否循环结束 { Del(); return; } if (CheckTask(schedTask)) { return; } } })); }
/// <summary> /// 检查状态,过滤无效爬取 /// </summary> /// <param name="schedTask"></param> /// <returns></returns> public bool CheckTask(SchedTask schedTask) { var status = schedTask.TriggerState; if (status == TriggerState.Error) { return(Del()); } return(false); }
internal void RunTask(SchedTask tsk) { lock (_running) { if (_running.Contains(tsk.Id)) { log.Debug("Task already running: {0}", tsk.Id); return; } _running.Add(tsk.Id); } DateTime dt = DateTime.Now; try { tsk.NextTrigger = DateTime.Now.AddSeconds(tsk.TaskData.IntervalSeconds); CreateAndRunTask(tsk); } catch (Exception ex) { log.Error("Error running task {0}: {1}", tsk.Id, ex); ReportTaskFailed(tsk.Id, ex); } finally { lock (_running) { _running.Remove(tsk.Id); } log.Debug("Task {0} finished. Time: {1}, Next trigger: {2}", tsk.Id, DateTime.Now - dt, tsk.NextTrigger); } }
protected void UpdateScheduledJobs() { log.Debug("Updating scheduler jobs. group: {0}", this.SchedulerGroup); string pid = System.Diagnostics.Process.GetCurrentProcess().Id.ToString(); string[] pcl = PerfCounters == null ? null : PerfCounters.GetPerfCounterNames().ToArray(); var resp = CogMon.CallService<GetActiveScheduledJobsResponse>(new GetActiveScheduledJobs { Groups = string.IsNullOrEmpty(SchedulerGroup) ? (string[]) null : new string[] {SchedulerGroup}, PerfCounters = pcl, AgentPID = pid }); List<SchedTask> lst = new List<SchedTask>(); var tsks = _tasks; foreach (var t in resp.Tasks) { if (!string.IsNullOrEmpty(TestJobId) && t.Id != TestJobId) continue; SchedTask pt = tsks.FirstOrDefault(x => x.Id == t.Id); if (pt != null) { pt.TaskData = t; } else pt = new SchedTask { Id = t.Id, TaskData = t, NextTrigger = t.LastRun.AddSeconds(t.IntervalSeconds) < DateTime.Now ? DateTime.Now.AddSeconds(_r.Next(60)) : t.LastRun.AddSeconds(t.IntervalSeconds) }; lst.Add(pt); } log.Debug("Updated job list: {0} jobs", lst.Count); _tasks = lst; }
internal void CreateAndRunTask(SchedTask t) { var ti = ServiceResolver.GetInstance<JobBase>(t.TaskData.QueryMethod.ToString()); var json = JsonConvert.SerializeObject(t.TaskData); JsonConvert.PopulateObject(json, ti); ti.Execute(); }