Skip to content

Commit

Permalink
add second job,and Fixed #123 Bug
Browse files Browse the repository at this point in the history
  • Loading branch information
anjoy8 committed Oct 23, 2020
1 parent 900f9fe commit 172460d
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 1 deletion.
1 change: 1 addition & 0 deletions Blog.Core.Extensions/ServiceExtensions/JobSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public static void AddJobSetup(this IServiceCollection services)

services.AddSingleton<IJobFactory, JobFactory>();
services.AddTransient<Job_Blogs_Quartz>();//Job使用瞬时依赖注入
services.AddTransient<Job_OperateLog_Quartz>();//Job使用瞬时依赖注入
services.AddSingleton<ISchedulerCenter, SchedulerCenterServer>();
}
}
Expand Down
14 changes: 14 additions & 0 deletions Blog.Core.IServices/IOperateLogServices.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Blog.Core.IServices.BASE;
using Blog.Core.Model.Models;

namespace Blog.Core.IServices
{
/// <summary>
/// IOperateLogServices
/// </summary>
public interface IOperateLogServices : IBaseServices<OperateLog>
{

}
}

18 changes: 18 additions & 0 deletions Blog.Core.Services/OperateLogServices.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Blog.Core.IRepository.Base;
using Blog.Core.IServices;
using Blog.Core.Model.Models;
using Blog.Core.Services.BASE;

namespace Blog.Core.Services
{
public partial class OperateLogServices : BaseServices<OperateLog>, IOperateLogServices
{
IBaseRepository<OperateLog> _dal;
public OperateLogServices(IBaseRepository<OperateLog> dal)
{
this._dal = dal;
base.BaseDal = dal;
}

}
}
2 changes: 1 addition & 1 deletion Blog.Core.Tasks/QuartzNet/Jobs/Job_Blogs_Quartz.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public async Task Run(IJobExecutionContext context, int jobid)
model.RunTimes += 1;
var separator = "<br>";
model.Remark =
$"【{DateTime.Now}】执行任务【Id:{context.JobDetail.Key.Name},组别:{context.JobDetail.Key.Group}】【执行成功】{separator}"
$"【{DateTime.Now}】执行任务【Id:{context.JobDetail.Key.Name},组别:{context.JobDetail.Key.Group}】【执行成功】:博客数{list.Count}{separator}"
+ string.Join(separator, StringHelper.GetTopDataBySeparator(model.Remark, separator, 9));

await _tasksQzServices.Update(model);
Expand Down
102 changes: 102 additions & 0 deletions Blog.Core.Tasks/QuartzNet/Jobs/Job_OperateLog_Quartz.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
using Blog.Core.Common.Helper;
using Blog.Core.Common.LogHelper;
using Blog.Core.IServices;
using Blog.Core.Model.Models;
using Microsoft.AspNetCore.Hosting;
using Quartz;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

/// <summary>
/// 这里要注意下,命名空间和程序集是一样的,不然反射不到
/// </summary>
namespace Blog.Core.Tasks
{
public class Job_OperateLog_Quartz : JobBase, IJob
{
private readonly IOperateLogServices _operateLogServices;
private readonly ITasksQzServices _tasksQzServices;
private readonly IWebHostEnvironment _environment;

public Job_OperateLog_Quartz(IOperateLogServices operateLogServices, ITasksQzServices tasksQzServices, IWebHostEnvironment environment)
{
_operateLogServices = operateLogServices;
_tasksQzServices = tasksQzServices;
_environment = environment;
}
public async Task Execute(IJobExecutionContext context)
{
// 可以直接获取 JobDetail 的值
var jobKey = context.JobDetail.Key;
var jobId = jobKey.Name;

var executeLog = await ExecuteJob(context, async () => await Run(context, jobId.ObjToInt()));

// 也可以通过数据库配置,获取传递过来的参数
JobDataMap data = context.JobDetail.JobDataMap;
}
public async Task Run(IJobExecutionContext context, int jobid)
{
List<LogInfo> excLogs = new List<LogInfo>();
var exclogContent = LogLock.ReadLog(Path.Combine(_environment.ContentRootPath, "Log"), $"GlobalExceptionLogs_{DateTime.Now.ToString("yyyMMdd")}.log", Encoding.UTF8);

if (!string.IsNullOrEmpty(exclogContent))
{
excLogs = exclogContent.Split("--------------------------------")
.Where(d => !string.IsNullOrEmpty(d) && d != "\n" && d != "\r\n")
.Select(d => new LogInfo
{
Datetime = (d.Split("|")[0]).Split(',')[0].ObjToDate(),
Content = d.Split("|")[1]?.Replace("\r\n", "<br>"),
LogColor = "EXC",
Import = 9,
}).ToList();
}

var filterDatetime = DateTime.Now.AddHours(-1);
excLogs = excLogs.Where(d => d.Datetime >= filterDatetime).ToList();

var operateLogs = new List<OperateLog>() { };
excLogs.ForEach(m =>
{
operateLogs.Add(new OperateLog()
{
LogTime = m.Datetime,
Description = m.Content,
IPAddress = m.IP,
UserId = 0,
IsDeleted = false,
});
});


if (operateLogs.Count > 0)
{
var logsIds = await _operateLogServices.Add(operateLogs);
}

if (jobid > 0)
{
var model = await _tasksQzServices.QueryById(jobid);
if (model != null)
{
var list = await _operateLogServices.Query(d => d.IsDeleted == false);
model.RunTimes += 1;
var separator = "<br>";
model.Remark =
$"【{DateTime.Now}】执行任务【Id:{context.JobDetail.Key.Name},组别:{context.JobDetail.Key.Group}】【执行成功】:异常数{list.Count}{separator}"
+ string.Join(separator, StringHelper.GetTopDataBySeparator(model.Remark, separator, 9));

await _tasksQzServices.Update(model);
}
}
}
}



}
2 changes: 2 additions & 0 deletions Blog.Core.Tasks/QuartzNet/SchedulerCenterServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,8 @@ public async Task<MessageModel<string>> ResumeJob(TasksQz tasksQz)
trigger = CreateSimpleTrigger(tasksQz);
}

((CronTriggerImpl)trigger).MisfireInstruction = MisfireInstruction.CronTrigger.DoNothing;

TriggerKey triggerKey = new TriggerKey(tasksQz.Id.ToString(), tasksQz.JobGroup);
await _scheduler.Result.RescheduleJob(triggerKey, trigger);

Expand Down

0 comments on commit 172460d

Please sign in to comment.