Skip to content

Commit e38c196

Browse files
committed
优化了定时任务
1 parent 03bd6fe commit e38c196

File tree

4 files changed

+281
-217
lines changed

4 files changed

+281
-217
lines changed

Sourcecode/Song.WebSite/Global.asax.cs

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
using Song.Entities;
1111
using System.Data;
1212
using WeiSha.Core;
13+
using System.Threading.Tasks;
14+
using Quartz;
15+
using Quartz.Impl;
1316

1417
namespace Song.WebSite
1518
{
@@ -46,11 +49,61 @@ protected void Application_Start()
4649
{
4750
Log.Error(this.GetType().ToString(), ex);
4851
}
49-
//更新统计数据,延迟执行
50-
WeiSha.Core.Business.Do<IOrganization>().UpdateStatisticalData_Delay(10);
51-
//创建定时任务
52-
WeiSha.Core.Business.Do<IOrganization>().UpdateStatisticalData_CronJob();
53-
52+
////更新统计数据,延迟执行
53+
//WeiSha.Core.Business.Do<IOrganization>().UpdateStatisticalData_Delay(10);
54+
////创建定时任务
55+
//WeiSha.Core.Business.Do<IOrganization>().UpdateStatisticalData_CronJob();
56+
57+
//执行定时任务
58+
// 启动 Quartz 调度器
59+
StartScheduler().GetAwaiter().GetResult();
60+
61+
}
62+
// 定时任务的调度器
63+
private static IScheduler _scheduler;
64+
private async Task StartScheduler()
65+
{
66+
// 创建调度器
67+
_scheduler = await StdSchedulerFactory.GetDefaultScheduler();
68+
69+
70+
// 定义作业
71+
IJobDetail job = JobBuilder.Create<ScheduledTaskJob>()
72+
.WithIdentity("ScheduledTaskJob", "group1")
73+
.Build();
74+
75+
//定时的时间,CRON 表达式
76+
string cron = WeiSha.Core.App.Get["QueryDetail_Cron"].String;
77+
// 创建触发器,设置指定时间执行
78+
ITrigger trigger = TriggerBuilder.Create()
79+
.WithIdentity("myTrigger", "group1")
80+
.StartNow()
81+
.WithCronSchedule(cron) // CRON 表达式
82+
.Build();
83+
84+
// 调度作业
85+
await _scheduler.ScheduleJob(job, trigger);
86+
87+
await _scheduler.Start();
88+
}
89+
protected void Application_End(object sender, EventArgs e)
90+
{
91+
// 停止并释放 Quartz 调度器
92+
_scheduler?.Shutdown(waitForJobsToComplete: true);
93+
}
94+
95+
}
96+
/// <summary>
97+
/// 定时任务的执行类
98+
/// </summary>
99+
public class ScheduledTaskJob : IJob
100+
{
101+
public async Task Execute(IJobExecutionContext context)
102+
{
103+
//执行方法
104+
//更新统计数据
105+
WeiSha.Core.Business.Do<IOrganization>().UpdateStatisticalData();
106+
await Task.CompletedTask;
54107
}
55108
}
56109
}

Sourcecode/Song.WebSite/Song.WebSite.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,18 @@
7575
<HintPath>..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.2.0.1\lib\net45\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.dll</HintPath>
7676
</Reference>
7777
<Reference Include="Microsoft.CSharp" />
78+
<Reference Include="Microsoft.Extensions.Logging.Abstractions, Version=2.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60, processorArchitecture=MSIL">
79+
<HintPath>..\packages\Microsoft.Extensions.Logging.Abstractions.2.1.1\lib\netstandard2.0\Microsoft.Extensions.Logging.Abstractions.dll</HintPath>
80+
</Reference>
7881
<Reference Include="Microsoft.Web.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
7982
<HintPath>..\packages\Microsoft.Web.Infrastructure.1.0.0.0\lib\net40\Microsoft.Web.Infrastructure.dll</HintPath>
8083
</Reference>
8184
<Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
8285
<HintPath>..\packages\Newtonsoft.Json.12.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
8386
</Reference>
87+
<Reference Include="Quartz, Version=3.13.1.0, Culture=neutral, PublicKeyToken=f6b8c98a402cc8a4, processorArchitecture=MSIL">
88+
<HintPath>..\packages\Quartz.3.13.1\lib\net462\Quartz.dll</HintPath>
89+
</Reference>
8490
<Reference Include="System" />
8591
<Reference Include="System.Buffers, Version=4.0.2.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
8692
<HintPath>..\packages\System.Buffers.4.4.0\lib\netstandard2.0\System.Buffers.dll</HintPath>
@@ -124,6 +130,7 @@
124130
<Reference Include="System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
125131
<HintPath>..\packages\System.Runtime.CompilerServices.Unsafe.4.5.2\lib\netstandard2.0\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
126132
</Reference>
133+
<Reference Include="System.Runtime.Remoting" />
127134
<Reference Include="System.Security" />
128135
<Reference Include="System.Security.Cryptography.Algorithms, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
129136
<HintPath>..\packages\System.Security.Cryptography.Algorithms.4.3.0\lib\net461\System.Security.Cryptography.Algorithms.dll</HintPath>

0 commit comments

Comments
 (0)