/// <summary> /// 转化为bsonDocList对象 /// </summary> /// <param name="docStr"></param> /// <returns></returns> private static List <BsonDocument> ConvertToBsonDocList(string docStr) { try { var dataDoc = MongoDB.Bson.Serialization.BsonSerializer.Deserialize <List <BsonDocument> >(docStr); return(dataDoc); } catch (Exception ex) { JobLogger.Info(ex.Message); } return(new List <BsonDocument>()); }
/// <summary> /// 通过执行webapi请求与中控数据库进行交互,进行token集成验证请求有效性 /// </summary> /// <param name="url"></param> /// <param name="paramDoc"></param> /// <returns></returns> public static string ExecHttpWebAPI(string url, BsonDocument paramDoc) { var postData = string.Empty; var encodeUrl = string.Empty; try { //JobLogger.Info("开始进行url请求{0}\n{1}", url, GeneratePostData(paramDoc)); encodeUrl = PageReq.UrlGenerateSign(CustomerConfig.ApiUrl + url); //获取加密后的url请求 var http = new HttpHelper(); //创建Httphelper参数对象 HttpItem item = new HttpItem() { URL = encodeUrl, //URL 必需项 UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36", //返回类型 可选项有默认值 Timeout = 25000, Accept = "*/*", Method = "Get", ContentType = "application/x-www-form-urlencoded; charset=UTF-8", Expect100Continue = false, KeepAlive = true }; item.Header.Add("Accept-Encoding", "gzip, deflate");//启用压缩 postData = GeneratePostData(paramDoc); if (!string.IsNullOrEmpty(postData)) { item.Method = "Post";//URL 可选项 默认为Get item.Postdata = postData; } var result = http.GetHtml(item); if (result.StatusCode == System.Net.HttpStatusCode.OK) { JobLogger.ApiExecInfo("{0},{1}\n{2}\n\r", encodeUrl, postData, result.Html); return(result.Html); } else { JobLogger.ApiExecInfo("{0},{1}\n{2}\n\r", encodeUrl, postData, result.Html); return(string.Empty); } } catch (Exception ex) { JobLogger.Info("{0},{1},{2}\n\r", encodeUrl, postData, ex.Message); } return(string.Empty); }
/// <summary> /// Job调度 /// </summary> /// <param name="scheduler"></param> /// <param name="jobInfo"></param> public void ScheduleJob(IScheduler scheduler, BackgroundJob jobInfo) { JobLogger.Info("加入调度{0} ", jobInfo.name); if (ValidExpression(jobInfo.cron)) { Type type = GetClassInfo(jobInfo.assemblyName, jobInfo.className); if (type != null) { IJobDetail job = new JobDetailImpl(jobInfo.jobId.ToString(), "Group", type); job.JobDataMap.Add("Parameters", jobInfo.jobArgs); job.JobDataMap.Add("JobName", jobInfo.name); job.JobDataMap.Add("JobType", jobInfo.jobType); //类型 job.JobDataMap.Add("ClassName", jobInfo.className); // job.JobDataMap.Add("AssemblyName", jobInfo.assemblyName); //处理类 job.JobDataMap.Add("statementData", jobInfo.statementData); //处理类参数 CronTriggerImpl trigger = new CronTriggerImpl(); trigger.CronExpressionString = jobInfo.cron; trigger.Name = jobInfo.jobId.ToString(); trigger.Description = jobInfo.description; trigger.StartTimeUtc = DateTime.UtcNow; trigger.Group = jobInfo.jobId + "TriggerGroup"; scheduler.ScheduleJob(job, trigger); } else { var resultContent = new BsonDocument(); resultContent.Set("status", "false"); resultContent.Set("errorMessage", jobInfo.assemblyName + jobInfo.className + "无效,无法启动该任务"); new BackgroundJobService().WriteBackgroundJoLog(jobInfo, DateTime.Now, resultContent.ToJson()); } } else { var resultContent = new BsonDocument(); resultContent.Set("status", "false"); resultContent.Set("errorMessage", string.Format($"{jobInfo.cron}不是正确的Cron表达式,无法启动该任务")); new BackgroundJobService().WriteBackgroundJoLog(jobInfo, DateTime.Now, resultContent.ToJson()); } }
/// <summary> /// Job状态管控 /// </summary> /// <param name="Scheduler"></param> public void JobScheduler(IScheduler Scheduler) { //请求客户配置是否发生了改变,没改变保持当前运行,通知服务进行更新 var canContinue = CanContinueJobScheduler(); if (canContinue == false) { return; } List <BackgroundJob> list = new BackgroundJobService().GeAllowScheduleJobInfoList(); JobLogger.Info($"获得Job个数:{list.Count}"); //定时获取job数据,此处需要判断数据为0的情况是因为网络情况导致的异常 if (list != null && list.Count > 0) { var allRunningJobKeys = new List <JobKey>(); lastExecDate = DateTime.Now; foreach (BackgroundJob jobInfo in list) { ///保证只有一个managerjob运行,且管理job不能停止;默认运行器的ID将联系对应管理JOB删除默认job if (jobInfo.jobType == ((int)BackgroundJobType.Manager).ToString() && jobInfo.jobId != CustomerConfig.Managerjobkey)//有当前的默认job { JobKey managerJobKey = new JobKey(CustomerConfig.Managerjobkey, "Group"); if (Scheduler.CheckExists(managerJobKey)) { Scheduler.DeleteJob(managerJobKey);//删除默认job } } JobKey jobKey = new JobKey(jobInfo.jobId.ToString(), "Group"); ///job不存在 if (Scheduler.CheckExists(jobKey) == false) { if (jobInfo.state == (int)BackgroundJobStateType.Running || jobInfo.state == (int)BackgroundJobStateType.Luanch) { ScheduleJob(Scheduler, jobInfo); if (Scheduler.CheckExists(jobKey) == false) { //设置为停止 new BackgroundJobService().UpdateBackgroundJobState(jobInfo.jobId, (int)BackgroundJobStateType.Stop); } else { if (!allRunningJobKeys.Contains(jobKey)) { allRunningJobKeys.Add(jobKey); } ////设置为运行中 new BackgroundJobService().UpdateBackgroundJobState(jobInfo.jobId, (int)BackgroundJobStateType.Running); } } else if (jobInfo.state == (int)BackgroundJobStateType.Stopping) { ////设置为停止 new BackgroundJobService().UpdateBackgroundJobState(jobInfo.jobId, (int)BackgroundJobStateType.Stop); } } else//已经在运行 { if (jobInfo.state == (int)BackgroundJobStateType.Stopping) { Scheduler.DeleteJob(jobKey); ////设置为停止 new BackgroundJobService().UpdateBackgroundJobState(jobInfo.jobId, (int)BackgroundJobStateType.Stop); } else if (jobInfo.state == (int)BackgroundJobStateType.Luanch) { if (!allRunningJobKeys.Contains(jobKey)) { allRunningJobKeys.Add(jobKey); } ////设置为运行 new BackgroundJobService().UpdateBackgroundJobState(jobInfo.jobId, (int)BackgroundJobStateType.Running); } else if (jobInfo.state == (int)BackgroundJobStateType.Running) { if (!allRunningJobKeys.Contains(jobKey)) { allRunningJobKeys.Add(jobKey); } } } } //检查无效状态的任务 InvalidJobCheck(Scheduler, allRunningJobKeys); } }