24小时一体机服务
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
3.3 KiB

using Elight.Utility;
using Elight.Utility.logs;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Quartz;
using Quartz.Spi;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;
namespace Elight.Logic.Job
{
public class QuartzHostedService : IHostedService
{
private readonly ISchedulerFactory _schedulerFactory;
private readonly IJobFactory _jobFactory;
private readonly WsJob _myJob;
public QuartzHostedService(ISchedulerFactory schedulerFactory, IJobFactory jobFactory, WsJob myJob, IConfiguration configuration)
{
_schedulerFactory = schedulerFactory;
_jobFactory = jobFactory;
_myJob = myJob;
}
public async Task StartAsync(CancellationToken cancellationToken)
{
try
{
IScheduler scheduler = await _schedulerFactory.GetScheduler(cancellationToken);
scheduler.JobFactory = _jobFactory;
#region 每33秒执行
IJobDetail job3 = JobBuilder.Create<WsJob>()
.WithIdentity("job3", SchedulerConstants.DefaultGroup)
.Build();
var trigger3 = TriggerBuilder.Create()
.WithIdentity("trigger3") // 给任务一个名字
.StartAt(DateTime.Now) // 设置任务开始时间
.ForJob("job3", SchedulerConstants.DefaultGroup) //给任务指定一个分组
.WithCronSchedule("*/20 * * * * ?")
.UsingJobData("k", 321)
.Build();
await scheduler.ScheduleJob(job3, trigger3, cancellationToken);
await scheduler.Start(cancellationToken);
#endregion
#region 每33秒执行
IJobDetail job = JobBuilder.Create<YgJob>()
.WithIdentity("job", SchedulerConstants.DefaultGroup)
.Build();
var trigger = TriggerBuilder.Create()
.WithIdentity("trigger") // 给任务一个名字
.StartAt(DateTime.Now) // 设置任务开始时间
.ForJob("job", SchedulerConstants.DefaultGroup) //给任务指定一个分组
.WithCronSchedule("*/20 * * * * ?")
.UsingJobData("k", 321)
.Build();
await scheduler.ScheduleJob(job, trigger, cancellationToken);
await scheduler.Start(cancellationToken);
}
catch (Exception ex)
{
LogService.WriteLog(ex.Message+"___"+ex.InnerException, "同步程序错误");
}
#endregion
}
public async Task StopAsync(CancellationToken cancellationToken)
{
IScheduler scheduler = await _schedulerFactory.GetScheduler(cancellationToken);
await scheduler.Shutdown(cancellationToken);
}
}
}