-
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.
feat(workflows): adds registry syncer
- Loading branch information
Showing
14 changed files
with
1,575 additions
and
19 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
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
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,72 @@ | ||
package syncer | ||
|
||
import ( | ||
"context" | ||
"encoding/hex" | ||
"fmt" | ||
|
||
"github.com/smartcontractkit/chainlink/v2/core/logger" | ||
) | ||
|
||
// eventHandler is a handler for WorkflowRegistryEvent events. Each event type has a corresponding | ||
// method that handles the event. | ||
type eventHandler struct { | ||
lggr logger.Logger | ||
orm ORM | ||
fetcher FetcherFunc | ||
} | ||
|
||
// newEventHandler returns a new eventHandler instance. | ||
func newEventHandler( | ||
lggr logger.Logger, | ||
orm ORM, | ||
gateway FetcherFunc, | ||
) *eventHandler { | ||
return &eventHandler{ | ||
lggr: lggr, | ||
orm: orm, | ||
fetcher: gateway, | ||
} | ||
} | ||
|
||
func (h *eventHandler) Handle(ctx context.Context, event WorkflowRegistryEvent) error { | ||
switch event.EventType { | ||
case ForceUpdateSecretsEvent: | ||
return h.forceUpdateSecretsEvent(ctx, event) | ||
default: | ||
return fmt.Errorf("event type unsupported: %v", event.EventType) | ||
} | ||
} | ||
|
||
// forceUpdateSecretsEvent handles the ForceUpdateSecretsEvent event type. | ||
func (h *eventHandler) forceUpdateSecretsEvent( | ||
ctx context.Context, | ||
event WorkflowRegistryEvent, | ||
) error { | ||
// Get the URL of the secrets file from the event data | ||
data, ok := event.Data.(WorkflowRegistryForceUpdateSecretsRequestedV1) | ||
if !ok { | ||
return fmt.Errorf("invalid data type %T for event", event.Data) | ||
} | ||
|
||
hash := hex.EncodeToString(data.SecretsURLHash) | ||
|
||
url, err := h.orm.GetSecretsURLByHash(ctx, hash) | ||
if err != nil { | ||
h.lggr.Errorf("failed to get URL by hash %s : %w", hash, err) | ||
return err | ||
} | ||
|
||
// Fetch the contents of the secrets file from the url via the fetcher | ||
secrets, err := h.fetcher(ctx, url) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Update the secrets in the ORM | ||
if _, err := h.orm.Update(ctx, hash, string(secrets)); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
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,63 @@ | ||
package syncer | ||
|
||
import "container/heap" | ||
|
||
type Heap interface { | ||
// Push adds a new item to the heap. | ||
Push(x WorkflowRegistryEventResponse) | ||
|
||
// Pop removes the smallest item from the heap and returns it. | ||
Pop() WorkflowRegistryEventResponse | ||
|
||
// Len returns the number of items in the heap. | ||
Len() int | ||
} | ||
|
||
// publicHeap is a wrapper around the heap.Interface that exposes the Push and Pop methods. | ||
type publicHeap[T any] struct { | ||
heap heap.Interface | ||
} | ||
|
||
func (h *publicHeap[T]) Push(x T) { | ||
heap.Push(h.heap, x) | ||
} | ||
|
||
func (h *publicHeap[T]) Pop() T { | ||
return heap.Pop(h.heap).(T) | ||
} | ||
|
||
func (h *publicHeap[T]) Len() int { | ||
return h.heap.Len() | ||
} | ||
|
||
// blockHeightHeap is a heap.Interface that sorts WorkflowRegistryEventResponses by block height. | ||
type blockHeightHeap []WorkflowRegistryEventResponse | ||
|
||
// newBlockHeightHeap returns an initialized heap that sorts WorkflowRegistryEventResponses by block height. | ||
func newBlockHeightHeap() Heap { | ||
h := blockHeightHeap(make([]WorkflowRegistryEventResponse, 0)) | ||
heap.Init(&h) | ||
return &publicHeap[WorkflowRegistryEventResponse]{heap: &h} | ||
} | ||
|
||
func (h *blockHeightHeap) Len() int { return len(*h) } | ||
|
||
func (h *blockHeightHeap) Less(i, j int) bool { | ||
return (*h)[i].Event.Head.Height < (*h)[j].Event.Head.Height | ||
} | ||
|
||
func (h *blockHeightHeap) Swap(i, j int) { | ||
(*h)[i], (*h)[j] = (*h)[j], (*h)[i] | ||
} | ||
|
||
func (h *blockHeightHeap) Push(x any) { | ||
*h = append(*h, x.(WorkflowRegistryEventResponse)) | ||
} | ||
|
||
func (h *blockHeightHeap) Pop() any { | ||
old := *h | ||
n := len(old) | ||
x := old[n-1] | ||
*h = old[0 : n-1] | ||
return x | ||
} |
Oops, something went wrong.