添加项目文件。
This commit is contained in:
1 parent
05e131ca4e
commit
514d6f5255
1162 files changed
+81255
No files matched your search
@@ -0,0 +1,111 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using HHWMS.Web.Models;
|
||||
using Quartz;
|
||||
using Quartz.Impl;
|
||||
using Quartz.Impl.Triggers;
|
||||
using WalkingTec.Mvvm.Core;
|
||||
using WalkingTec.Mvvm.Core.Support.Quartz;
|
||||
|
||||
namespace HHWMS.Web.Support
|
||||
{
|
||||
public class QuartzService
|
||||
{
|
||||
private readonly IServiceProvider _sp;
|
||||
private readonly GlobalData _gd;
|
||||
private readonly IDataContext _context;
|
||||
private IScheduler _scheduler;
|
||||
private static Task<IScheduler> task_scheduler = StdSchedulerFactory.GetDefaultScheduler();
|
||||
private static readonly object objlock = new object();
|
||||
|
||||
public QuartzService(IServiceProvider sp, GlobalData gd, IDataContext context)
|
||||
{
|
||||
_sp = sp;
|
||||
_gd = gd;
|
||||
_context = context;
|
||||
if (_scheduler == null)
|
||||
{
|
||||
lock (objlock)
|
||||
{
|
||||
if (_scheduler == null)
|
||||
{
|
||||
_scheduler = task_scheduler.Result;
|
||||
if (!_scheduler.IsStarted)
|
||||
_scheduler.Start();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async Task AddJob(SysJob jobDetail)
|
||||
{
|
||||
try
|
||||
{
|
||||
var test = _gd.AllAssembly.FirstOrDefault(x => x.FullName.Contains("HHWMS.Web"));
|
||||
var t = test.GetExportedTypes().FirstOrDefault(x => typeof(WtmJob).IsAssignableFrom(x) && x.Name == jobDetail.JobName);
|
||||
var ci = t.GetConstructor(Type.EmptyTypes);
|
||||
if (ci?.Invoke(null) as WtmJob != null)
|
||||
{
|
||||
string jobName = jobDetail.JobName;
|
||||
string groupName = jobDetail.JobName;
|
||||
if (await _scheduler.CheckExists(new JobKey(jobName, groupName)))
|
||||
{
|
||||
ModJob(jobDetail);
|
||||
return;
|
||||
}
|
||||
JobDataMap jobDataMap = new JobDataMap();
|
||||
jobDataMap.Add("Sp", _sp);
|
||||
|
||||
// 创建作业
|
||||
IJobDetail j = JobBuilder.Create(t)
|
||||
.WithIdentity(jobName, groupName)
|
||||
.UsingJobData(jobDataMap)
|
||||
.Build();
|
||||
|
||||
// 创建触发器
|
||||
ICronTrigger trigger = (ICronTrigger)TriggerBuilder.Create()
|
||||
.WithIdentity(jobName, groupName)
|
||||
.WithCronSchedule(jobDetail.CronExpression)
|
||||
.ForJob(new JobKey(jobName, groupName))
|
||||
.Build();
|
||||
|
||||
((CronTriggerImpl)trigger).MisfireInstruction = MisfireInstruction.CronTrigger.DoNothing;
|
||||
await _scheduler.ScheduleJob(j, trigger);
|
||||
|
||||
if (jobDetail.Status == SysJobEnum.stoped)
|
||||
_scheduler.PauseJob(new JobKey(jobName, groupName)).Wait();
|
||||
}
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
|
||||
public void DeleteJob(SysJob job)
|
||||
{
|
||||
_scheduler.PauseTrigger(new TriggerKey(job.JobName, job.JobGroup)).Wait();
|
||||
_scheduler.UnscheduleJob(new TriggerKey(job.JobName, job.JobGroup)).Wait();
|
||||
_scheduler.DeleteJob(new JobKey(job.JobName, job.JobGroup)).Wait();
|
||||
}
|
||||
|
||||
public void Execute(SysJob job)
|
||||
{
|
||||
_scheduler.TriggerJob(new JobKey(job.JobName, job.JobGroup)).Wait();
|
||||
}
|
||||
|
||||
public void ModJob(SysJob job)
|
||||
{
|
||||
TriggerKey triggerKey = new TriggerKey(job.JobName, job.JobGroup);
|
||||
ITrigger trigger = _scheduler.GetTrigger(triggerKey).Result;
|
||||
|
||||
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.CronSchedule(job.CronExpression);
|
||||
trigger = trigger.GetTriggerBuilder()
|
||||
.WithIdentity(triggerKey)
|
||||
.WithSchedule(scheduleBuilder.WithMisfireHandlingInstructionDoNothing())
|
||||
.Build();
|
||||
|
||||
_scheduler.RescheduleJob(triggerKey, trigger).Wait();
|
||||
if (job.Status == SysJobEnum.stoped)
|
||||
_scheduler.PauseJob(new JobKey(job.JobName, job.JobGroup));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user