Skip to content

Commit

Permalink
Complete usage of services.Service with StartOnce and StopOnce with t…
Browse files Browse the repository at this point in the history
…ests updated
  • Loading branch information
kidambisrinivas committed Sep 30, 2024
1 parent 723b872 commit 9b6d66b
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 16 deletions.
17 changes: 8 additions & 9 deletions core/capabilities/triggers/logevent/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,28 +131,27 @@ func (s *TriggerService) UnregisterTrigger(ctx context.Context, req capabilities

// Start the service.
func (s *TriggerService) Start(ctx context.Context) error {
return nil
return s.StartOnce("LogEventTriggerCapabilityService", func() error {
s.lggr.Info("Starting LogEventTriggerCapabilityService")
return nil
})
}

// Close stops the Service.
// After this call the Service cannot be started again,
// The service will need to be re-built to start scheduling again.
func (s *TriggerService) Close() error {
return s.StopOnce("Log Event Trigger Capability Service", func() error {
s.lggr.Infow("Stopping LogEventTrigger Capability Service")
return s.StopOnce("LogEventTriggerCapabilityService", func() error {
s.lggr.Infow("Stopping LogEventTriggerCapabilityService")
triggers := s.triggers.ReadAll()
return services.MultiCloser(triggers).Close()
})
}

func (s *TriggerService) Ready() error {
return nil
}

func (s *TriggerService) HealthReport() map[string]error {
return map[string]error{s.Name(): nil}
return map[string]error{s.Name(): s.Healthy()}
}

func (s *TriggerService) Name() string {
return "Service"
return s.lggr.Name()
}
8 changes: 4 additions & 4 deletions core/capabilities/triggers/logevent/trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func newLogEventTrigger(ctx context.Context,
contractReader, err := relayer.NewContractReader(ctx, jsonBytes)
if err != nil {
return nil, nil,
fmt.Errorf("error fetching contractReader for chainID %s from relayerSet: %v", logEventConfig.ChainID, err)
fmt.Errorf("error fetching contractReader for chainID %s from relayerSet: %w", logEventConfig.ChainID, err)
}

// Bind Contract in ContractReader
Expand All @@ -77,11 +77,11 @@ func newLogEventTrigger(ctx context.Context,
// Get current block HEAD/tip of the blockchain to start polling from
latestHead, err := relayer.LatestHead(ctx)
if err != nil {
return nil, nil, fmt.Errorf("error getting latestHead from relayer client: %v", err)
return nil, nil, fmt.Errorf("error getting latestHead from relayer client: %w", err)
}
height, err := strconv.ParseUint(latestHead.Height, 10, 64)
if err != nil {
return nil, nil, fmt.Errorf("invalid height in latestHead from relayer client: %v", err)
return nil, nil, fmt.Errorf("invalid height in latestHead from relayer client: %w", err)
}
startBlockNum := uint64(0)
if height > logEventConfig.LookbackBlocks {
Expand Down Expand Up @@ -158,7 +158,7 @@ func (l *logEventTrigger) listen() {
&logData,
)
if err != nil {
l.lggr.Fatalw("QueryKey failure", "err", err)
l.lggr.Errorw("QueryKey failure", "err", err)
continue
}
// ChainReader QueryKey API provides logs including the cursor value and not
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"

"github.com/smartcontractkit/chainlink-common/pkg/services/servicetest"
commontypes "github.com/smartcontractkit/chainlink-common/pkg/types"
commonmocks "github.com/smartcontractkit/chainlink-common/pkg/types/core/mocks"
"github.com/smartcontractkit/chainlink/v2/core/capabilities/triggers/logevent"
Expand Down Expand Up @@ -50,6 +51,10 @@ func TestLogEventTriggerEVMHappyPath(t *testing.T) {
relayer,
logEventConfig)
require.NoError(t, err)

// Start the service
servicetest.Run(t, logEventTriggerService)

log1Ch, err := logEventTriggerService.RegisterTrigger(ctx, th.LogEmitterRegRequest)
require.NoError(t, err)

Expand Down Expand Up @@ -77,6 +82,4 @@ func TestLogEventTriggerEVMHappyPath(t *testing.T) {
actualLogVal, err := testutils.GetBigIntValL2(output, "Data", "Arg0")
require.NoError(t, err)
require.Equal(t, expectedLogVal, actualLogVal.Int64())

logEventTriggerService.Close()
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ func NewContractReaderTH(t *testing.T) *ContractReaderTH {
// Encode the config map as JSON to specify in the expected call in mocked object
// The LogEventTrigger Capability receives a config map, encodes it and
// calls NewContractReader with it
contractReaderCfgBytes, _ = json.Marshal(contractReaderCfgMap)
contractReaderCfgBytes, err = json.Marshal(contractReaderCfgMap)
require.NoError(t, err)

reqConfig.ContractReaderConfig = contractReaderCfgMap

Expand Down

0 comments on commit 9b6d66b

Please sign in to comment.