using Quartz;
|
using Quartz.Impl;
|
using System;
|
using System.Collections.Specialized;
|
using System.Reflection;
|
using System.Threading.Tasks;
|
using WIDESEA.Core.Utilities;
|
using WIDESEA.Services.IServices;
|
|
namespace WIDESEA.Job
|
{
|
public class SchedulerCenterServer
|
{
|
private Task<IScheduler> _scheduler;
|
private StdSchedulerFactory _jobFactory;
|
|
|
public SchedulerCenterServer(StdSchedulerFactory jobFactory)
|
{
|
_jobFactory = jobFactory;
|
_scheduler = GetSchedulerAsync();
|
}
|
private Task<IScheduler> GetSchedulerAsync()
|
{
|
if (_scheduler != null)
|
return this._scheduler;
|
else
|
{
|
// 从Factory中获取Scheduler实例
|
NameValueCollection collection = new NameValueCollection
|
{
|
{ "quartz.serializer.type", "binary" },
|
};
|
StdSchedulerFactory factory = new StdSchedulerFactory(collection);
|
return _scheduler = factory.GetScheduler();
|
}
|
}
|
#region 将任务计划添加到调度中心
|
/// <summary>
|
/// 添加一个任务
|
/// </summary>
|
/// <param name="job"></param>
|
/// <returns></returns>
|
public async Task<WebResponseContent> AddScheduleJobAsync(string jobClassName)
|
{
|
WebResponseContent content = new WebResponseContent();
|
string jobName = jobClassName;
|
string jobGroup = "jobGroup";
|
string jobAssembName = "WIDESEA.Job";
|
string className = jobClassName;
|
try
|
{
|
JobKey jobKey = new JobKey(jobName, jobGroup);
|
if (!await this._scheduler.Result.CheckExists(jobKey))
|
{
|
Assembly assembly = Assembly.LoadFile(AppDomain.CurrentDomain.BaseDirectory + $"{jobAssembName}.dll");
|
|
Type jobType = assembly.GetType(jobAssembName + "." + className);
|
|
//判断任务调度是否已经开启
|
if (!this._scheduler.Result.IsStarted)
|
{
|
await StartScheduleAsync();
|
}
|
|
//传入反射出来的执行程序集
|
IJobDetail jobDetail = new JobDetailImpl(jobName, jobGroup, jobType);
|
ITrigger trigger = CreateSimpleTrigger(jobName, jobGroup);
|
|
|
// 告诉Quartz使用我们的触发器来安排作业
|
await _scheduler.Result.ScheduleJob(jobDetail, trigger);
|
|
}
|
await Console.Out.WriteLineAsync($"【{jobName}】任务计划添加成功");
|
return content = content.OK(message: $"【{jobName}】添加成功");
|
|
}
|
catch (Exception ex)
|
{
|
Console.Out.WriteLine($"任务计划异常:【{ex.Message}】");
|
//this._scheduler.Dispose();
|
this._scheduler = null;
|
return content = content.Error(message: $"任务计划异常:【{ex.Message}】");
|
}
|
}
|
#endregion
|
#region 检测任务是否存在
|
/// <summary>
|
/// 检测任务是否存在
|
/// </summary>
|
/// <param name="job"></param>
|
/// <returns></returns>
|
public async Task<bool> IsExistScheduleJobAsync(string jobName, string jobGroup)
|
{
|
JobKey jobKey = new JobKey(jobName, jobGroup);
|
if (await _scheduler.Result.CheckExists(jobKey))
|
{
|
return true;
|
}
|
else
|
{
|
return false;
|
}
|
}
|
#endregion
|
|
#region 开启任务调度
|
/// <summary>
|
/// 开启任务调度
|
/// </summary>
|
/// <returns></returns>
|
public async Task<WebResponseContent> StartScheduleAsync()
|
{
|
WebResponseContent content = new WebResponseContent();
|
try
|
{
|
if (!this._scheduler.Result.IsStarted)
|
{
|
await this._scheduler.Result.Start();
|
await Console.Out.WriteLineAsync("任务调度开启!");
|
content = content.OK(message: $"任务调度开启成功");
|
|
//WIDESEA.Helper.GetToken();
|
|
//调度开启成功后,开启Hsl的websocket,端口号为9111
|
if (!ToWCSService.webServer.IsStarted)
|
{
|
ToWCSService.webServer.ServerStart(9111); ;
|
}
|
|
}
|
else
|
{
|
await Console.Out.WriteLineAsync($"任务调度已经开启");
|
content = content.Error($"任务调度已经开启");
|
}
|
}
|
catch (Exception ex)
|
{
|
await Console.Out.WriteLineAsync($"任务调度开启异常:{ex.Message}");
|
content = content.Error($"任务调度开启异常:{ex.Message}");
|
}
|
return content;
|
}
|
#endregion
|
|
private ITrigger CreateSimpleTrigger(string jobName, string jobGroup)
|
{
|
ITrigger trigger = TriggerBuilder.Create()
|
.WithIdentity(jobName, jobGroup)
|
.WithSimpleSchedule(x => x
|
.WithIntervalInSeconds(10)
|
.RepeatForever()
|
)
|
.Build();
|
return trigger;
|
}
|
}
|
}
|