添加定时任务

This commit is contained in:
j4587698 2020-02-12 15:23:34 +08:00
parent 514b2ddfe2
commit 104cfb2463
6 changed files with 79 additions and 8 deletions

View File

@ -1,9 +1,13 @@
using System.Collections.ObjectModel;
using System.Linq; using System.Linq;
using JXCMS.CMS.Attribute; using JXCMS.CMS.Attribute;
using JXCMS.CMS.Movie.Entity; using JXCMS.CMS.Movie.Entity;
using JXCMS.CMS.Movie.Jobs;
using JXCMS.Core.TimingTask;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Quartz;
namespace JXCMS.CMS.Admin.Controllers namespace JXCMS.CMS.Movie.Admin.Controllers
{ {
[Area("Admin")] [Area("Admin")]
[AdminAuthentication] [AdminAuthentication]
@ -24,25 +28,39 @@ namespace JXCMS.CMS.Admin.Controllers
public IActionResult AddWebSite(WebSiteEntity webSiteEntity) public IActionResult AddWebSite(WebSiteEntity webSiteEntity)
{ {
webSiteEntity.Save(); webSiteEntity.Save();
if (webSiteEntity.IsEnable)
{
QuartzTask.Instance().AddTask(typeof(CollectionJob), webSiteEntity.Cron, webSiteEntity.Id.ToString(), "default", ("id", webSiteEntity.Id));
}
return Redirect(Url.Action("Index")); return Redirect(Url.Action("Index"));
} }
public IActionResult DeleteWebSite(int id) public IActionResult DeleteWebSite(int id)
{ {
WebSiteEntity.Find(id).Delete(); WebSiteEntity.Find(id).Delete();
QuartzTask.Instance().DeleteTask(id.ToString());
return Redirect(Url.Action("Index")); return Redirect(Url.Action("Index"));
} }
public IActionResult DeleteWebSiteBatch(int[] ids) public IActionResult DeleteWebSiteBatch(int[] ids)
{ {
WebSiteEntity.Where(x => ids.Contains(x.Id)).ToDelete().ExecuteAffrows(); WebSiteEntity.Where(x => ids.Contains(x.Id)).ToDelete().ExecuteAffrows();
QuartzTask.Instance()
.DeleteTasks(
new ReadOnlyCollection<JobKey>(ids.Select(x => new JobKey(x.ToString(), "default")).ToList()));
return Redirect(Url.Action("Index")); return Redirect(Url.Action("Index"));
} }
public IActionResult EnableWebSiteBatch(int[] ids) public IActionResult EnableWebSiteBatch(int[] ids)
{ {
WebSiteEntity.Where(x => ids.Contains(x.Id)).ToUpdate() var webSiteEntities = WebSiteEntity.Where(x => ids.Contains(x.Id)).ToUpdate()
.Set(y => y.IsEnable, true).ExecuteAffrows(); .Set(y => y.IsEnable, true).ExecuteUpdated();
foreach (var webSiteEntity in webSiteEntities)
{
QuartzTask.Instance().AddTask(typeof(CollectionJob), webSiteEntity.Cron, webSiteEntity.Id.ToString(),
"default", ("id", webSiteEntity.Id));
}
return Redirect(Url.Action("Index")); return Redirect(Url.Action("Index"));
} }
@ -50,6 +68,7 @@ namespace JXCMS.CMS.Admin.Controllers
{ {
WebSiteEntity.Where(x => ids.Contains(x.Id)).ToUpdate() WebSiteEntity.Where(x => ids.Contains(x.Id)).ToUpdate()
.Set(y => y.IsEnable, false).ExecuteAffrows(); .Set(y => y.IsEnable, false).ExecuteAffrows();
QuartzTask.Instance().DeleteTasks(new ReadOnlyCollection<JobKey>(ids.Select(x => new JobKey(x.ToString(), "default")).ToList()));
return Redirect(Url.Action("Index")); return Redirect(Url.Action("Index"));
} }

View File

@ -70,7 +70,10 @@
<a class="btn btn-xs btn-default" data-src="@Url.Action("WebSiteDialog", "Collection", new {id = webSiteEntity.Id})" title="编辑" data-toggle="modal" data-target="#exampleModal"> <a class="btn btn-xs btn-default" data-src="@Url.Action("WebSiteDialog", "Collection", new {id = webSiteEntity.Id})" title="编辑" data-toggle="modal" data-target="#exampleModal">
<i class="mdi mdi-pencil"></i> <i class="mdi mdi-pencil"></i>
</a> </a>
<a class="btn btn-xs btn-default" href="#!" title="删除" onclick="delWebsite(@webSiteEntity.Id, '@webSiteEntity.WebSiteName')"> <a class="btn btn-xs btn-default" href="#" title="绑定分类" onclick="delWebsite(@webSiteEntity.Id, '@webSiteEntity.WebSiteName')">
<i class="mdi mdi-link-variant"></i>
</a>
<a class="btn btn-xs btn-default" href="#" title="删除" onclick="delWebsite(@webSiteEntity.Id, '@webSiteEntity.WebSiteName')">
<i class="mdi mdi-window-close"></i> <i class="mdi mdi-window-close"></i>
</a> </a>
</div> </div>

View File

@ -0,0 +1,11 @@
using FreeSql;
namespace JXCMS.CMS.Movie.Entity
{
public class ClassifyEntity : BaseEntity<ClassifyEntity, int>
{
public string Name { get; set; }
public int ParentId { get; set; }
}
}

View File

@ -0,0 +1,19 @@
using System;
using System.Threading.Tasks;
using JXCMS.CMS.Movie.Entity;
using Quartz;
namespace JXCMS.CMS.Movie.Jobs
{
public class CollectionJob : IJob
{
public Task Execute(IJobExecutionContext context)
{
var id = context.MergedJobDataMap.GetIntValue("id");
var websiteEntity = WebSiteEntity.Find(id);
Console.WriteLine(id);
return Task.CompletedTask;
}
}
}

View File

@ -20,11 +20,13 @@
<PackageReference Include="McMaster.NETCore.Plugins" Version="0.3.1" /> <PackageReference Include="McMaster.NETCore.Plugins" Version="0.3.1" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" /> <PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="Quartz" Version="3.0.7" /> <PackageReference Include="Quartz" Version="3.0.7" />
<PackageReference Include="Quartz.Plugins.TimeZoneConverter" Version="3.0.7" />
<PackageReference Include="Serilog.AspNetCore" Version="3.2.0" /> <PackageReference Include="Serilog.AspNetCore" Version="3.2.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="3.1.0" /> <PackageReference Include="Serilog.Settings.Configuration" Version="3.1.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.2-dev-00824" /> <PackageReference Include="Serilog.Sinks.Console" Version="3.1.2-dev-00824" />
<PackageReference Include="Serilog.Sinks.File" Version="4.1.0" /> <PackageReference Include="Serilog.Sinks.File" Version="4.1.0" />
<PackageReference Include="Serilog.Sinks.RollingFile" Version="3.3.1-dev-00771" /> <PackageReference Include="Serilog.Sinks.RollingFile" Version="3.3.1-dev-00771" />
<PackageReference Include="TimeZoneConverter" Version="3.2.0" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -1,4 +1,5 @@
using System; using System;
using System.Collections.ObjectModel;
using System.Threading.Tasks; using System.Threading.Tasks;
using Quartz; using Quartz;
using Quartz.Impl; using Quartz.Impl;
@ -29,12 +30,28 @@ namespace JXCMS.Core.TimingTask
return _instance; return _instance;
} }
public async Task<bool> AddTask(Type jobType, string cron, string jobName, string jobGroup = "default") public async Task<bool> AddTask(Type jobType, string cron, string jobName, string jobGroup = "default", params (string name, object value)[] param)
{ {
var job = JobBuilder.Create(jobType).WithIdentity("job", "job").Build(); var time = DateTime.Now;
var trigger = TriggerBuilder.Create().WithIdentity(jobName + "tigger").StartNow().WithCronSchedule(cron).Build(); var job = JobBuilder.Create(jobType).WithIdentity(jobName, jobGroup).Build();
await _scheduler.ScheduleJob(job, trigger); var trigger = TriggerBuilder.Create().WithIdentity(jobName + "trigger").StartNow().WithCronSchedule(cron).Build();
foreach (var par in param)
{
job.JobDataMap.Put(par.name, par.value);
}
var offset = await _scheduler.ScheduleJob(job, trigger);
return true; return true;
} }
public async Task<bool> DeleteTask(string jobName, string jobGroup = "default")
{
return await _scheduler.DeleteJob(new JobKey(jobName, jobGroup));
}
public async Task<bool> DeleteTasks(ReadOnlyCollection<JobKey> jobKeys)
{
return await _scheduler.DeleteJobs(jobKeys);
}
} }
} }