feat: enhance event handling and add scheduling capabilities
- Introduced a new scheduler to manage timed tasks within the event dispatcher. - Updated the dispatcher to support the new scheduler, allowing for improved event processing. - Enhanced action serialization in the OneBot11 adapter to convert message chains to the appropriate format. - Added new dependencies for cron scheduling and other indirect packages in go.mod and go.sum. - Improved logging for event publishing and handler matching, providing better insights during execution. - Refactored plugin loading to include scheduled job management.
This commit is contained in:
@@ -7,7 +7,8 @@ import (
|
||||
"cellbot/internal/adapter/onebot11"
|
||||
"cellbot/internal/config"
|
||||
"cellbot/internal/engine"
|
||||
_ "cellbot/internal/plugins/echo" // 导入插件以触发 init 函数
|
||||
_ "cellbot/internal/plugins/echo" // 导入插件以触发 init 函数
|
||||
_ "cellbot/internal/plugins/welcome" // 导入插件以触发 init 函数
|
||||
"cellbot/internal/protocol"
|
||||
"cellbot/pkg/net"
|
||||
|
||||
@@ -42,8 +43,12 @@ func ProvideEventBus(logger *zap.Logger) *engine.EventBus {
|
||||
return engine.NewEventBus(logger, 10000)
|
||||
}
|
||||
|
||||
func ProvideDispatcher(eventBus *engine.EventBus, logger *zap.Logger, cfg *config.Config) *engine.Dispatcher {
|
||||
dispatcher := engine.NewDispatcher(eventBus, logger)
|
||||
func ProvideScheduler(logger *zap.Logger) *engine.Scheduler {
|
||||
return engine.NewScheduler(logger)
|
||||
}
|
||||
|
||||
func ProvideDispatcher(eventBus *engine.EventBus, logger *zap.Logger, cfg *config.Config, scheduler *engine.Scheduler) *engine.Dispatcher {
|
||||
dispatcher := engine.NewDispatcherWithScheduler(eventBus, logger, scheduler)
|
||||
|
||||
// 注册限流中间件
|
||||
if cfg.Engine.RateLimit.Enabled {
|
||||
@@ -97,7 +102,18 @@ func ProvideMilkyBots(cfg *config.Config, logger *zap.Logger, eventBus *engine.E
|
||||
lc.Append(fx.Hook{
|
||||
OnStart: func(ctx context.Context) error {
|
||||
logger.Info("Starting Milky bot", zap.String("bot_id", botCfg.ID))
|
||||
return bot.Connect(ctx)
|
||||
// 在后台启动连接,失败时只记录错误,不终止应用
|
||||
go func() {
|
||||
if err := bot.Connect(context.Background()); err != nil {
|
||||
logger.Error("Failed to connect Milky bot, will retry in background",
|
||||
zap.String("bot_id", botCfg.ID),
|
||||
zap.Error(err))
|
||||
// 可以在这里实现重试逻辑
|
||||
} else {
|
||||
logger.Info("Milky bot connected successfully", zap.String("bot_id", botCfg.ID))
|
||||
}
|
||||
}()
|
||||
return nil
|
||||
},
|
||||
OnStop: func(ctx context.Context) error {
|
||||
logger.Info("Stopping Milky bot", zap.String("bot_id", botCfg.ID))
|
||||
@@ -137,7 +153,18 @@ func ProvideOneBot11Bots(cfg *config.Config, logger *zap.Logger, wsManager *net.
|
||||
lc.Append(fx.Hook{
|
||||
OnStart: func(ctx context.Context) error {
|
||||
logger.Info("Starting OneBot11 bot", zap.String("bot_id", botCfg.ID))
|
||||
return bot.Connect(ctx)
|
||||
// 在后台启动连接,失败时只记录错误,不终止应用
|
||||
go func() {
|
||||
if err := bot.Connect(context.Background()); err != nil {
|
||||
logger.Error("Failed to connect OneBot11 bot, will retry in background",
|
||||
zap.String("bot_id", botCfg.ID),
|
||||
zap.Error(err))
|
||||
// 可以在这里实现重试逻辑
|
||||
} else {
|
||||
logger.Info("OneBot11 bot connected successfully", zap.String("bot_id", botCfg.ID))
|
||||
}
|
||||
}()
|
||||
return nil
|
||||
},
|
||||
OnStop: func(ctx context.Context) error {
|
||||
logger.Info("Stopping OneBot11 bot", zap.String("bot_id", botCfg.ID))
|
||||
@@ -171,12 +198,18 @@ func LoadPlugins(logger *zap.Logger, botManager *protocol.BotManager, registry *
|
||||
zap.Strings("plugins", engine.GetRegisteredPlugins()))
|
||||
}
|
||||
|
||||
// LoadScheduledJobs 加载所有定时任务(由依赖注入系统调用)
|
||||
func LoadScheduledJobs(scheduler *engine.Scheduler, logger *zap.Logger) error {
|
||||
return engine.LoadAllJobs(scheduler, logger)
|
||||
}
|
||||
|
||||
var Providers = fx.Options(
|
||||
fx.Provide(
|
||||
ProvideConfig,
|
||||
ProvideConfigManager,
|
||||
ProvideLogger,
|
||||
ProvideEventBus,
|
||||
ProvideScheduler,
|
||||
ProvideDispatcher,
|
||||
ProvidePluginRegistry,
|
||||
ProvideBotManager,
|
||||
@@ -186,4 +219,5 @@ var Providers = fx.Options(
|
||||
fx.Invoke(ProvideMilkyBots),
|
||||
fx.Invoke(ProvideOneBot11Bots),
|
||||
fx.Invoke(LoadPlugins),
|
||||
fx.Invoke(LoadScheduledJobs),
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user