Skip to content

Commit a12262e

Browse files
committed
定时任务使用Quartz
1 parent 2a0ba7d commit a12262e

23 files changed

+237
-138
lines changed

Directory.Packages.props

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
<Project>
22
<ItemGroup>
3-
<PackageVersion Include="Microsoft.AspNetCore.OpenApi" Version="7.0.2" />
4-
<PackageVersion Include="Microsoft.Extensions.Hosting" Version="7.0.0" />
3+
<PackageVersion Include="Microsoft.AspNetCore.OpenApi" Version="7.0.3" />
4+
<PackageVersion Include="Microsoft.Extensions.Hosting" Version="7.0.1" />
55
<PackageVersion Include="Microsoft.Extensions.Hosting.Abstractions" Version="7.0.0" />
66
<PackageVersion Include="Microsoft.Extensions.Http" Version="7.0.0" />
77
<PackageVersion Include="Microsoft.Extensions.Logging" Version="7.0.0" />
88
<PackageVersion Include="MySql.Data" Version="8.0.32" />
99
<PackageVersion Include="NLog" Version="5.1.1" />
1010
<PackageVersion Include="NLog.Extensions.Logging" Version="5.2.1" />
11-
<PackageVersion Include="SqlSugar.IOC" Version="1.9.0" />
12-
<PackageVersion Include="SqlSugarCore" Version="5.1.3.49" />
11+
<PackageVersion Include="Quartz" Version="3.6.0" />
12+
<PackageVersion Include="Quartz.AspNetCore" Version="3.6.0" />
13+
<PackageVersion Include="SqlSugar.IOC" Version="2.0.0" />
14+
<PackageVersion Include="SqlSugarCore" Version="5.1.3.50" />
1315
<PackageVersion Include="Swashbuckle.AspNetCore" Version="6.5.0" />
1416
<PackageVersion Include="Telegram.Bot" Version="19.0.0-preview.2" />
1517
</ItemGroup>

XinjingdailyBot.Command/AdminCommand.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1098,7 +1098,7 @@ public async Task GetMessageDetail(Message message)
10981098
sb.AppendLine($"Chat Type: <code>{msg.Chat.Type}</code>");
10991099
if (message.Chat.Type != ChatType.Private)
11001100
{
1101-
sb.AppendLine($"Chat Title: <code>{msg.Chat.ChatProfile()}</code>");
1101+
sb.AppendLine($"Chat Title: <code>{msg.Chat.FullChatProfile()}</code>");
11021102
}
11031103
sb.AppendLine($"Chat Id: <code>{msg.Chat.Id}</code>");
11041104

XinjingdailyBot.Infrastructure/Attribute/AppServiceAttribute.cs

-5
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,6 @@ public sealed class AppServiceAttribute : System.Attribute
2222
/// </summary>
2323
public bool InterfaceServiceType { get; set; }
2424

25-
[Obsolete]
26-
public AppServiceAttribute()
27-
{
28-
}
29-
3025
public AppServiceAttribute(LifeTime serviceLifetime)
3126
{
3227
ServiceLifetime = serviceLifetime;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
namespace XinjingdailyBot.Infrastructure.Attribute
2+
{
3+
/// <summary>
4+
/// 用于标记定时任务
5+
/// </summary>
6+
[AttributeUsage(AttributeTargets.Class, Inherited = false)]
7+
public sealed class JobAttribute : System.Attribute
8+
{
9+
public string Schedule { get; set; }
10+
public string? Group { get; set; }
11+
12+
public JobAttribute(string schedule)
13+
{
14+
Schedule = schedule;
15+
}
16+
17+
public JobAttribute(string schedule, string group)
18+
{
19+
Schedule = schedule;
20+
Group = group;
21+
}
22+
}
23+
}

XinjingdailyBot.Infrastructure/Extensions/ChatExtension.cs

+10
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,21 @@ public static string ChatID(this Chat chat)
99
return string.IsNullOrEmpty(chat.Username) ? $"#{chat.Id}" : $"@{chat.Username}";
1010
}
1111

12+
public static string FullChatID(this Chat chat)
13+
{
14+
return string.IsNullOrEmpty(chat.Username) ? $"#{chat.Id}" : $"@{chat.Username} #{chat.Id}";
15+
}
16+
1217
public static string ChatProfile(this Chat chat)
1318
{
1419
return $"{chat.Title} {chat.ChatID()}";
1520
}
1621

22+
public static string FullChatProfile(this Chat chat)
23+
{
24+
return $"{chat.Title} {chat.FullChatID()}";
25+
}
26+
1727
/// <summary>
1828
/// HTML转义后的聊天名
1929
/// </summary>

