添加定时任务
This commit is contained in:
parent
514b2ddfe2
commit
104cfb2463
@ -1,9 +1,13 @@
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
using JXCMS.CMS.Attribute;
|
||||
using JXCMS.CMS.Movie.Entity;
|
||||
using JXCMS.CMS.Movie.Jobs;
|
||||
using JXCMS.Core.TimingTask;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Quartz;
|
||||
|
||||
namespace JXCMS.CMS.Admin.Controllers
|
||||
namespace JXCMS.CMS.Movie.Admin.Controllers
|
||||
{
|
||||
[Area("Admin")]
|
||||
[AdminAuthentication]
|
||||
@ -24,25 +28,39 @@ namespace JXCMS.CMS.Admin.Controllers
|
||||
public IActionResult AddWebSite(WebSiteEntity webSiteEntity)
|
||||
{
|
||||
webSiteEntity.Save();
|
||||
if (webSiteEntity.IsEnable)
|
||||
{
|
||||
QuartzTask.Instance().AddTask(typeof(CollectionJob), webSiteEntity.Cron, webSiteEntity.Id.ToString(), "default", ("id", webSiteEntity.Id));
|
||||
}
|
||||
|
||||
return Redirect(Url.Action("Index"));
|
||||
}
|
||||
|
||||
public IActionResult DeleteWebSite(int id)
|
||||
{
|
||||
WebSiteEntity.Find(id).Delete();
|
||||
QuartzTask.Instance().DeleteTask(id.ToString());
|
||||
return Redirect(Url.Action("Index"));
|
||||
}
|
||||
|
||||
public IActionResult DeleteWebSiteBatch(int[] ids)
|
||||
{
|
||||
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"));
|
||||
}
|
||||
|
||||
public IActionResult EnableWebSiteBatch(int[] ids)
|
||||
{
|
||||
WebSiteEntity.Where(x => ids.Contains(x.Id)).ToUpdate()
|
||||
.Set(y => y.IsEnable, true).ExecuteAffrows();
|
||||
var webSiteEntities = WebSiteEntity.Where(x => ids.Contains(x.Id)).ToUpdate()
|
||||
.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"));
|
||||
}
|
||||
|
||||
@ -50,6 +68,7 @@ namespace JXCMS.CMS.Admin.Controllers
|
||||
{
|
||||
WebSiteEntity.Where(x => ids.Contains(x.Id)).ToUpdate()
|
||||
.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"));
|
||||
}
|
||||
|
||||
|
@ -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">
|
||||
<i class="mdi mdi-pencil"></i>
|
||||
</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>
|
||||
</a>
|
||||
</div>
|
||||
|
11
CMS/JXCMS.CMS.Movie/Entity/ClassifyEntity.cs
Normal file
11
CMS/JXCMS.CMS.Movie/Entity/ClassifyEntity.cs
Normal 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; }
|
||||
}
|
||||
}
|
19
CMS/JXCMS.CMS.Movie/Jobs/CollectionJob.cs
Normal file
19
CMS/JXCMS.CMS.Movie/Jobs/CollectionJob.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
@ -20,11 +20,13 @@
|
||||
<PackageReference Include="McMaster.NETCore.Plugins" Version="0.3.1" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
|
||||
<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.Settings.Configuration" Version="3.1.0" />
|
||||
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.2-dev-00824" />
|
||||
<PackageReference Include="Serilog.Sinks.File" Version="4.1.0" />
|
||||
<PackageReference Include="Serilog.Sinks.RollingFile" Version="3.3.1-dev-00771" />
|
||||
<PackageReference Include="TimeZoneConverter" Version="3.2.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Threading.Tasks;
|
||||
using Quartz;
|
||||
using Quartz.Impl;
|
||||
@ -29,12 +30,28 @@ namespace JXCMS.Core.TimingTask
|
||||
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 trigger = TriggerBuilder.Create().WithIdentity(jobName + "tigger").StartNow().WithCronSchedule(cron).Build();
|
||||
await _scheduler.ScheduleJob(job, trigger);
|
||||
var time = DateTime.Now;
|
||||
var job = JobBuilder.Create(jobType).WithIdentity(jobName, jobGroup).Build();
|
||||
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;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user