提交.Net6版本
This commit is contained in:
44
Yi.Framework.Net6/Yi.Framework.Task/HttpJob.cs
Normal file
44
Yi.Framework.Net6/Yi.Framework.Task/HttpJob.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Quartz;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Yi.Framework.Common.Models;
|
||||
|
||||
namespace Yi.Framework.Job
|
||||
{
|
||||
public class HttpJob : IJob
|
||||
{
|
||||
private ILogger<HttpJob> _logger;
|
||||
public HttpJob(ILogger<HttpJob> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public Task Execute(IJobExecutionContext context)
|
||||
{
|
||||
return Task.Run(() =>
|
||||
{
|
||||
var jobData = context.JobDetail.JobDataMap;
|
||||
string method= jobData[Common.Const.JobConst.method].ToString();
|
||||
string url = jobData[Common.Const.JobConst.url].ToString();
|
||||
string data="异常!";
|
||||
switch (method)
|
||||
{
|
||||
case "post":
|
||||
data = Common.Helper.HttpHelper.HttpPost(url);
|
||||
break;
|
||||
case "get":
|
||||
data = Common.Helper.HttpHelper.HttpGet(url);
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
_logger.LogWarning("定时任务开始调度:" + nameof(HttpJob) + ":" + DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + $":访问地址为:{url},结果为:{data}");
|
||||
Console.WriteLine($"结果:{data}");
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
43
Yi.Framework.Net6/Yi.Framework.Task/VisitJob.cs
Normal file
43
Yi.Framework.Net6/Yi.Framework.Task/VisitJob.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Quartz;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Yi.Framework.Common.Models;
|
||||
using Yi.Framework.Model.ModelFactory;
|
||||
using Yi.Framework.Model.Models;
|
||||
|
||||
namespace Yi.Framework.Job
|
||||
{
|
||||
public class VisitJob : IJob
|
||||
{
|
||||
private ILogger<VisitJob> _logger;
|
||||
private DbContext _DBWrite;
|
||||
public VisitJob(ILogger<VisitJob> logger, IDbContextFactory DbFactory)
|
||||
{
|
||||
_logger = logger;
|
||||
_DBWrite = DbFactory.ConnWriteOrRead(Common.Enum.WriteAndReadEnum.Write);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 应该将拜访清零,并且写入数据库中的拜访表中
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
/// <returns></returns>
|
||||
public Task Execute(IJobExecutionContext context)
|
||||
{
|
||||
return Task.Run(() =>
|
||||
{
|
||||
_DBWrite.Set<visit>().Add(new visit() { num = JobModel.visitNum, time = DateTime.Now });
|
||||
_DBWrite.SaveChanges();
|
||||
_logger.LogWarning("定时任务开始调度:" + nameof(VisitJob) + ":" + DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + $":访问总数为:{JobModel.visitNum}");
|
||||
JobModel.visitNum = 0;
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
16
Yi.Framework.Net6/Yi.Framework.Task/Yi.Framework.Job.csproj
Normal file
16
Yi.Framework.Net6/Yi.Framework.Task/Yi.Framework.Job.csproj
Normal file
@@ -0,0 +1,16 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Quartz.AspNetCore" Version="3.3.3" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Yi.Framework.Common\Yi.Framework.Common.csproj" />
|
||||
<ProjectReference Include="..\Yi.Framework.Model\Yi.Framework.Model.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Reference in New Issue
Block a user