Skip to content

Commit

Permalink
feat: add cron job for relayer flush (#954)
Browse files Browse the repository at this point in the history
  • Loading branch information
artemijspavlovs authored Sep 16, 2024
1 parent d3c1705 commit 24d39dd
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
21 changes: 18 additions & 3 deletions cmd/services/load/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@ import (
"strings"
"text/template"

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

"github.com/dymensionxyz/roller/cmd/consts"
"github.com/dymensionxyz/roller/cmd/utils"
datalayer "github.com/dymensionxyz/roller/data_layer"
"github.com/dymensionxyz/roller/utils/bash"
"github.com/dymensionxyz/roller/utils/config/cronjobs"
"github.com/dymensionxyz/roller/utils/config/tomlconfig"
"github.com/dymensionxyz/roller/utils/errorhandling"
"github.com/dymensionxyz/roller/utils/filesystem"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
)

type Service struct {
Expand Down Expand Up @@ -110,7 +112,6 @@ func Cmd(services []string, module string) *cobra.Command {
"💈 Services %s been loaded successfully.\n",
strings.Join(services, ", "),
)

} else {
pterm.Info.Printf(
"the %s commands currently support only darwin and linux operating systems",
Expand All @@ -119,6 +120,20 @@ func Cmd(services []string, module string) *cobra.Command {
return
}

if module == "relayer" {
schedule := "*/15 * * * *" // Run every hour
command := fmt.Sprintf(
"%s tx flush hub-rollapp --max-msgs 100",
consts.Executables.Relayer,
)

err := cronjobs.Add(schedule, command)
if err != nil {
pterm.Error.Println("failed to add flush cronjob", err)
return
}
}

pterm.Info.Println("next steps:")
pterm.Info.Printf(
"run %s to start %s on your local machine\n",
Expand Down
23 changes: 23 additions & 0 deletions utils/config/cronjobs/cronjobs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package cronjobs

import (
"fmt"
"os/exec"
"strings"
)

func Add(schedule, command string) error {
cronEntry := fmt.Sprintf("%s %s\n", schedule, command)

getCurrentCmd := exec.Command("crontab", "-l")
currentCrontab, err := getCurrentCmd.Output()
if err != nil {
currentCrontab = []byte{}
}

newCrontab := string(currentCrontab) + cronEntry

setCrontabCmd := exec.Command("crontab", "-")
setCrontabCmd.Stdin = strings.NewReader(newCrontab)
return setCrontabCmd.Run()
}

0 comments on commit 24d39dd

Please sign in to comment.