From 809788d44c34834946a89220a5462d30bf31d555 Mon Sep 17 00:00:00 2001 From: Michael Street <5597260+MStreet3@users.noreply.github.com> Date: Wed, 20 Nov 2024 14:45:15 +0200 Subject: [PATCH] chore(workflows): stubs out engine registry --- .../workflows/syncer/engine_registry.go | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 core/services/workflows/syncer/engine_registry.go diff --git a/core/services/workflows/syncer/engine_registry.go b/core/services/workflows/syncer/engine_registry.go new file mode 100644 index 00000000000..1d32dcf4a66 --- /dev/null +++ b/core/services/workflows/syncer/engine_registry.go @@ -0,0 +1,64 @@ +package syncer + +import ( + "errors" + "sync" + + "github.com/smartcontractkit/chainlink/v2/core/services/workflows" +) + +type engineRegistry struct { + engines map[string]*workflows.Engine + mu sync.RWMutex +} + +func newEngineRegistry() *engineRegistry { + return &engineRegistry{ + engines: make(map[string]*workflows.Engine), + } +} + +// Add adds an engine to the registry. +func (r *engineRegistry) Add(id string, engine *workflows.Engine) { + r.mu.Lock() + defer r.mu.Unlock() + r.engines[id] = engine +} + +// Get retrieves an engine from the registry. +func (r *engineRegistry) Get(id string) *workflows.Engine { + r.mu.RLock() + defer r.mu.RUnlock() + return r.engines[id] +} + +// Remove removes an engine from the registry. +func (r *engineRegistry) Remove(id string) error { + r.mu.Lock() + defer r.mu.Unlock() + engine, ok := r.engines[id] + if !ok { + return errors.New("remove failed: engine not found") + } + err := engine.Close() + if err != nil { + return err + } + delete(r.engines, id) + return nil +} + +// Close closes all engines in the registry. +func (r *engineRegistry) Close() error { + r.mu.Lock() + defer r.mu.Unlock() + var err error + for id, engine := range r.engines { + closeErr := engine.Close() + if closeErr != nil { + err = errors.Join(err, closeErr) + } + delete(r.engines, id) + } + return err +}