Skip to content

Commit

Permalink
支持插件激活与去激活行为通知
Browse files Browse the repository at this point in the history
  • Loading branch information
pangdogs committed Sep 28, 2024
1 parent 3eb753b commit 508de18
Show file tree
Hide file tree
Showing 12 changed files with 66 additions and 39 deletions.
6 changes: 3 additions & 3 deletions runtime/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ type iContext interface {
setFrame(frame Frame)
setCallee(callee async.Callee)
getServiceCtx() service.Context
changeRunningState(state RunningState)
changeRunningState(state RunningState, args ...any)
gc()
}

Expand Down Expand Up @@ -213,9 +213,9 @@ func (ctx *ContextBehavior) getServiceCtx() service.Context {
return ctx.svcCtx
}

func (ctx *ContextBehavior) changeRunningState(state RunningState) {
func (ctx *ContextBehavior) changeRunningState(state RunningState, args ...any) {
ctx.entityMgr.changeRunningState(state)
ctx.opts.RunningHandler.Call(ctx.GetAutoRecover(), ctx.GetReportError(), nil, ctx.opts.InstanceFace.Iface, state)
ctx.opts.RunningHandler.Call(ctx.GetAutoRecover(), ctx.GetReportError(), nil, ctx.opts.InstanceFace.Iface, state, args...)

switch state {
case RunningState_Terminated:
Expand Down
3 changes: 1 addition & 2 deletions runtime/context_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
)

type (
RunningHandler = generic.DelegateAction2[Context, RunningState] // 运行状态变化处理器
RunningHandler = generic.DelegateActionVar2[Context, RunningState, any] // 运行状态变化处理器
)

// ContextOptions 创建运行时上下文的所有选项
Expand All @@ -42,7 +42,6 @@ type ContextOptions struct {
PersistId uid.Id // 运行时持久化Id
PluginBundle plugin.PluginBundle // 插件包
RunningHandler RunningHandler // 运行状态变化处理器
Plugin generic.Action1[*plugin.PluginStatus]
}

type _ContextOption struct{}
Expand Down
30 changes: 17 additions & 13 deletions runtime/runningstate.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,21 @@ package runtime
type RunningState int32

const (
RunningState_Birth RunningState = iota // 出生
RunningState_Starting // 开始启动
RunningState_Started // 已启动
RunningState_FrameLoopBegin // 帧循环开始
RunningState_FrameUpdateBegin // 帧更新开始
RunningState_FrameUpdateEnd // 帧更新结束
RunningState_FrameLoopEnd // 帧循环结束
RunningState_RunCallBegin // Call开始执行
RunningState_RunCallEnd // Call结束执行
RunningState_RunGCBegin // GC开始执行
RunningState_RunGCEnd // GC结束执行
RunningState_Terminating // 开始停止
RunningState_Terminated // 已停止
RunningState_Birth RunningState = iota // 出生
RunningState_Starting // 开始启动
RunningState_Started // 已启动
RunningState_FrameLoopBegin // 帧循环开始
RunningState_FrameUpdateBegin // 帧更新开始
RunningState_FrameUpdateEnd // 帧更新结束
RunningState_FrameLoopEnd // 帧循环结束
RunningState_RunCallBegin // Call开始执行
RunningState_RunCallEnd // Call结束执行
RunningState_RunGCBegin // GC开始执行
RunningState_RunGCEnd // GC结束执行
RunningState_Terminating // 开始停止
RunningState_Terminated // 已停止
RunningState_PluginActivating // 开始激活插件
RunningState_PluginActivated // 插件已激活
RunningState_PluginDeactivating // 开始去激活插件
RunningState_PluginDeactivated // 插件已去激活
)
8 changes: 6 additions & 2 deletions runtime/runningstate_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions runtime/unsafe_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ func (uc _UnsafeContext) GetServiceCtx() service.Context {
}

// ChangeRunningState 修改运行状态
func (uc _UnsafeContext) ChangeRunningState(state RunningState) {
uc.changeRunningState(state)
func (uc _UnsafeContext) ChangeRunningState(state RunningState, args ...any) {
uc.changeRunningState(state, args...)
}

// GC GC
Expand Down
10 changes: 8 additions & 2 deletions runtime_running.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (rt *RuntimeBehavior) running() {
close(ictx.UnsafeContext(ctx).GetTerminatedChan())
}

func (rt *RuntimeBehavior) changeRunningState(state runtime.RunningState) {
func (rt *RuntimeBehavior) changeRunningState(state runtime.RunningState, args ...any) {
switch state {
case runtime.RunningState_Starting:
rt.initPlugin()
Expand All @@ -101,7 +101,7 @@ func (rt *RuntimeBehavior) changeRunningState(state runtime.RunningState) {
rt.shutPlugin()
}

runtime.UnsafeContext(rt.ctx).ChangeRunningState(state)
runtime.UnsafeContext(rt.ctx).ChangeRunningState(state, args...)
}

func (rt *RuntimeBehavior) initPlugin() {
Expand Down Expand Up @@ -139,6 +139,9 @@ func (rt *RuntimeBehavior) activatePlugin(pluginStatus plugin.PluginStatus) {
return
}

rt.changeRunningState(runtime.RunningState_PluginActivating, pluginStatus)
defer rt.changeRunningState(runtime.RunningState_PluginActivated, pluginStatus)

if pluginInit, ok := pluginStatus.InstanceFace().Iface.(LifecycleRuntimePluginInit); ok {
generic.MakeAction1(pluginInit.InitRP).Call(rt.ctx.GetAutoRecover(), rt.ctx.GetReportError(), rt.ctx)
}
Expand All @@ -147,6 +150,9 @@ func (rt *RuntimeBehavior) activatePlugin(pluginStatus plugin.PluginStatus) {
}

func (rt *RuntimeBehavior) deactivatePlugin(pluginStatus plugin.PluginStatus) {
rt.changeRunningState(runtime.RunningState_PluginDeactivating, pluginStatus)
defer rt.changeRunningState(runtime.RunningState_PluginDeactivated, pluginStatus)

if !plugin.UnsafePluginStatus(pluginStatus).SetState(plugin.PluginState_Inactive, plugin.PluginState_Active) {
return
}
Expand Down
6 changes: 3 additions & 3 deletions service/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ type Context interface {
type iContext interface {
init(opts ContextOptions)
getOptions() *ContextOptions
changeRunningState(state RunningState)
changeRunningState(state RunningState, args ...any)
}

// ContextBehavior 服务上下文行为,在扩展服务上下文能力时,匿名嵌入至服务上下文结构体中
Expand Down Expand Up @@ -138,6 +138,6 @@ func (ctx *ContextBehavior) getOptions() *ContextOptions {
return &ctx.opts
}

func (ctx *ContextBehavior) changeRunningState(state RunningState) {
ctx.opts.RunningHandler.Call(ctx.GetAutoRecover(), ctx.GetReportError(), nil, ctx.opts.InstanceFace.Iface, state)
func (ctx *ContextBehavior) changeRunningState(state RunningState, args ...any) {
ctx.opts.RunningHandler.Call(ctx.GetAutoRecover(), ctx.GetReportError(), nil, ctx.opts.InstanceFace.Iface, state, args...)
}
2 changes: 1 addition & 1 deletion service/context_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
)

type (
RunningHandler = generic.DelegateAction2[Context, RunningState] // 运行状态变化处理器
RunningHandler = generic.DelegateActionVar2[Context, RunningState, any] // 运行状态变化处理器
)

// ContextOptions 创建服务上下文的所有选项
Expand Down
14 changes: 9 additions & 5 deletions service/runningstate.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,13 @@ package service
type RunningState int32

const (
RunningState_Birth RunningState = iota // 出生
RunningState_Starting // 开始启动
RunningState_Started // 已启动
RunningState_Terminating // 开始停止
RunningState_Terminated // 已停止
RunningState_Birth RunningState = iota // 出生
RunningState_Starting // 开始启动
RunningState_Started // 已启动
RunningState_Terminating // 开始停止
RunningState_Terminated // 已停止
RunningState_PluginActivating // 开始激活插件
RunningState_PluginActivated // 插件已激活
RunningState_PluginDeactivating // 开始去激活插件
RunningState_PluginDeactivated // 插件已去激活
)
8 changes: 6 additions & 2 deletions service/runningstate_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions service/unsafe_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ func (uc _UnsafeContext) GetOptions() *ContextOptions {
}

// ChangeRunningState 修改运行状态
func (uc _UnsafeContext) ChangeRunningState(state RunningState) {
uc.changeRunningState(state)
func (uc _UnsafeContext) ChangeRunningState(state RunningState, args ...any) {
uc.changeRunningState(state, args...)
}
10 changes: 8 additions & 2 deletions service_running.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,15 @@ loop:
close(ictx.UnsafeContext(ctx).GetTerminatedChan())
}

func (svc *ServiceBehavior) changeRunningState(state service.RunningState) {
func (svc *ServiceBehavior) changeRunningState(state service.RunningState, args ...any) {
switch state {
case service.RunningState_Starting:
svc.initPlugin()
case service.RunningState_Terminated:
svc.shutPlugin()
}

service.UnsafeContext(svc.ctx).ChangeRunningState(state)
service.UnsafeContext(svc.ctx).ChangeRunningState(state, args...)
}

func (svc *ServiceBehavior) initPlugin() {
Expand Down Expand Up @@ -135,6 +135,9 @@ func (svc *ServiceBehavior) activatePlugin(pluginStatus plugin.PluginStatus) {
return
}

svc.changeRunningState(service.RunningState_PluginActivating, pluginStatus)
defer svc.changeRunningState(service.RunningState_PluginActivated, pluginStatus)

if pluginInit, ok := pluginStatus.InstanceFace().Iface.(LifecycleServicePluginInit); ok {
generic.MakeAction1(pluginInit.InitSP).Call(svc.ctx.GetAutoRecover(), svc.ctx.GetReportError(), svc.ctx)
}
Expand All @@ -143,6 +146,9 @@ func (svc *ServiceBehavior) activatePlugin(pluginStatus plugin.PluginStatus) {
}

func (svc *ServiceBehavior) deactivatePlugin(pluginStatus plugin.PluginStatus) {
svc.changeRunningState(service.RunningState_PluginDeactivating, pluginStatus)
defer svc.changeRunningState(service.RunningState_PluginDeactivated, pluginStatus)

if !plugin.UnsafePluginStatus(pluginStatus).SetState(plugin.PluginState_Inactive, plugin.PluginState_Active) {
return
}
Expand Down

0 comments on commit 508de18

Please sign in to comment.