You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
2.7 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using jiajie;
using jiajie.Model.Entity;
using jiajie.Services.Sys.TasksQzs;
using Quartz;
using System;
using System.Diagnostics;
using System.Threading.Tasks;
namespace jiajie.Tasks
{
public class JobBase
{
public ITasksQzServices _tasksQzServices;
/// <summary>
/// 执行指定任务
/// </summary>
/// <param name="context"></param>
/// <param name="action"></param>
public async Task<string> ExecuteJob(IJobExecutionContext context, Func<Task> func)
{
//记录Job时间
Stopwatch stopwatch = new Stopwatch();
//JOBID
long jobid = context.JobDetail.Key.Name.ObjToInt();
//JOB组名
string groupName = context.JobDetail.Key.Group;
//日志
string jobHistory = $"【{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}】【执行开始】【Id{jobid},组别:{groupName}】";
//耗时
double taskSeconds = 0;
try
{
stopwatch.Start();
await func();//执行任务
stopwatch.Stop();
jobHistory += $",【{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}】【执行成功】";
}
catch (Exception ex)
{
JobExecutionException e2 = new JobExecutionException(ex);
//true 是立即重新执行任务
e2.RefireImmediately = true;
jobHistory += $",【{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}】【执行失败:{ex.Message}】";
}
finally
{
taskSeconds = Math.Round(stopwatch.Elapsed.TotalSeconds, 3);
jobHistory += $",【{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}】【执行结束】(耗时:{taskSeconds}秒)";
if (_tasksQzServices != null)
{
var model = await _tasksQzServices.GetOneById<TasksQz,long>(jobid);
if (model != null)
{
model.RunTimes += 1;
var separator = "<br>";
// 这里注意数据库字段的长度问题超过限制会造成数据库remark不更新问题。
model.Remark = jobHistory;
//model.Remark = $"{jobHistory}{separator}" + string.Join(separator, StringHelper.GetTopDataBySeparator(model.Remark, separator, 9));
await _tasksQzServices.UpdOne<TasksQz, long>(model);
}
}
}
Console.Out.WriteLine(jobHistory);
return jobHistory;
}
}
}