/// <summary> /// 任务添加 /// </summary> /// <param name="job"></param> /// <param name="context"></param> /// <returns></returns> private async Task JobAdd(JobEntity job, IJobExecutionContext context) { try { string group = job.Group; string name = job.Name; JobKey jobKey = new JobKey(name, group); if (!await context.Scheduler.CheckExists(jobKey)) { IJobDetail jobDetail = JobBuilder.Create <BaseJob>() .WithIdentity(jobKey) .UsingJobData("RequestUrl", job.RequestUrl) .UsingJobData("ID", job.Id) .Build(); ITrigger trigger = TriggerBuilder.Create() .WithIdentity(group, name) .StartNow() .WithCronSchedule(job.CronExpression) .Build(); await context.Scheduler.ScheduleJob(jobDetail, trigger); await _jobService.UpdateStatusAsync(job.Id, (int)JobStatusEnum.Running); } } catch (Exception ex) { await _jobRunLogService.InsertAsync(new JobRunLogEntity() { JobGroup = job.Group, JobName = job.Name, StartTime = DateTime.Now, Succ = false, Exception = "任务重启失败:" + ex.StackTrace }); } }
public async Task Execute(IJobExecutionContext context) { var sw = new Stopwatch(); sw.Start(); DateTime dateTime = DateTime.Now; var id = context.JobDetail.JobDataMap.GetIntValue("ID"); // var RequestUrl = context.JobDetail.JobDataMap.GetString("RequestUrl"); JobEntity job = await _jobService.GetAsync(id); AbstractTrigger trigger = (context as JobExecutionContextImpl).Trigger as AbstractTrigger; if (job == null) { await _jobRunLogService.InsertAsync(new JobRunLogEntity() { JobGroup = trigger.Group, JobName = trigger.Name, StartTime = DateTime.Now, Succ = false, Exception = "未到找作业或可能被移除" }); return; } Utility.FileHelper.WriteFile(FileQuartz.LogPath + trigger.Group, $"{trigger.Name}.txt", $"作业[{job.Name}]开始:{ DateTime.Now:yyyy-MM-dd HH:mm:sss}", true); if (string.IsNullOrEmpty(job.RequestUrl) || job.RequestUrl == "/") { Utility.FileHelper.WriteFile(FileQuartz.LogPath + trigger.Group, $"{trigger.Name}.txt", $"{ DateTime.Now:yyyy-MM-dd HH:mm:sss}未配置url,", true); await _jobRunLogService.InsertAsync(new JobRunLogEntity() { JobGroup = trigger.Group, JobName = trigger.Name, StartTime = DateTime.Now, Succ = false, Exception = "未配置url" }); return; } try { string httpMessage = ""; var client = _clientFactory.CreateClient(); var request = new HttpRequestMessage(job.RequestType?.ToLower() == "get" ? HttpMethod.Get : HttpMethod.Post, job.RequestUrl); Dictionary <string, string> headers = new Dictionary <string, string>(); if (!string.IsNullOrEmpty(job.AuthKey) && !string.IsNullOrEmpty(job.AuthValue)) { headers.Add(job.AuthKey.Trim(), job.AuthValue.Trim()); } if (headers != null && headers.Count > 0) { foreach (var header in headers) { request.Headers.Add(header.Key, header.Value); } } var response = await client.SendAsync(request); if (response.IsSuccessStatusCode) { httpMessage = await response.Content.ReadAsStringAsync(); } sw.Stop(); await _jobRunLogService.InsertAsync(new JobRunLogEntity() { JobGroup = job.Group, JobName = job.Name, StartTime = DateTime.Now, Succ = response.IsSuccessStatusCode, Exception = response.IsSuccessStatusCode?httpMessage: response.ReasonPhrase, RequestMessage = response.ToJson(), StatusCode = (int)response.StatusCode, TotalSeconds = (int)sw.ElapsedMilliseconds }); string logContent = $"{DateTime.Now:yyyy-MM-dd HH:mm:ss}_{dateTime:yyyy-MM-dd HH:mm:ss}_{(string.IsNullOrEmpty(httpMessage) ? "" : httpMessage)}\r\n"; Utility.FileHelper.WriteFile(FileQuartz.LogPath + job.Group + "\\", $"{job.Name}.txt", logContent, true); Console.WriteLine(trigger.FullName + " " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:sss") + " " + httpMessage); } catch (Exception ex) { await _jobRunLogService.InsertAsync(new JobRunLogEntity() { JobGroup = job.Group, JobName = job.Name, StartTime = DateTime.Now, Succ = false, Exception = ex.Message, TotalSeconds = (int)sw.ElapsedMilliseconds }); } }