diff --git a/Api/Utility/TaskScheduler.cs b/Api/Utility/TaskScheduler.cs
index 9f3b832..ad695aa 100644
--- a/Api/Utility/TaskScheduler.cs
+++ b/Api/Utility/TaskScheduler.cs
@@ -1,4 +1,5 @@
-using Milimoe.FunGame.Core.Library.Constant;
+using Milimoe.FunGame.Core.Interface.Base;
+using Milimoe.FunGame.Core.Library.Constant;
using Milimoe.FunGame.Core.Model;
namespace Milimoe.FunGame.Core.Api.Utility
@@ -80,78 +81,75 @@ namespace Milimoe.FunGame.Core.Api.Utility
}
///
- /// 获取任务计划上一次执行时间
+ /// 获取任务计划
///
///
///
///
- public DateTime GetLastTime(string name, bool recurring = false)
- {
- if (!recurring)
- {
- if (_tasks.FirstOrDefault(t => t.Name == name) is ScheduledTask task && task.LastRun.HasValue)
- {
- return task.LastRun.Value;
- }
- else if (_recurringTasks.FirstOrDefault(t => t.Name == name) is RecurringTask recurringTask && recurringTask.LastRun.HasValue)
- {
- return recurringTask.LastRun.Value;
- }
- }
- else if (_recurringTasks.FirstOrDefault(t => t.Name == name) is RecurringTask recurringTask && recurringTask.LastRun.HasValue)
- {
- return recurringTask.LastRun.Value;
- }
- return DateTime.MinValue;
- }
-
- ///
- /// 获取任务计划下一次执行时间
- ///
- ///
- ///
- ///
- public DateTime GetNextTime(string name, bool recurring = false)
+ public IScheduledTask? GetTask(string name, bool recurring = false)
{
if (!recurring)
{
if (_tasks.FirstOrDefault(t => t.Name == name) is ScheduledTask task)
{
- DateTime today = DateTime.Today.Add(task.TimeOfDay);
- return task.IsTodayRun ? today.AddDays(1) : today;
+ return task;
}
else if (_recurringTasks.FirstOrDefault(t => t.Name == name) is RecurringTask recurringTask)
{
- return recurringTask.NextRun;
+ return recurringTask;
}
}
else if (_recurringTasks.FirstOrDefault(t => t.Name == name) is RecurringTask recurringTask)
{
- return recurringTask.NextRun;
+ return recurringTask;
}
- return DateTime.MaxValue;
+ return null;
}
+ ///
+ /// 获取任务计划上一次执行时间
+ ///
+ ///
+ ///
+ public static DateTime GetLastTime(IScheduledTask task) => task.LastRun.HasValue ? task.LastRun.Value : DateTime.MinValue;
+
+ ///
+ /// 获取任务计划下一次执行时间
+ ///
+ ///
+ ///
+ public static DateTime GetNextTime(IScheduledTask task) => task.NextRun;
+
+ ///
+ /// 格式化任务计划执行信息
+ ///
+ ///
+ ///
public string GetRunTimeInfo(string name)
{
- DateTime last = GetLastTime(name);
- DateTime next = GetNextTime(name);
- string msg = "";
- if (last != DateTime.MinValue)
+ string msg;
+ IScheduledTask? task = GetTask(name);
+ if (task is null)
{
- msg += $"上次运行时间:{last.ToString(General.GeneralDateTimeFormat)}\r\n";
- }
- if (next != DateTime.MaxValue)
- {
- msg += $"下次运行时间:{next.ToString(General.GeneralDateTimeFormat)}\r\n";
- }
- if (msg != "")
- {
- msg = $"任务计划:{name}\r\n{msg}";
+ msg = $"任务计划 {name} 不存在!";
}
else
{
- msg = $"任务计划 {name} 不存在!";
+ msg = $"任务计划:{name}\r\n";
+ DateTime last = GetLastTime(task);
+ DateTime next = GetNextTime(task);
+ if (last != DateTime.MinValue)
+ {
+ msg += $"上次执行时间:{last.ToString(General.GeneralDateTimeFormat)}\r\n";
+ }
+ if (next != DateTime.MaxValue)
+ {
+ msg += $"下次执行时间:{next.ToString(General.GeneralDateTimeFormat)}\r\n";
+ }
+ if (task.ExecutedTimeSpan.TotalSeconds > 0)
+ {
+ msg += $"任务执行时长:{task.ExecutedTimeSpan.TotalSeconds:0.###} 秒\r\n";
+ }
}
return msg.Trim();
}
@@ -168,6 +166,7 @@ namespace Milimoe.FunGame.Core.Api.Utility
{
if (!task.IsTodayRun)
{
+ now = DateTime.Now;
if (now.TimeOfDay >= task.TimeOfDay && now.TimeOfDay < task.TimeOfDay.Add(TimeSpan.FromSeconds(10)))
{
task.LastRun = now;
@@ -176,6 +175,7 @@ namespace Milimoe.FunGame.Core.Api.Utility
try
{
task.Action();
+ task.ExecutedTimeSpan = DateTime.Now - now;
}
catch (Exception ex)
{
@@ -190,6 +190,7 @@ namespace Milimoe.FunGame.Core.Api.Utility
foreach (RecurringTask recurringTask in _recurringTasks)
{
+ now = DateTime.Now;
if (now >= recurringTask.NextRun)
{
recurringTask.LastRun = now;
@@ -199,6 +200,7 @@ namespace Milimoe.FunGame.Core.Api.Utility
try
{
recurringTask.Action();
+ recurringTask.ExecutedTimeSpan = DateTime.Now - now;
}
catch (Exception ex)
{
diff --git a/Entity/Explore/Activity.cs b/Entity/Explore/Activity.cs
index 25496e9..000a6b9 100644
--- a/Entity/Explore/Activity.cs
+++ b/Entity/Explore/Activity.cs
@@ -130,7 +130,7 @@ namespace Milimoe.FunGame.Core.Entity
return false;
}
- public bool RegisterAwardedUser(long userId, long questId) => Quests.FirstOrDefault(q => q.Id == questId && q.Status == QuestState.Completed) is Quest quest && RegisterAwardedUser(userId, quest);
+ public bool RegisterAwardedUser(long userId, long questId) => Quests.FirstOrDefault(q => q.Id == questId) is Quest quest && RegisterAwardedUser(userId, quest);
public bool HasUserAwarded(long userId, Quest quest)
{
diff --git a/Interface/Base/IScheduledTask.cs b/Interface/Base/IScheduledTask.cs
new file mode 100644
index 0000000..1b30263
--- /dev/null
+++ b/Interface/Base/IScheduledTask.cs
@@ -0,0 +1,40 @@
+namespace Milimoe.FunGame.Core.Interface.Base
+{
+ public interface IScheduledTask
+ {
+ ///
+ /// 任务名称
+ ///
+ public string Name { get; }
+
+ ///
+ /// 任务执行逻辑
+ ///
+ public Action Action { get; }
+
+ ///
+ /// 记录上一次执行时间
+ ///
+ public DateTime? LastRun { get; }
+
+ ///
+ /// 记录下一次执行时间
+ ///
+ public DateTime NextRun { get; }
+
+ ///
+ /// 任务执行时长
+ ///
+ public TimeSpan ExecutedTimeSpan { get; }
+
+ ///
+ /// 最后一次执行时发生的错误
+ ///
+ public Exception? Error { get; }
+
+ ///
+ /// 捕获异常后,触发的回调函数
+ ///
+ public Action? ErrorHandler { get; }
+ }
+}
diff --git a/Model/RecurringTask.cs b/Model/RecurringTask.cs
index de08ff8..5f71c03 100644
--- a/Model/RecurringTask.cs
+++ b/Model/RecurringTask.cs
@@ -1,6 +1,8 @@
-namespace Milimoe.FunGame.Core.Model
+using Milimoe.FunGame.Core.Interface.Base;
+
+namespace Milimoe.FunGame.Core.Model
{
- public class RecurringTask(string name, TimeSpan interval, Action action, Action? error = null)
+ public class RecurringTask(string name, TimeSpan interval, Action action, Action? error = null) : IScheduledTask
{
///
/// 任务名称
@@ -18,17 +20,22 @@
public Action Action { get; set; } = action;
///
- /// 记录上一次运行时间
+ /// 记录上一次执行时间
///
public DateTime? LastRun { get; set; } = null;
///
- /// 记录下一次运行时间
+ /// 记录下一次执行时间
///
public DateTime NextRun { get; set; } = DateTime.MaxValue;
///
- /// 最后一次运行时发生的错误
+ /// 任务执行时长
+ ///
+ public TimeSpan ExecutedTimeSpan { get; set; } = new();
+
+ ///
+ /// 最后一次执行时发生的错误
///
public Exception? Error { get; set; }
diff --git a/Model/ScheduledTask.cs b/Model/ScheduledTask.cs
index 0aea74c..c0280f0 100644
--- a/Model/ScheduledTask.cs
+++ b/Model/ScheduledTask.cs
@@ -1,6 +1,8 @@
-namespace Milimoe.FunGame.Core.Model
+using Milimoe.FunGame.Core.Interface.Base;
+
+namespace Milimoe.FunGame.Core.Model
{
- public class ScheduledTask(string name, TimeSpan timeSpan, Action action, Action? error = null)
+ public class ScheduledTask(string name, TimeSpan timeSpan, Action action, Action? error = null) : IScheduledTask
{
///
/// 任务名称
@@ -18,17 +20,27 @@
public Action Action { get; set; } = action;
///
- /// 记录上一次运行时间
+ /// 记录上一次执行时间
///
public DateTime? LastRun { get; set; } = null;
///
- /// 当天是否已经运行
+ /// 记录下一次执行时间
+ ///
+ public DateTime NextRun => IsTodayRun ? DateTime.Today.AddDays(1).Add(TimeOfDay) : DateTime.Today.Add(TimeOfDay);
+
+ ///
+ /// 当天是否已经执行
///
public bool IsTodayRun => LastRun.HasValue && LastRun.Value.Date == DateTime.Today;
///
- /// 最后一次运行时发生的错误
+ /// 任务执行时长
+ ///
+ public TimeSpan ExecutedTimeSpan { get; set; } = new();
+
+ ///
+ /// 最后一次执行时发生的错误
///
public Exception? Error { get; set; }