XinjingdailyBot.Interface/Bot/Common/IChannelService.cs

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ namespace XinjingdailyBot.Interface.Bot.Common
55
public interface IChannelService
66
{
77
Chat ReviewGroup { get; }
8+
Chat ReviewLogChannel { get; }
89
Chat CommentGroup { get; }
910
Chat SubGroup { get; }
1011
Chat AcceptChannel { get; }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using Telegram.Bot.Types;
2+
using XinjingdailyBot.Model.Models;
3+
4+
namespace XinjingdailyBot.Interface.Data
5+
{
6+
public interface IDialogueService : IBaseService<Dialogue>
7+
{
8+
Task RecordUpdate(Update update);
9+
}
10+
}

XinjingdailyBot.Model/Models/ChannelOptions.cs

+2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ public sealed record ChannelOptions : BaseModel
2929
/// </summary>
3030
public ChannelOption Option { get; set; } = ChannelOption.Normal;
3131

32+
public int Count { get; set; }
33+
3234
public DateTime CreateAt { get; set; } = DateTime.Now;
3335

3436
public DateTime ModifyAt { get; set; } = DateTime.Now;
+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using SqlSugar;
2+
using Telegram.Bot.Types.Enums;
3+
using XinjingdailyBot.Model.Base;
4+
5+
namespace XinjingdailyBot.Model.Models
6+
{
7+
[SugarTable("dialogue", TableDescription = "消息记录")]
8+
public sealed record Dialogue : BaseModel
9+
{
10+
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
11+
public int Id { get; set; }
12+
/// <summary>
13+
/// 原会话ID
14+
/// </summary>
15+
public long ChatID { get; set; }
16+
/// <summary>
17+
/// 原消息ID
18+
/// </summary>
19+
public long MessageID { get; set; }
20+
21+
/// <summary>
22+
/// 用户ID
23+
/// </summary>
24+
public long UserID { get; set; }
25+
26+
/// <summary>
27+
/// 回复消息ID
28+
/// </summary>
29+
public long ReplyMessageID { get; set; } = -1;
30+
31+
/// <summary>
32+
/// 广告发布位置
33+
/// </summary>
34+
public MessageType Type { get; set; }
35+
36+
/// <summary>
37+
/// 展示权重, 数值越大概率越高, 0为不展示
38+
/// </summary>
39+
public byte Weight { get; set; }
40+
41+
/// <summary>
42+
/// 上次发布时间
43+
/// </summary>
44+
public DateTime LastPostAt { get; set; } = DateTime.MinValue;
45+
46+
/// <summary>
47+
/// 广告展示次数
48+
/// </summary>
49+
public uint ShowCount { get; set; }
50+
/// <summary>
51+
/// 最大展示次数, 当次数值不为0且展示次数大于等于该值时自动禁用
52+
/// </summary>
53+
public uint MaxShowCount { get; set; }
54+
55+
/// <summary>
56+
/// 过期时间, 系统时间大于过期时间自动禁用
57+
/// </summary>
58+
public DateTime ExpireAt { get; set; } = DateTime.MaxValue;
59+
60+
/// <summary>
61+
/// 创建时间
62+
/// </summary>
63+
public DateTime CreateAt { get; set; } = DateTime.Now;
64+
}
65+
}

XinjingdailyBot.Service/Bot/Common/ChannelService.cs

+7-2
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@ public class ChannelService : IChannelService
1818
private readonly ILogger<ChannelService> _logger;
1919

2020
private Chat _reviewGroup = new();
21+
private Chat _reviewLogChannel = new();
2122
private Chat _commentGroup = new();
2223
private Chat _subGroup = new();
2324
private Chat _acceptChannel = new();
2425
private Chat _rejectChannel = new();
2526
private User _botUser = new();
2627

2728
Chat IChannelService.ReviewGroup { get => _reviewGroup; }
29+
Chat IChannelService.ReviewLogChannel { get => _reviewLogChannel; }
2830
Chat IChannelService.CommentGroup { get => _commentGroup; }
2931
Chat IChannelService.SubGroup { get => _subGroup; }
3032
Chat IChannelService.AcceptChannel { get => _acceptChannel; }
@@ -86,7 +88,11 @@ public async Task InitChannelInfo()
8688
catch
8789
{
8890
_logger.LogError("未找到指定的审核群组, 可以使用 /groupinfo 命令获取群组信息");
89-
_reviewGroup = new() { Id = -1 };
91+
}
92+
93+
if (channelOption.UseReviewLogMode)
94+
{
95+
9096
}
9197

9298
try
@@ -104,7 +110,6 @@ public async Task InitChannelInfo()
104110
catch
105111
{
106112
_logger.LogError("未找到指定的评论区群组, 可以使用 /groupinfo 命令获取群组信息");
107-
_commentGroup = new() { Id = -1 };
108113
}
109114

110115
try

XinjingdailyBot.Service/Data/ChannelOptionService.cs

+9-4
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,21 @@ public async Task<ChannelOption> FetchChannelOption(long channelId, string? chan
2626
ChannelName = channelName ?? "",
2727
ChannelTitle = channelTitle ?? "",
2828
Option = ChannelOption.Normal,
29+
Count = 1,
2930
CreateAt = DateTime.Now,
3031
ModifyAt = DateTime.Now,
3132
};
3233
await Insertable(channel).ExecuteCommandAsync();
3334
}
34-
else if (channel.ChannelName != channelName || channel.ChannelTitle != channelTitle)
35+
else
3536
{
36-
channel.ChannelTitle = channelTitle ?? "";
37-
channel.ChannelName = channelName ?? "";
38-
channel.ModifyAt = DateTime.Now;
37+
if (channel.ChannelName != channelName || channel.ChannelTitle != channelTitle)
38+
{
39+
channel.ChannelTitle = channelTitle ?? "";
40+
channel.ChannelName = channelName ?? "";
41+
channel.ModifyAt = DateTime.Now;
42+
}
43+
channel.Count++;
3944
await Updateable(channel).ExecuteCommandAsync();
4045
}
4146

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using Telegram.Bot.Types;
2+
using XinjingdailyBot.Infrastructure.Attribute;
3+
using XinjingdailyBot.Interface.Data;
4+
using XinjingdailyBot.Model.Models;
5+
6+
namespace XinjingdailyBot.Service.Data
7+
{
8+
[AppService(typeof(IDialogueService), LifeTime.Transient)]
9+
public sealed class DialogueService : BaseService<Dialogue>, IDialogueService
10+
{
11+
public async Task RecordUpdate(Update update)
12+
{
13+
14+
}
15+
}
16+
}

