From 24d39ddecdff423ec6084899f83eb12905a437c2 Mon Sep 17 00:00:00 2001 From: artpav <19916123+artemijspavlovs@users.noreply.github.com> Date: Mon, 16 Sep 2024 21:53:04 +0300 Subject: [PATCH] feat: add cron job for relayer flush (#954) --- cmd/services/load/load.go | 21 ++++++++++++++++++--- utils/config/cronjobs/cronjobs.go | 23 +++++++++++++++++++++++ 2 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 utils/config/cronjobs/cronjobs.go diff --git a/cmd/services/load/load.go b/cmd/services/load/load.go index 5af1d336..1aa0d19a 100644 --- a/cmd/services/load/load.go +++ b/cmd/services/load/load.go @@ -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 { @@ -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", @@ -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", diff --git a/utils/config/cronjobs/cronjobs.go b/utils/config/cronjobs/cronjobs.go new file mode 100644 index 00000000..2566069a --- /dev/null +++ b/utils/config/cronjobs/cronjobs.go @@ -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() +}