Skip to content

Commit

Permalink
[chore] Don't hardcode the trigger id (#13506)
Browse files Browse the repository at this point in the history
  • Loading branch information
cedric-cordenier authored Jun 11, 2024
1 parent e16b6ce commit cf9a221
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions core/services/workflows/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit cf9a221

Please sign in to comment.