Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CLD-16]: feat(operation): introduce operation component #16756

Merged
merged 1 commit into from
Mar 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions deployment/operations/operation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package operations

import (
"context"

"github.com/Masterminds/semver/v3"

"github.com/smartcontractkit/chainlink-common/pkg/logger"
)

// Bundle contains the dependencies required by Operations API and is passed to the OperationHandler and SequenceHandler.
// It contains the Logger, Reporter and the context.
// Use NewBundle to create a new Bundle.
type Bundle struct {
Logger logger.Logger
GetContext func() context.Context
}

// NewBundle creates and returns a new Bundle.
func NewBundle(getContext func() context.Context, logger logger.Logger) Bundle {
return Bundle{
Logger: logger,
GetContext: getContext,
}
}

// OperationHandler is the function signature of an operation handler.
type OperationHandler[IN, OUT, DEP any] func(e Bundle, deps DEP, input IN) (output OUT, err error)

// Definition is the metadata for a sequence or an operation.
// It contains the ID, version and description.
// This definition and OperationHandler together form the composite keys for an Operation.
// 2 Operations are considered the same if they have the Definition and OperationHandler.
type Definition struct {
ID string
Version *semver.Version
Description string
}

// Operation is the low level building blocks of the Operations API.
// Developers define their own operation with custom input and output types.
// Each operation should only perform max 1 side effect (e.g. send a transaction, post a job spec...)
// Use NewOperation to create a new operation.
type Operation[IN, OUT, DEP any] struct {
def Definition
handler OperationHandler[IN, OUT, DEP]
}

// ID returns the operation ID.
func (o *Operation[IN, OUT, DEP]) ID() string {
return o.def.ID
}

// Version returns the operation semver version in string.
func (o *Operation[IN, OUT, DEP]) Version() string {
return o.def.Version.String()
}

// Description returns the operation description.
func (o *Operation[IN, OUT, DEP]) Description() string {
return o.def.Description
}

// execute runs the operation by calling the OperationHandler.
func (o *Operation[IN, OUT, DEP]) execute(b Bundle, deps DEP, input IN) (output OUT, err error) {
b.Logger.Infow("Executing operation",
"id", o.def.ID, "version", o.def.Version, "description", o.def.Description)
return o.handler(b, deps, input)
}

// NewOperation creates a new operation.
// Version can be created using semver.MustParse("1.0.0") or semver.New("1.0.0").
// Note: The handler should only perform maximum 1 side effect.
func NewOperation[IN, OUT, DEP any](
id string, version *semver.Version, description string, handler OperationHandler[IN, OUT, DEP],
) *Operation[IN, OUT, DEP] {
return &Operation[IN, OUT, DEP]{
def: Definition{
ID: id,
Version: version,
Description: description,
},
handler: handler,
}
}

// EmptyInput is a placeholder for operations that do not require input.
type EmptyInput struct{}
71 changes: 71 additions & 0 deletions deployment/operations/operation_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package operations

import (
"context"
"testing"

"github.com/Masterminds/semver/v3"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zapcore"

"github.com/smartcontractkit/chainlink-common/pkg/logger"
)

type OpDeps struct{}

type OpInput struct {
A int
B int
}

func Test_NewOperation(t *testing.T) {
t.Parallel()

version := semver.MustParse("1.0.0")
description := "test operation"
handler := func(b Bundle, deps OpDeps, input OpInput) (output int, err error) {
return input.A + input.B, nil
}

op := NewOperation("sum", version, description, handler)

assert.Equal(t, "sum", op.ID())
assert.Equal(t, version.String(), op.Version())
assert.Equal(t, description, op.Description())
res, err := op.handler(Bundle{}, OpDeps{}, OpInput{1, 2})
require.NoError(t, err)
assert.Equal(t, 3, res)
}

func Test_Operation_Execute(t *testing.T) {
t.Parallel()

version := semver.MustParse("1.0.0")
description := "test operation"
log, observedLog := logger.TestObserved(t, zapcore.InfoLevel)

// simulate an addition operation
handler := func(b Bundle, deps OpDeps, input OpInput) (output int, err error) {
return input.A + input.B, nil
}

op := NewOperation("sum", version, description, handler)
e := NewBundle(context.Background, log)
input := OpInput{
A: 1,
B: 2,
}

output, err := op.execute(e, OpDeps{}, input)

require.NoError(t, err)
assert.Equal(t, 3, output)

require.Equal(t, 1, observedLog.Len())
entry := observedLog.All()[0]
assert.Equal(t, "Executing operation", entry.Message)
assert.Equal(t, "sum", entry.ContextMap()["id"])
assert.Equal(t, version.String(), entry.ContextMap()["version"])
assert.Equal(t, description, entry.ContextMap()["description"])
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we also add test where handler uses empty input ?

Copy link
Collaborator Author

@graham-chainlink graham-chainlink Mar 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a particular scenario you are thinking? The empty input test doesnt cover any extra logic or scenario.

Copy link
Contributor

@ajaskolski ajaskolski Mar 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just meant to cover it e2e. so to addition smth like

 func Test_Operation_WithEmptyInput(t *testing.T) {
	t.Parallel()

	handler := func(b Bundle, deps OpDeps, _ EmptyInput) (string, error) {
		return "ok", nil
	}
	op := NewOperation("noop", semver.MustParse("1.0.0"), "no-op", handler)
	out, err := op.execute(NewBundle(context.Background, logger.Test(t)), OpDeps{}, EmptyInput{})
	require.NoError(t, err)
	assert.Equal(t, "ok", out)
}

its nit

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay, i will add it in my next PR

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added it here #16801

Loading