Skip to content

Commit

Permalink
feat: add concurrent run staker
Browse files Browse the repository at this point in the history
  • Loading branch information
Ja7ad committed Mar 2, 2024
1 parent 72b6cee commit 92ece97
Showing 1 changed file with 39 additions and 3 deletions.
42 changes: 39 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package main

import (
"context"
"encoding/json"
"flag"
"log"
"os"
"os/exec"
"os/signal"
"strconv"
"sync"
"syscall"
)

type Cfg struct {
Expand Down Expand Up @@ -41,6 +45,14 @@ func main() {

amount := strconv.FormatFloat(cfg.Amount, 'g', -1, 64)

var wg sync.WaitGroup
ctx, cancel := context.WithCancel(context.Background())

defer func() {
log.Println("Exiting...")
os.Exit(0)
}()

for _, val := range cfg.Validators {
args := make([]string, 0)
args = append(args, "--path", cfg.WalletPath, "tx", "bond")
Expand All @@ -55,11 +67,35 @@ func main() {
args = append(args, "--server", *rpc)
}

out, err := exec.Command(cfg.PactusWalletExecPath, args...).Output()
wg.Add(1)
go runCmd(ctx, cfg.PactusWalletExecPath, val.Address, &wg, args...)
}

go func() {
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt, syscall.SIGTERM)

select {
case s := <-interrupt:
cancel()
log.Printf("task canceled by user, %s", s.String())
}
}()

wg.Wait()
}

func runCmd(ctx context.Context, pactusWalletExecPath, validator string, wg *sync.WaitGroup, args ...string) {
defer wg.Done()

select {
case <-ctx.Done():
return
default:
out, err := exec.CommandContext(ctx, pactusWalletExecPath, args...).Output()
if err != nil {
log.Fatalf("err: %s, msg: %s", err.Error(), string(out))
log.Printf("validator: %s err: %s, msg: %s", validator, err.Error(), string(out))
}
log.Println(string(out))
}

}

0 comments on commit 92ece97

Please sign in to comment.