optimize CronTabHook

This commit is contained in:
nick.yi 2025-04-25 17:09:59 +08:00
parent 0302ccc9c5
commit 093c681b6d
3 changed files with 10 additions and 22 deletions

View file

@ -1,13 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BotSharp.Core.Crontab.Abstraction
{
public interface ICrontabAuthenticationHook
{
void SetUserIdentity(CrontabItem item);
}
}

View file

@ -1,16 +1,20 @@
namespace BotSharp.Core.Crontab.Abstraction;
public interface ICrontabHook
public abstract class ICrontabHook
{
string[]? Triggers
public string[]? Triggers
=> null;
Task OnCronTriggered(CrontabItem item)
public virtual void OnAuthenticate(CrontabItem item)
{
}
public Task OnCronTriggered(CrontabItem item)
=> Task.CompletedTask;
Task OnTaskExecuting(CrontabItem item)
public Task OnTaskExecuting(CrontabItem item)
=> Task.CompletedTask;
Task OnTaskExecuted(CrontabItem item)
public Task OnTaskExecuted(CrontabItem item)
=> Task.CompletedTask;
}

View file

@ -115,15 +115,12 @@ public class CrontabService : ICrontabService, ITaskFeeder
public async Task ScheduledTimeArrived(CrontabItem item)
{
_logger.LogDebug($"ScheduledTimeArrived {item}");
HookEmitter.Emit<ICrontabAuthenticationHook>(_services, hook =>
{
hook.SetUserIdentity(item);
});
await HookEmitter.Emit<ICrontabHook>(_services, async hook =>
{
if (hook.Triggers == null || hook.Triggers.Contains(item.Title))
{
hook.OnAuthenticate(item);
await hook.OnTaskExecuting(item);
await hook.OnCronTriggered(item);
await hook.OnTaskExecuted(item);