-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
standard capability support (#13215)
- Loading branch information
Showing
26 changed files
with
594 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"chainlink": minor | ||
--- | ||
|
||
#internal standard capability support |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,7 @@ var ( | |
VRF: {}, | ||
Webhook: {}, | ||
Workflow: {}, | ||
StandardCapabilities: {}, | ||
} | ||
) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package standardcapabilities | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/google/uuid" | ||
"github.com/pelletier/go-toml" | ||
"github.com/pkg/errors" | ||
|
||
"github.com/smartcontractkit/chainlink-common/pkg/loop" | ||
"github.com/smartcontractkit/chainlink-common/pkg/sqlutil" | ||
"github.com/smartcontractkit/chainlink-common/pkg/types" | ||
"github.com/smartcontractkit/chainlink-common/pkg/types/core" | ||
"github.com/smartcontractkit/chainlink/v2/core/logger" | ||
"github.com/smartcontractkit/chainlink/v2/core/services/job" | ||
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/generic" | ||
"github.com/smartcontractkit/chainlink/v2/core/services/pipeline" | ||
"github.com/smartcontractkit/chainlink/v2/core/services/telemetry" | ||
"github.com/smartcontractkit/chainlink/v2/plugins" | ||
) | ||
|
||
type RelayGetter interface { | ||
Get(id types.RelayID) (loop.Relayer, error) | ||
GetIDToRelayerMap() (map[types.RelayID]loop.Relayer, error) | ||
} | ||
|
||
type Delegate struct { | ||
logger logger.Logger | ||
ds sqlutil.DataSource | ||
jobORM job.ORM | ||
registry core.CapabilitiesRegistry | ||
cfg plugins.RegistrarConfig | ||
monitoringEndpointGen telemetry.MonitoringEndpointGenerator | ||
pipelineRunner pipeline.Runner | ||
relayers RelayGetter | ||
|
||
isNewlyCreatedJob bool | ||
} | ||
|
||
func NewDelegate(logger logger.Logger, ds sqlutil.DataSource, jobORM job.ORM, registry core.CapabilitiesRegistry, | ||
cfg plugins.RegistrarConfig, monitoringEndpointGen telemetry.MonitoringEndpointGenerator, pipelineRunner pipeline.Runner, | ||
relayers RelayGetter) *Delegate { | ||
return &Delegate{logger: logger, ds: ds, jobORM: jobORM, registry: registry, cfg: cfg, monitoringEndpointGen: monitoringEndpointGen, pipelineRunner: pipelineRunner, | ||
relayers: relayers, isNewlyCreatedJob: false} | ||
} | ||
|
||
func (d *Delegate) JobType() job.Type { | ||
return job.StandardCapabilities | ||
} | ||
|
||
func (d *Delegate) BeforeJobCreated(job job.Job) { | ||
// This is only called first time the job is created | ||
d.isNewlyCreatedJob = true | ||
} | ||
|
||
func (d *Delegate) ServicesForSpec(ctx context.Context, spec job.Job) ([]job.ServiceCtx, error) { | ||
log := d.logger.Named("StandardCapabilities").Named(spec.StandardCapabilitiesSpec.GetID()) | ||
|
||
kvStore := job.NewKVStore(spec.ID, d.ds, log) | ||
telemetryService := generic.NewTelemetryAdapter(d.monitoringEndpointGen) | ||
errorLog := &ErrorLog{jobID: spec.ID, recordError: d.jobORM.RecordError} | ||
pr := generic.NewPipelineRunnerAdapter(log, spec, d.pipelineRunner) | ||
|
||
relayerSet, err := generic.NewRelayerSet(d.relayers, spec.ExternalJobID, spec.ID, d.isNewlyCreatedJob) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to create relayer set: %w", err) | ||
} | ||
|
||
standardCapability := newStandardCapabilities(log, spec.StandardCapabilitiesSpec, d.cfg, telemetryService, kvStore, d.registry, errorLog, | ||
pr, relayerSet) | ||
|
||
return []job.ServiceCtx{standardCapability}, nil | ||
} | ||
|
||
func (d *Delegate) AfterJobCreated(job job.Job) {} | ||
|
||
func (d *Delegate) BeforeJobDeleted(job job.Job) {} | ||
|
||
func (d *Delegate) OnDeleteJob(ctx context.Context, jb job.Job) error { return nil } | ||
|
||
func ValidatedStandardCapabilitiesSpec(tomlString string) (job.Job, error) { | ||
var jb = job.Job{ExternalJobID: uuid.New()} | ||
|
||
tree, err := toml.Load(tomlString) | ||
if err != nil { | ||
return jb, errors.Wrap(err, "toml error on load standard capabilities") | ||
} | ||
|
||
err = tree.Unmarshal(&jb) | ||
if err != nil { | ||
return jb, errors.Wrap(err, "toml unmarshal error on standard capabilities spec") | ||
} | ||
|
||
var spec job.StandardCapabilitiesSpec | ||
err = tree.Unmarshal(&spec) | ||
if err != nil { | ||
return jb, errors.Wrap(err, "toml unmarshal error on standard capabilities job") | ||
} | ||
|
||
jb.StandardCapabilitiesSpec = &spec | ||
if jb.Type != job.StandardCapabilities { | ||
return jb, errors.Errorf("standard capabilities unsupported job type %s", jb.Type) | ||
} | ||
|
||
if len(jb.StandardCapabilitiesSpec.Command) == 0 { | ||
return jb, errors.Errorf("standard capabilities command must be set") | ||
} | ||
|
||
return jb, nil | ||
} | ||
|
||
type ErrorLog struct { | ||
jobID int32 | ||
recordError func(ctx context.Context, jobID int32, description string) error | ||
} | ||
|
||
func (l *ErrorLog) SaveError(ctx context.Context, msg string) error { | ||
return l.recordError(ctx, l.jobID, msg) | ||
} |
Oops, something went wrong.