Skip to content

Commit

Permalink
feat: add logs command for services (#943)
Browse files Browse the repository at this point in the history
  • Loading branch information
artemijspavlovs authored Sep 15, 2024
1 parent 74323ef commit 360644f
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 2 deletions.
5 changes: 4 additions & 1 deletion cmd/eibc/eibc.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package eibc

import (
"github.com/spf13/cobra"

"github.com/dymensionxyz/roller/cmd/eibc/fulfill"
"github.com/dymensionxyz/roller/cmd/eibc/funds"
eibcinit "github.com/dymensionxyz/roller/cmd/eibc/init"
"github.com/dymensionxyz/roller/cmd/eibc/scale"
"github.com/dymensionxyz/roller/cmd/eibc/start"
"github.com/dymensionxyz/roller/cmd/services"
loadservices "github.com/dymensionxyz/roller/cmd/services/load"
logservices "github.com/dymensionxyz/roller/cmd/services/logs"
restartservices "github.com/dymensionxyz/roller/cmd/services/restart"
startservices "github.com/dymensionxyz/roller/cmd/services/start"
stopservices "github.com/dymensionxyz/roller/cmd/services/stop"
"github.com/spf13/cobra"
)

func Cmd() *cobra.Command {
Expand All @@ -33,6 +35,7 @@ func Cmd() *cobra.Command {
startservices.EibcCmd(),
restartservices.Cmd(sl),
stopservices.Cmd(sl),
logservices.EibcCmd(),
),
)

Expand Down
2 changes: 2 additions & 0 deletions cmd/relayer/relayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/dymensionxyz/roller/cmd/relayer/status"
"github.com/dymensionxyz/roller/cmd/services"
loadservices "github.com/dymensionxyz/roller/cmd/services/load"
logservices "github.com/dymensionxyz/roller/cmd/services/logs"
restartservices "github.com/dymensionxyz/roller/cmd/services/restart"
startservices "github.com/dymensionxyz/roller/cmd/services/start"
stopservices "github.com/dymensionxyz/roller/cmd/services/stop"
Expand All @@ -29,6 +30,7 @@ func Cmd() *cobra.Command {
startservices.RelayerCmd(),
restartservices.Cmd(sl),
stopservices.Cmd(sl),
logservices.RelayerCmd(),
),
)

Expand Down
2 changes: 2 additions & 0 deletions cmd/rollapp/rollapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/dymensionxyz/roller/cmd/rollapp/status"
"github.com/dymensionxyz/roller/cmd/services"
loadservices "github.com/dymensionxyz/roller/cmd/services/load"
logservices "github.com/dymensionxyz/roller/cmd/services/logs"
restartservices "github.com/dymensionxyz/roller/cmd/services/restart"
startservices "github.com/dymensionxyz/roller/cmd/services/start"
stopservices "github.com/dymensionxyz/roller/cmd/services/stop"
Expand All @@ -35,6 +36,7 @@ func Cmd() *cobra.Command {
startservices.RollappCmd(),
restartservices.Cmd(sl),
stopservices.Cmd(sl),
logservices.RollappCmd(),
),
)

Expand Down
143 changes: 143 additions & 0 deletions cmd/services/logs/logs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package logs

import (
"fmt"
"os"
"path/filepath"
"time"

"github.com/pterm/pterm"
"github.com/spf13/cobra"

"github.com/dymensionxyz/roller/cmd/consts"
"github.com/dymensionxyz/roller/cmd/utils"
"github.com/dymensionxyz/roller/utils/config/tomlconfig"
"github.com/dymensionxyz/roller/utils/filesystem"
)

// TODO: refactor
func RollappCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "logs",
Short: "Follow the logs for rollapp and da light client",
Run: func(cmd *cobra.Command, args []string) {
home := cmd.Flag(utils.FlagNames.Home).Value.String()

rollerData, err := tomlconfig.LoadRollerConfig(home)
if err != nil {
pterm.Error.Println("failed to load roller config file", err)
return
}

raLogFilePath := filepath.Join(
rollerData.Home,
consts.ConfigDirName.Rollapp,
"rollapp.log",
)

daLogFilePath := filepath.Join(
rollerData.Home,
consts.ConfigDirName.DALightNode,
"light_client.log",
)
pterm.Info.Println("Follow the logs for rollapp: ", raLogFilePath)
pterm.Info.Println("Follow the logs for da light client: ", daLogFilePath)

errChan := make(chan error, 2)
doneChan := make(chan bool)

go func() {
err := filesystem.TailFile(raLogFilePath, "rollapp")
if err != nil {
pterm.Error.Println("failed to tail file", err)
errChan <- fmt.Errorf("failed to tail RA file: %w", err)
return
}
}()
go func() {
err := filesystem.TailFile(daLogFilePath, "da light client")
if err != nil {
pterm.Error.Println("failed to tail file", err)
errChan <- fmt.Errorf("failed to tail DA file: %w", err)
return
}
}()

// Keep the program running
go func() {
time.Sleep(time.Hour) // Adjust this duration as needed
doneChan <- true
}()

select {
case err := <-errChan:
pterm.Error.Println(err)
os.Exit(1)
default:
select {}
}
},
}
return cmd
}

func RelayerCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "logs",
Short: "Follow the logs for relayer",
Run: func(cmd *cobra.Command, args []string) {
home := cmd.Flag(utils.FlagNames.Home).Value.String()

rollerData, err := tomlconfig.LoadRollerConfig(home)
if err != nil {
pterm.Error.Println("failed to load roller config file", err)
return
}

rlyLogFilePath := filepath.Join(
rollerData.Home,
consts.ConfigDirName.Relayer,
"relayer.log",
)

pterm.Info.Println("Follow the logs for da light client: ", rlyLogFilePath)

errChan := make(chan error, 2)
doneChan := make(chan bool)

go func() {
err := filesystem.TailFile(rlyLogFilePath, "relayer")
if err != nil {
pterm.Error.Println("failed to tail file", err)
errChan <- fmt.Errorf("failed to tail RA file: %w", err)
return
}
}()

go func() {
time.Sleep(time.Hour)
doneChan <- true
}()

select {
case err := <-errChan:
pterm.Error.Println(err)
os.Exit(1)
default:
select {}
}
},
}
return cmd
}

func EibcCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "logs",
Short: "Follow the logs for eibc",
Run: func(cmd *cobra.Command, args []string) {
pterm.Info.Println("not implemented")
},
}
return cmd
}
4 changes: 3 additions & 1 deletion cmd/services/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package services

import "github.com/spf13/cobra"

func Cmd(loadCmd, startCmd, restartCmd, stopCmd *cobra.Command) *cobra.Command {
// TODO: use options instead
func Cmd(loadCmd, startCmd, restartCmd, stopCmd, logsCmd *cobra.Command) *cobra.Command {
cmd := &cobra.Command{
Use: "services [command]",
Short: "Commands for managing systemd services.",
Expand All @@ -11,5 +12,6 @@ func Cmd(loadCmd, startCmd, restartCmd, stopCmd *cobra.Command) *cobra.Command {
cmd.AddCommand(startCmd)
cmd.AddCommand(restartCmd)
cmd.AddCommand(stopCmd)
cmd.AddCommand(logsCmd)
return cmd
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ require (
github.com/lib/pq v1.10.9
github.com/manifoldco/promptui v0.9.0
github.com/naoina/toml v0.1.2-0.20170918210437-9fafd6967416
github.com/nxadm/tail v1.4.8
github.com/olekukonko/tablewriter v0.0.5
github.com/pelletier/go-toml v1.9.5
github.com/pelletier/go-toml/v2 v2.1.0
Expand Down Expand Up @@ -219,6 +220,7 @@ require (
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce // indirect
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
nhooyr.io/websocket v1.8.7 // indirect
pgregory.net/rapid v1.1.0 // indirect
sigs.k8s.io/yaml v1.4.0 // indirect
Expand Down
20 changes: 20 additions & 0 deletions utils/filesystem/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"os/user"
"path/filepath"

"github.com/nxadm/tail"
"github.com/pterm/pterm"

"github.com/dymensionxyz/roller/utils/bash"
Expand Down Expand Up @@ -232,3 +233,22 @@ func RemoveFileIfExists(filePath string) error {
}
return nil
}

func TailFile(fp, svcName string) error {
t, err := tail.TailFile(fp, tail.Config{Follow: true, ReOpen: false})
if err != nil {
return fmt.Errorf("failed to tail file: %v", err)
}

infoPrefix := pterm.Info.Prefix
infoPrefix.Text = svcName
cp := pterm.PrefixPrinter{
Prefix: infoPrefix,
}

for line := range t.Lines {
cp.Println(line.Text)
}

return nil
}

0 comments on commit 360644f

Please sign in to comment.