From b68c8aa8d0439c0d744f38facd2623502a8d92e5 Mon Sep 17 00:00:00 2001 From: siyul-park Date: Fri, 20 Dec 2024 09:59:14 +0900 Subject: [PATCH] refactor: expose load and unload listener --- pkg/runtime/runtime.go | 4 ++-- pkg/symbol/cluster.go | 22 ++----------------- pkg/symbol/cluster_test.go | 40 --------------------------------- pkg/symbol/loadhook.go | 44 ++++++++++++++++++++++++++----------- pkg/symbol/unloadhook.go | 45 ++++++++++++++++++++++++++------------ 5 files changed, 66 insertions(+), 89 deletions(-) diff --git a/pkg/runtime/runtime.go b/pkg/runtime/runtime.go index 8ccf2ac4..446bb274 100644 --- a/pkg/runtime/runtime.go +++ b/pkg/runtime/runtime.go @@ -65,8 +65,8 @@ func New(config Config) *Runtime { config.ChartStore = chart.NewStore() } - config.Hook.AddLoadHook(symbol.NewClusterLoadHook(config.Hook)) - config.Hook.AddUnloadHook(symbol.NewClusterUnloadHook(config.Hook)) + config.Hook.AddLoadHook(symbol.LoadListenerHook(config.Hook)) + config.Hook.AddUnloadHook(symbol.UnloadListenerHook(config.Hook)) symbolTable := symbol.NewTable(symbol.TableOption{ LoadHooks: []symbol.LoadHook{config.Hook}, diff --git a/pkg/symbol/cluster.go b/pkg/symbol/cluster.go index dbc84b1e..6b355a30 100644 --- a/pkg/symbol/cluster.go +++ b/pkg/symbol/cluster.go @@ -20,26 +20,8 @@ type Cluster struct { } var _ node.Node = (*Cluster)(nil) - -// NewClusterLoadHook creates a LoadHook for Cluster nodes. -func NewClusterLoadHook(hook LoadHook) LoadHook { - return LoadFunc(func(sb *Symbol) error { - if cluster, ok := node.Unwrap(sb).(*Cluster); ok { - return cluster.Load(hook) - } - return nil - }) -} - -// NewClusterUnloadHook creates an UnloadHook for Cluster nodes. -func NewClusterUnloadHook(hook UnloadHook) UnloadHook { - return UnloadFunc(func(sb *Symbol) error { - if cluster, ok := node.Unwrap(sb).(*Cluster); ok { - return cluster.Unload(hook) - } - return nil - }) -} +var _ LoadListener = (*Cluster)(nil) +var _ UnloadListener = (*Cluster)(nil) // NewCluster creates a new Cluster with the provided symbol table. func NewCluster(symbols []*Symbol) *Cluster { diff --git a/pkg/symbol/cluster_test.go b/pkg/symbol/cluster_test.go index 4c807a64..750ba9b1 100644 --- a/pkg/symbol/cluster_test.go +++ b/pkg/symbol/cluster_test.go @@ -15,46 +15,6 @@ import ( "time" ) -func TestNewClusterLoadHook(t *testing.T) { - h := NewClusterLoadHook(LoadFunc(func(_ *Symbol) error { - return nil - })) - - n := NewCluster(nil) - defer n.Close() - - sb := &Symbol{ - Spec: &spec.Meta{ - ID: uuid.Must(uuid.NewV7()), - Kind: faker.Word(), - }, - Node: n, - } - - err := h.Load(sb) - assert.NoError(t, err) -} - -func TestNewClusterUnloadHook(t *testing.T) { - h := NewClusterUnloadHook(UnloadFunc(func(_ *Symbol) error { - return nil - })) - - n := NewCluster(nil) - defer n.Close() - - sb := &Symbol{ - Spec: &spec.Meta{ - ID: uuid.Must(uuid.NewV7()), - Kind: faker.Word(), - }, - Node: n, - } - - err := h.Unload(sb) - assert.NoError(t, err) -} - func TestNewCluster(t *testing.T) { n := NewCluster(nil) assert.NotNil(t, n) diff --git a/pkg/symbol/loadhook.go b/pkg/symbol/loadhook.go index 615f90b4..9baf7e3e 100644 --- a/pkg/symbol/loadhook.go +++ b/pkg/symbol/loadhook.go @@ -1,35 +1,53 @@ package symbol -// LoadHook defines an interface for handling events when a symbol is loaded. +import "github.com/siyul-park/uniflow/pkg/node" + +// LoadHook handles symbol load events. type LoadHook interface { - // Load is called to handle the loading of a symbol and may return an error. Load(*Symbol) error } -// LoadHooks is a slice of LoadHook interfaces, processed sequentially. +// LoadHooks is a collection of LoadHook instances. type LoadHooks []LoadHook +// LoadListener registers and invokes LoadHook instances. +type LoadListener interface { + Load(LoadHook) error +} + type loadHook struct { - load func(*Symbol) error + fn func(*Symbol) error } -var _ LoadHook = (LoadHooks)(nil) var _ LoadHook = (*loadHook)(nil) +var _ LoadHook = (LoadHooks)(nil) + +// LoadFunc wraps a function as a LoadHook. +func LoadFunc(fn func(*Symbol) error) LoadHook { + return &loadHook{fn: fn} +} -// LoadFunc creates a new LoadHook from the provided function. -func LoadFunc(load func(*Symbol) error) LoadHook { - return &loadHook{load: load} +// LoadListenerHook creates a LoadHook for nodes implementing LoadListener. +func LoadListenerHook(hook LoadHook) LoadHook { + return LoadFunc(func(symbol *Symbol) error { + if listener, ok := node.Unwrap(symbol).(LoadListener); ok { + return listener.Load(hook) + } + return nil + }) } -func (h LoadHooks) Load(sb *Symbol) error { - for _, hook := range h { - if err := hook.Load(sb); err != nil { +// Load executes all LoadHooks sequentially. +func (hooks LoadHooks) Load(symbol *Symbol) error { + for _, hook := range hooks { + if err := hook.Load(symbol); err != nil { return err } } return nil } -func (h *loadHook) Load(sb *Symbol) error { - return h.load(sb) +// Load executes the associated function. +func (h *loadHook) Load(symbol *Symbol) error { + return h.fn(symbol) } diff --git a/pkg/symbol/unloadhook.go b/pkg/symbol/unloadhook.go index 08ad79bd..b99404f3 100644 --- a/pkg/symbol/unloadhook.go +++ b/pkg/symbol/unloadhook.go @@ -1,36 +1,53 @@ package symbol -// UnloadHook defines an interface for handling events when a symbol is unloaded. +import "github.com/siyul-park/uniflow/pkg/node" + +// UnloadHook handles symbol unload events. type UnloadHook interface { - // Unload is called when a symbol is unloaded and may return an error. Unload(*Symbol) error } -// UnloadHooks is a slice of UnloadHook interfaces, processed in reverse order. +// UnloadHooks is a collection of UnloadHook instances, processed in reverse order. type UnloadHooks []UnloadHook +// UnloadListener registers and invokes UnloadHook instances. +type UnloadListener interface { + Unload(UnloadHook) error +} + type unloadHook struct { - unload func(*Symbol) error + fn func(*Symbol) error } -var _ UnloadHook = (UnloadHooks)(nil) var _ UnloadHook = (*unloadHook)(nil) +var _ UnloadHook = (UnloadHooks)(nil) + +// UnloadFunc wraps a function as an UnloadHook. +func UnloadFunc(fn func(*Symbol) error) UnloadHook { + return &unloadHook{fn: fn} +} -// UnloadFunc creates a new UnloadHook from the provided function. -func UnloadFunc(unload func(*Symbol) error) UnloadHook { - return &unloadHook{unload: unload} +// UnloadListenerHook creates an UnloadHook for nodes implementing UnloadListener. +func UnloadListenerHook(hook UnloadHook) UnloadHook { + return UnloadFunc(func(symbol *Symbol) error { + if listener, ok := node.Unwrap(symbol).(UnloadListener); ok { + return listener.Unload(hook) + } + return nil + }) } -func (h UnloadHooks) Unload(sb *Symbol) error { - for i := len(h) - 1; i >= 0; i-- { - hook := h[i] - if err := hook.Unload(sb); err != nil { +// Unload executes all UnloadHooks in reverse order. +func (hooks UnloadHooks) Unload(symbol *Symbol) error { + for i := len(hooks) - 1; i >= 0; i-- { + if err := hooks[i].Unload(symbol); err != nil { return err } } return nil } -func (h *unloadHook) Unload(sb *Symbol) error { - return h.unload(sb) +// Unload executes the associated function. +func (h *unloadHook) Unload(symbol *Symbol) error { + return h.fn(symbol) }