From cf9a2213b17163411ed685a2ce4fadca8e8b49d2 Mon Sep 17 00:00:00 2001 From: Cedric Date: Tue, 11 Jun 2024 18:01:04 +0100 Subject: [PATCH] [chore] Don't hardcode the trigger id (#13506) --- core/services/workflows/engine.go | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/core/services/workflows/engine.go b/core/services/workflows/engine.go index 47638f71434..0bedd63f163 100644 --- a/core/services/workflows/engine.go +++ b/core/services/workflows/engine.go @@ -22,11 +22,6 @@ import ( "github.com/smartcontractkit/chainlink/v2/core/services/workflows/store" ) -const ( - // NOTE: max 32 bytes per ID - consider enforcing exactly 32 bytes? - mockedTriggerID = "cccccccccc0000000000000000000000" -) - type donInfo struct { *capabilities.DON PeerID func() *p2ptypes.PeerID @@ -217,8 +212,8 @@ func (e *Engine) init(ctx context.Context) { } e.logger.Debug("registering triggers") - for _, t := range e.workflow.triggers { - err := e.registerTrigger(ctx, t) + for idx, t := range e.workflow.triggers { + err := e.registerTrigger(ctx, t, idx) if err != nil { e.logger.Errorf("failed to register trigger: %s", err) } @@ -277,11 +272,15 @@ func (e *Engine) resumeInProgressExecutions(ctx context.Context) error { return nil } +func generateTriggerId(workflowID string, triggerIdx int) string { + return fmt.Sprintf("wf_%s_trigger_%d", workflowID, triggerIdx) +} + // registerTrigger is used during the initialization phase to bind a trigger to this workflow -func (e *Engine) registerTrigger(ctx context.Context, t *triggerCapability) error { +func (e *Engine) registerTrigger(ctx context.Context, t *triggerCapability, triggerIdx int) error { triggerInputs, err := values.NewMap( map[string]any{ - "triggerId": mockedTriggerID, + "triggerId": generateTriggerId(e.workflow.id, triggerIdx), }, ) if err != nil { @@ -649,10 +648,10 @@ func (e *Engine) executeStep(ctx context.Context, l logger.Logger, msg stepReque return inputs, output, err } -func (e *Engine) deregisterTrigger(ctx context.Context, t *triggerCapability) error { +func (e *Engine) deregisterTrigger(ctx context.Context, t *triggerCapability, triggerIdx int) error { triggerInputs, err := values.NewMap( map[string]any{ - "triggerId": mockedTriggerID, + "triggerId": generateTriggerId(e.workflow.id, triggerIdx), }, ) if err != nil { @@ -687,8 +686,8 @@ func (e *Engine) Close() error { // any triggers to ensure no new executions are triggered, // then we'll close down any background goroutines, // and finally, we'll deregister any workflow steps. - for _, t := range e.workflow.triggers { - err := e.deregisterTrigger(ctx, t) + for idx, t := range e.workflow.triggers { + err := e.deregisterTrigger(ctx, t, idx) if err != nil { return err }