XinjingdailyBot.Tasks/ExpiredPostsTask.cs

+4-36
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
using System.Text;
2-
using Microsoft.Extensions.Hosting;
32
using Microsoft.Extensions.Logging;
43
using Microsoft.Extensions.Options;
54
using Telegram.Bot;
65
using Telegram.Bot.Types.Enums;
76
using XinjingdailyBot.Infrastructure;
7+
using XinjingdailyBot.Infrastructure.Attribute;
88
using XinjingdailyBot.Infrastructure.Enums;
99
using XinjingdailyBot.Interface.Data;
1010

@@ -13,7 +13,8 @@ namespace XinjingdailyBot.Tasks
1313
/// <summary>
1414
/// 过期稿件处理
1515
/// </summary>
16-
public class ExpiredPostsTask : IHostedService, IDisposable
16+
[Job("0 0 0 * * ?")]
17+
public class ExpiredPostsTask : IJob
1718
{
1819
private readonly ILogger<ExpiredPostsTask> _logger;
1920
private readonly IPostService _postService;
@@ -34,33 +35,12 @@ public ExpiredPostsTask(
3435
PostExpiredTime = TimeSpan.FromDays(options.Value.Post.PostExpiredTime);
3536
}
3637

37-
/// <summary>
38-
/// 定时器周期
39-
/// </summary>
40-
private readonly TimeSpan CheckInterval = TimeSpan.FromDays(3);
41-
4238
/// <summary>
4339
/// 稿件过期时间
4440
/// </summary>
4541
private TimeSpan PostExpiredTime { get; init; }
4642

47-
/// <summary>
48-
/// 计时器
49-
/// </summary>
50-
private Timer? _timer = null;
51-
52-
public Task StartAsync(CancellationToken cancellationToken)
53-
{
54-
var now = DateTime.Now;
55-
var nextDay = now.AddDays(1).AddHours(-now.Hour).AddMinutes(-now.Minute).AddSeconds(-now.Second);
56-
var tillTomorrow = nextDay - now;
57-
58-
_timer = new Timer(DoWork, null, tillTomorrow, CheckInterval);
59-
60-
return Task.CompletedTask;
61-
}
62-
63-
private async void DoWork(object? _ = null)
43+
public async Task Execute(IJobExecutionContext context)
6444
{
6545
_logger.LogInformation("开始定时任务, 清理过期稿件任务");
6646

@@ -158,17 +138,5 @@ private async void DoWork(object? _ = null)
158138
}
159139
}
160140
}
161-
162-
public Task StopAsync(CancellationToken cancellationToken)
163-
{
164-
_timer?.Change(Timeout.Infinite, 0);
165-
return Task.CompletedTask;
166-
}
167-
168-
public void Dispose()
169-
{
170-
_timer?.Dispose();
171-
GC.SuppressFinalize(this);
172-
}
173141
}
174142
}

0 commit comments

Comments
 (0)