|
| 1 | +package operations |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/Masterminds/semver/v3" |
| 7 | + "github.com/google/uuid" |
| 8 | + |
| 9 | + "github.com/smartcontractkit/chainlink-common/pkg/logger" |
| 10 | +) |
| 11 | + |
| 12 | +// OperationEnv is the env passed to the OperationHandler. |
| 13 | +// It contains the logger and the context. |
| 14 | +// Use NewOperationEnv to create a new OperationEnv. |
| 15 | +type OperationEnv struct { |
| 16 | + logger logger.Logger |
| 17 | + getContext func() context.Context |
| 18 | +} |
| 19 | + |
| 20 | +// NewOperationEnv creates and returns a new OperationEnv. |
| 21 | +func NewOperationEnv(getContext func() context.Context, logger logger.Logger) OperationEnv { |
| 22 | + return OperationEnv{ |
| 23 | + logger: logger, |
| 24 | + getContext: getContext, |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +// OperationHandler is the function signature of an operation handler. |
| 29 | +type OperationHandler[I, O, D any] func(e OperationEnv, deps D, input I) (output O, err error) |
| 30 | + |
| 31 | +// OperationDefinition is the metadata of an operation. |
| 32 | +type OperationDefinition struct { |
| 33 | + ID string |
| 34 | + Version semver.Version |
| 35 | + Description string |
| 36 | +} |
| 37 | + |
| 38 | +// Operation is the low level building blocks of the Operations API. |
| 39 | +// Developers define their own operation with custom input and output types. |
| 40 | +// Each operation should only perform max 1 side effect (e.g. send a transaction, post a job spec...) |
| 41 | +// Use NewOperation to create a new operation. |
| 42 | +type Operation[Input, Output, Deps any] struct { |
| 43 | + def OperationDefinition |
| 44 | + handler OperationHandler[Input, Output, Deps] |
| 45 | +} |
| 46 | + |
| 47 | +// NewOperation creates a new operation. |
| 48 | +// Note: The handler should only perform maximum 1 side effect. |
| 49 | +func NewOperation[I, O, D any]( |
| 50 | + version semver.Version, description string, handler OperationHandler[I, O, D], |
| 51 | +) *Operation[I, O, D] { |
| 52 | + return &Operation[I, O, D]{ |
| 53 | + def: OperationDefinition{ |
| 54 | + ID: uuid.New().String(), |
| 55 | + Version: version, |
| 56 | + Description: description, |
| 57 | + }, |
| 58 | + handler: handler, |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +// Execute runs the operation by calling the OperationHandler. |
| 63 | +func (o *Operation[I, O, D]) Execute(e OperationEnv, deps D, input I) (output O, err error) { |
| 64 | + e.logger.Infow("Executing operation", |
| 65 | + "id", o.def.ID, "version", o.def.Version, "description", o.def.Description) |
| 66 | + return o.handler(e, deps, input) |
| 67 | +} |
| 68 | + |
| 69 | +// ID returns the operation ID. |
| 70 | +func (o *Operation[I, O, D]) ID() string { |
| 71 | + return o.def.ID |
| 72 | +} |
| 73 | + |
| 74 | +// Version returns the operation semver version in string. |
| 75 | +func (o *Operation[I, O, D]) Version() string { |
| 76 | + return o.def.Version.String() |
| 77 | +} |
| 78 | + |
| 79 | +// Description returns the operation description. |
| 80 | +func (o *Operation[I, O, D]) Description() string { |
| 81 | + return o.def.Description |
| 82 | +} |
| 83 | + |
| 84 | +// EmptyInput is a placeholder for operations that do not require input. |
| 85 | +type EmptyInput struct{} |
0 commit comments