Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

R4R: added slack notification to runsim #4547

Merged
merged 21 commits into from
Jun 26, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
added slack notification to runsim
  • Loading branch information
mircea-c committed Jun 19, 2019
commit fd99a292a4e745904ae6f919b6553c42a41dba83
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ test_sim_benchmark_invariants:

# Don't move it into tools - this will be gone once gaia has moved into the new repo
runsim: $(BINDIR)/runsim
$(BINDIR)/runsim: contrib/runsim/main.go
$(BINDIR)/runsim: contrib/runsim/main.go contrib/runsim/notification.go
go install github.com/cosmos/cosmos-sdk/contrib/runsim

SIM_NUM_BLOCKS ?= 500
Expand Down
53 changes: 43 additions & 10 deletions contrib/runsim/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"os/signal"
"path/filepath"
"runtime"
"strconv"
"strings"
"sync"
"syscall"
Expand All @@ -37,13 +38,19 @@ var (
results chan bool

// command line arguments and options
jobs = runtime.GOMAXPROCS(0)
pkgName string
blocks string
period string
testname string
genesis string
exitOnFail bool
jobs = runtime.GOMAXPROCS(0)
pkgName string
blocks string
period string
testname string
genesis string
slackToken string
slackChannel string
slackThread string
seedsStrRepr string
exitOnFail bool
gitReport bool
slackReport bool

// logs temporary directory
tempdir string
Expand All @@ -59,10 +66,12 @@ func init() {
flag.IntVar(&jobs, "j", jobs, "Number of parallel processes")
flag.StringVar(&genesis, "g", "", "Genesis file")
flag.BoolVar(&exitOnFail, "e", false, "Exit on fail during multi-sim, print error")
flag.BoolVar(&gitReport, "p", false, "Report results to git check")
mircea-c marked this conversation as resolved.
Show resolved Hide resolved
flag.BoolVar(&slackReport, "s", false, "Report results to slack channel")

flag.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(),
`Usage: %s [-j maxprocs] [-g genesis.json] [-e] [package] [blocks] [period] [testname]
`Usage: %s [-j maxprocs] [-g genesis.json] [-e] [-s] [-p] [package] [blocks] [period] [testname] [slacktoken] [channel] [thread] [seeds]
Run simulations in parallel

`, filepath.Base(os.Args[0]))
Expand All @@ -74,11 +83,19 @@ func main() {
var err error

flag.Parse()
if flag.NArg() != 4 {
if flag.NArg() < 8 && (gitReport || slackReport) {
log.Fatal("wrong number of arguments")
} else if !(gitReport || slackReport) && flag.NArg() != 4 {
log.Fatal("wrong number of arguments")
}

// prepare input channel
if gitReport || slackReport {
slackToken = flag.Arg(4)
slackChannel = flag.Arg(5)
slackThread = flag.Arg(6)
seeds, seedsStrRepr = makeSeedList(flag.Args()[7:])
}
queue := make(chan int, len(seeds))
for _, seed := range seeds {
queue <- seed
Expand Down Expand Up @@ -155,7 +172,9 @@ wait:
os.Exit(1)
}
}

if slackReport {
slackMessage(slackToken, slackChannel, &slackThread, "Finished running simulation for seeds: "+seedsStrRepr)
}
os.Exit(0)
}

Expand All @@ -182,6 +201,9 @@ func worker(id int, seeds <-chan int) {
results <- false
log.Printf("[W%d] Seed %d: FAILED", id, seed)
log.Printf("To reproduce run: %s", buildCommand(testname, blocks, period, genesis, seed))
if slackReport {
slackMessage(slackToken, slackChannel, nil, "Seed "+strconv.Itoa(seed)+" failed. To reproduce, run: "+buildCommand(testname, blocks, period, genesis, seed))
}
if exitOnFail {
log.Printf("\bERROR OUTPUT \n\n%s", err)
panic("halting simulations")
Expand All @@ -190,6 +212,7 @@ func worker(id int, seeds <-chan int) {
log.Printf("[W%d] Seed %d: OK", id, seed)
}
}

log.Printf("[W%d] no seeds left, shutting down", id)
}

Expand Down Expand Up @@ -262,3 +285,13 @@ func checkSignal(proc *os.Process, signal syscall.Signal) {
log.Printf("Failed to send %s to PID %d", signal, proc.Pid)
}
}

func makeSeedList(args []string) ([]int, string) {
seeds = make([]int, len(args))
var seedsStrRepr string
for i := range seeds {
seeds[i], _ = strconv.Atoi(args[i])
seedsStrRepr = seedsStrRepr + args[i] + " "
}
return seeds, seedsStrRepr
}
22 changes: 22 additions & 0 deletions contrib/runsim/notification.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package main

import (
"github.com/nlopes/slack"
"log"
)

func slackMessage(token string, channel string, threadTS *string, message string) {
client := slack.New(token)
if threadTS != nil {
_, _, err := client.PostMessage(channel, slack.MsgOptionText(message, false), slack.MsgOptionTS(*threadTS))
if err != nil {
log.Printf("ERROR: %v", err)
}
} else {
_, _, err := client.PostMessage(channel, slack.MsgOptionText(message, false))
if err != nil {
log.Printf("ERROR: %v", err)
}
}

}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ require (
github.com/magiconair/properties v1.8.0 // indirect
github.com/mattn/go-isatty v0.0.6
github.com/mitchellh/mapstructure v1.1.2 // indirect
github.com/nlopes/slack v0.5.0
github.com/pelletier/go-toml v1.2.0
github.com/pkg/errors v0.8.1
github.com/prometheus/client_golang v0.9.2 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5
github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/nlopes/slack v0.5.0 h1:NbIae8Kd0NpqaEI3iUrsuS0KbcEDhzhc939jLW5fNm0=
github.com/nlopes/slack v0.5.0/go.mod h1:jVI4BBK3lSktibKahxBF74txcK2vyvkza1z/+rRnVAM=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.7.0 h1:WSHQ+IS43OoUrWtD1/bbclrwK8TTH5hzp+umCiuxHgs=
github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
Expand Down