-
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
17 changed files
with
1,805 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 : %s", 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 | ||
} |
Oops, something went wrong.