Skip to content

Commit

Permalink
Now all the roller commands write to a log file
Browse files Browse the repository at this point in the history
  • Loading branch information
ItayLevyOfficial committed Jun 21, 2023
1 parent aa87e24 commit 0f82bd9
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 17 deletions.
13 changes: 9 additions & 4 deletions cmd/relayer/start/create_ibc_channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,18 @@ import (
"path/filepath"
)

func createIBCChannelIfNeeded(rollappConfig utils.RollappConfig) (string, error) {
func getLogFilePath(home string) string {
return filepath.Join(home, consts.ConfigDirName.Relayer, "relayer.log")
}

func createIBCChannelIfNeeded(rollappConfig utils.RollappConfig, logFileOption utils.CommandOption) (string, error) {
/**
Creates an IBC channel between the hub and the client, and return the source channel ID.
*/

createClientsCmd := getCreateClientsCmd(rollappConfig, rollappConfig.RollappID, rollappConfig.HubData.ID)
fmt.Println("Creating clients...")
if err := utils.ExecBashCmdWithOSOutput(createClientsCmd); err != nil {
if err := utils.ExecBashCmdWithOSOutput(createClientsCmd, logFileOption); err != nil {
return "", err
}
dstConnectionId, err := GetDstConnectionIDFromYAMLFile(filepath.Join(rollappConfig.Home, consts.ConfigDirName.Relayer,
Expand All @@ -25,7 +30,7 @@ func createIBCChannelIfNeeded(rollappConfig utils.RollappConfig) (string, error)
if dstConnectionId == "" {
createConnectionCmd := getCreateConnectionCmd(rollappConfig)
fmt.Println("Creating connection...")
if err := utils.ExecBashCmdWithOSOutput(createConnectionCmd); err != nil {
if err := utils.ExecBashCmdWithOSOutput(createConnectionCmd, logFileOption); err != nil {
return "", err
}
}
Expand All @@ -36,7 +41,7 @@ func createIBCChannelIfNeeded(rollappConfig utils.RollappConfig) (string, error)
if srcChannelId == "" {
createChannelCmd := getCreateChannelCmd(rollappConfig)
fmt.Println("Creating channel...")
if err := utils.ExecBashCmdWithOSOutput(createChannelCmd); err != nil {
if err := utils.ExecBashCmdWithOSOutput(createChannelCmd, logFileOption); err != nil {
return "", err
}
srcChannelId, err = GetSourceChannelForConnection(dstConnectionId, rollappConfig)
Expand Down
10 changes: 6 additions & 4 deletions cmd/relayer/start/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,18 @@ func Start() *cobra.Command {
home := cmd.Flag(utils.FlagNames.Home).Value.String()
rollappConfig, err := utils.LoadConfigFromTOML(home)
utils.PrettifyErrorIfExists(err)
srcChannelId, err := createIBCChannelIfNeeded(rollappConfig)
relayerLogFilePath := getLogFilePath(rollappConfig.Home)
logFileOption := utils.WithLogging(relayerLogFilePath)
srcChannelId, err := createIBCChannelIfNeeded(rollappConfig, logFileOption)
utils.PrettifyErrorIfExists(err)
updateClientsCmd := getUpdateClientsCmd(rollappConfig)
utils.RunCommandEvery(updateClientsCmd.Path, updateClientsCmd.Args[1:], 60)
utils.RunCommandEvery(updateClientsCmd.Path, updateClientsCmd.Args[1:], 60, logFileOption)
relayPacketsCmd := getRelayPacketsCmd(rollappConfig, srcChannelId)
utils.RunCommandEvery(relayPacketsCmd.Path, relayPacketsCmd.Args[1:], 30)
utils.RunCommandEvery(relayPacketsCmd.Path, relayPacketsCmd.Args[1:], 30, logFileOption)
startCmd := getRlyStartCmd(rollappConfig)
utils.RunBashCmdAsync(startCmd, func() {
fmt.Printf("💈 The relayer is running successfully on you local machine on channel %s!", srcChannelId)
}, parseError)
}, parseError, logFileOption)
},
}
utils.AddGlobalFlags(registerCmd)
Expand Down
15 changes: 10 additions & 5 deletions cmd/utils/bash_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,16 @@ func GetRelayerDefaultFlags(root string) []string {
}
}

func RunCommandEvery(command string, args []string, intervalSec int) {
func RunCommandEvery(command string, args []string, intervalSec int, options ...CommandOption) {
go func() {
for {
cmd := exec.Command(command, args...)
for _, option := range options {
option(cmd)
}
var stderr bytes.Buffer
cmd.Stderr = &stderr
errmw := io.MultiWriter(&stderr, cmd.Stderr)
cmd.Stderr = errmw
err := cmd.Run()
if err != nil {
// get the cmd args joined by space
Expand All @@ -40,9 +44,10 @@ func GetCommonDymdFlags(rollappConfig RollappConfig) []string {
}
}

type CommandOptions func(cmd *exec.Cmd)
type CommandOption func(cmd *exec.Cmd)

func RunBashCmdAsync(cmd *exec.Cmd, printOutput func(), parseError func(errMsg string) string, options ...CommandOptions) {
func RunBashCmdAsync(cmd *exec.Cmd, printOutput func(), parseError func(errMsg string) string,
options ...CommandOption) {
for _, option := range options {
option(cmd)
}
Expand All @@ -62,7 +67,7 @@ func RunBashCmdAsync(cmd *exec.Cmd, printOutput func(), parseError func(errMsg s
}
}

func WithLogging(logFile string) CommandOptions {
func WithLogging(logFile string) CommandOption {
return func(cmd *exec.Cmd) {
logger := getLogger(logFile)
cmd.Stdout = logger.Writer()
Expand Down
12 changes: 8 additions & 4 deletions cmd/utils/error_handling.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,15 @@ func ExecBashCommand(cmd *exec.Cmd) (bytes.Buffer, error) {
return stdout, nil
}

func ExecBashCmdWithOSOutput(cmd *exec.Cmd) error {
func ExecBashCmdWithOSOutput(cmd *exec.Cmd, options ...CommandOption) error {
for _, option := range options {
option(cmd)
}
var stderr bytes.Buffer
cmd.Stdout = os.Stdout
mw := io.MultiWriter(os.Stderr, &stderr)
cmd.Stderr = mw
outmw := io.MultiWriter(cmd.Stdout, os.Stdout)
cmd.Stdout = outmw
errmw := io.MultiWriter(os.Stderr, &stderr, cmd.Stderr)
cmd.Stderr = errmw
err := cmd.Run()
if err != nil {
return fmt.Errorf("command execution failed: %w, stderr: %s", err, stderr.String())
Expand Down

0 comments on commit 0f82bd9

Please sign in to comment.