创建项目
This commit is contained in:
22
Service/Application.Web/Plug/Timer/AutoJob.cs
Normal file
22
Service/Application.Web/Plug/Timer/AutoJob.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using Quartz;
|
||||
namespace Application.Web;
|
||||
|
||||
public class AutoJob: ITimerAutoJob
|
||||
{
|
||||
public async Task Execute(IJobExecutionContext context)
|
||||
{
|
||||
string jobId = context.JobDetail.Key.Name;
|
||||
if (jobId == "DayHandle")
|
||||
{
|
||||
|
||||
////处理日红包
|
||||
//await businessService.HandleRedPack();
|
||||
//Console.WriteLine($"{DateTime.Now.ToString("yyyy-MMMM-dd")}:红包奖金池已处理!");
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("定时程序测试执行");
|
||||
}
|
||||
}
|
||||
}
|
||||
68
Service/Application.Web/Plug/Timer/JobStart.cs
Normal file
68
Service/Application.Web/Plug/Timer/JobStart.cs
Normal file
@@ -0,0 +1,68 @@
|
||||
using Quartz;
|
||||
using Quartz.Spi;
|
||||
|
||||
namespace Application.Web;
|
||||
public class JobStart: ITimerAutoStart
|
||||
{
|
||||
private readonly ISchedulerFactory _schedulerFactory;
|
||||
private readonly IJobFactory _jobFactory;
|
||||
|
||||
public JobStart(
|
||||
ISchedulerFactory schedulerFactory,
|
||||
IJobFactory jobFactory)
|
||||
{
|
||||
_schedulerFactory = schedulerFactory;
|
||||
_jobFactory = jobFactory;
|
||||
}
|
||||
|
||||
public IScheduler Scheduler { get; private set; }
|
||||
|
||||
public async Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
Scheduler = await _schedulerFactory.GetScheduler(cancellationToken);
|
||||
Scheduler.JobFactory = _jobFactory;
|
||||
|
||||
// 创建作业
|
||||
var job = JobBuilder.Create<AutoJob>()
|
||||
.WithIdentity("DayHandle", "Handle")
|
||||
.Build();
|
||||
|
||||
// 创建触发器
|
||||
var trigger = TriggerBuilder.Create()
|
||||
.WithIdentity($"DayHandle_trigger", "Handle")
|
||||
.WithCronSchedule("0 0 0 * * ? ")
|
||||
.Build();
|
||||
|
||||
await Scheduler.ScheduleJob(job, trigger, cancellationToken);
|
||||
await Scheduler.Start(cancellationToken);
|
||||
|
||||
Console.WriteLine("----航海时代V3自动程序已启动-----");
|
||||
}
|
||||
|
||||
public async Task Begin()
|
||||
{
|
||||
Scheduler = await _schedulerFactory.GetScheduler();
|
||||
Scheduler.JobFactory = _jobFactory;
|
||||
|
||||
// 创建作业
|
||||
var job = JobBuilder.Create<AutoJob>()
|
||||
.WithIdentity("111", "test")
|
||||
.Build();
|
||||
|
||||
// 创建触发器
|
||||
var trigger = TriggerBuilder.Create()
|
||||
.WithIdentity($"111_trigger", "test")
|
||||
.WithCronSchedule("0/1 * * * * ? ")
|
||||
.Build();
|
||||
|
||||
await Scheduler.ScheduleJob(job, trigger);
|
||||
await Scheduler.Start();
|
||||
|
||||
Console.WriteLine("----探玩驿站自动程序已启动-----");
|
||||
}
|
||||
|
||||
public async Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
await Scheduler?.Shutdown(cancellationToken);
|
||||
}
|
||||
}
|
||||
56
Service/Application.Web/Plug/Timer/TimerJobManager.cs
Normal file
56
Service/Application.Web/Plug/Timer/TimerJobManager.cs
Normal file
@@ -0,0 +1,56 @@
|
||||
using Quartz;
|
||||
|
||||
namespace Application.Web;
|
||||
|
||||
public class TimerJobManager : ITimerJobManager
|
||||
{
|
||||
private readonly IScheduler _scheduler;
|
||||
|
||||
public TimerJobManager(IScheduler scheduler)
|
||||
{
|
||||
_scheduler = scheduler;
|
||||
}
|
||||
|
||||
public async Task CreateAndStartTask(string jobId, string group, string cronExpression)
|
||||
{
|
||||
// 创建作业
|
||||
var job = JobBuilder.Create<AutoJob>()
|
||||
.WithIdentity(jobId, group)
|
||||
.Build();
|
||||
|
||||
// 创建触发器
|
||||
var trigger = TriggerBuilder.Create()
|
||||
.WithIdentity($"{jobId}_trigger", group)
|
||||
.WithCronSchedule(cronExpression)
|
||||
.Build();
|
||||
|
||||
// 安排作业
|
||||
await _scheduler.ScheduleJob(job, trigger);
|
||||
|
||||
// 启动调度器(如果尚未启动)
|
||||
if (!_scheduler.IsStarted)
|
||||
{
|
||||
await _scheduler.Start();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task PauseTask(string jobId)
|
||||
{
|
||||
await _scheduler.PauseJob(new JobKey(jobId, "default"));
|
||||
}
|
||||
|
||||
public async Task ResumeTask(string jobId, string group)
|
||||
{
|
||||
await _scheduler.ResumeJob(new JobKey(jobId, group));
|
||||
}
|
||||
|
||||
public async Task DeleteTask(string jobId, string group)
|
||||
{
|
||||
await _scheduler.DeleteJob(new JobKey(jobId, group));
|
||||
}
|
||||
|
||||
public async Task DeleteTask(string jobId)
|
||||
{
|
||||
await _scheduler.DeleteJob(new JobKey(jobId));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user