/// <summary> /// 通过执行webapi请求与中控数据库进行交互,进行token集成验证请求有效性 /// </summary> /// <param name="url"></param> /// <param name="paramDoc"></param> /// <returns></returns> public static string ExecHttpWebAPIDoc(string url, BsonDocument paramDoc) { try { var resultHtml = ExecHttpWebAPI(url, paramDoc); if (!string.IsNullOrEmpty(resultHtml) && resultHtml.Contains("data")) { //返回数据需要多次进行转义返回数据,后续当成加密方式使用 //var html = DataDecode(resultHtml); JObject curData = JObject.Parse(resultHtml); if (curData["status"] != null && curData["status"].ToString() == "true") { return(curData["data"].ToString()); } else { JobLogger.Info(curData["message"].ToString()); } } } catch (Exception ex) { JobLogger.Info(ex.Message); } return(string.Empty); }
/// <summary> /// 检测无效任务并进行删除操作,当任务正在进行然后手动将状态设置为0的时候因为获取不到任务,但是本地服务仍然在运行,也无法更新状态回中控, /// 因此需要将该任务与中控同步 /// 当服务正在跑但是寻找不到任何list,此时不应该影响现有正在跑的调度服务,发送一条日志服务取不到任何ListJob回中控 /// </summary> public void InvalidJobCheck(IScheduler Scheduler, List <JobKey> runningJobKeylist) { try { if (runningJobKeylist.Count > 0) { var allSchedulerKeys = Scheduler.GetJobKeys(GroupMatcher <JobKey> .GroupEquals("Group")); ///正在进行的jobKey比回传回来的并且运行的Jobkey多,将多余的JobKeyDelete掉并保持与中控服务同步 if (allSchedulerKeys.Count > runningJobKeylist.Count) { foreach (var jobKey in allSchedulerKeys) { if (!runningJobKeylist.Contains(jobKey)) { Scheduler.DeleteJob(jobKey);//删除job JobLogger.Info($"{ jobKey.Name}被远程直接设置为stop"); } } } } } catch (Exception ex) { JobLogger.Info("InvalidJobCheck:" + ex.Message); } }
/// <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); } }