cmd/storj-sim: add -failfast

Fail all the processes immediately when one of the processes fails. This
is to make it more obvious that one of them has failed.

To disable failfast, use `-failfast=false`.

Change-Id: I2bbedf12fb653e42739d00273aa9ae515d34eda6
This commit is contained in:
Egon Elbre 2021-01-06 13:25:25 +02:00
parent 1e44be2900
commit b33d7a318e
3 changed files with 27 additions and 11 deletions

View File

@ -25,7 +25,8 @@ type Flags struct {
StorageNodeCount int
Identities int
IsDev bool
IsDev bool
FailFast bool
OnlyEnv bool // only do things necessary for loading env vars
@ -65,6 +66,7 @@ func main() {
rootCmd.PersistentFlags().BoolVarP(&printCommands, "print-commands", "x", false, "print commands as they are run")
rootCmd.PersistentFlags().BoolVarP(&flags.IsDev, "dev", "", true, "use configuration values tuned for development")
rootCmd.PersistentFlags().BoolVarP(&flags.FailFast, "failfast", "", true, "stop all processes when one of the processes fails")
rootCmd.PersistentFlags().StringVarP(&flags.Postgres, "postgres", "", os.Getenv("STORJ_SIM_POSTGRES"), "connection string for postgres (defaults to STORJ_SIM_POSTGRES)")
rootCmd.PersistentFlags().StringVarP(&flags.Redis, "redis", "", os.Getenv("STORJ_SIM_REDIS"), "connection string for redis e.g. 127.0.0.1:6379 (defaults to STORJ_SIM_REDIS)")

View File

@ -169,8 +169,14 @@ func networkTest(flags *Flags, command string, args []string) error {
ctx, cancel := NewCLIContext(context.Background())
var group errgroup.Group
processes.Start(ctx, &group, "run")
var group *errgroup.Group
if processes.FailFast {
group, ctx = errgroup.WithContext(ctx)
} else {
group = &errgroup.Group{}
}
processes.Start(ctx, group, "run")
for _, process := range processes.List {
process.Status.Started.Wait(ctx)
@ -230,7 +236,7 @@ func newNetwork(flags *Flags) (*Processes, error) {
return all
}
processes := NewProcesses(flags.Directory)
processes := NewProcesses(flags.Directory, flags.FailFast)
var host = flags.Host
versioncontrol := processes.New(Info{
@ -644,7 +650,7 @@ func newNetwork(flags *Flags) (*Processes, error) {
}
func identitySetup(network *Processes) (*Processes, error) {
processes := NewProcesses(network.Directory)
processes := NewProcesses(network.Directory, network.FailFast)
for _, process := range network.List {
if process.Info.Executable == "gateway" || process.Info.Executable == "redis-server" {

View File

@ -29,25 +29,33 @@ type Processes struct {
Directory string
List []*Process
FailFast bool
MaxStartupWait time.Duration
}
const storjSimMaxLineLen = 10000
// NewProcesses returns a group of processes.
func NewProcesses(dir string) *Processes {
func NewProcesses(dir string, failfast bool) *Processes {
return &Processes{
Output: NewPrefixWriter("sim", storjSimMaxLineLen, os.Stdout),
Directory: dir,
List: nil,
Output: NewPrefixWriter("sim", storjSimMaxLineLen, os.Stdout),
Directory: dir,
List: nil,
FailFast: failfast,
MaxStartupWait: time.Minute,
}
}
// Exec executes a command on all processes.
func (processes *Processes) Exec(ctx context.Context, command string) error {
var group errgroup.Group
processes.Start(ctx, &group, command)
var group *errgroup.Group
if processes.FailFast {
group, ctx = errgroup.WithContext(ctx)
} else {
group = &errgroup.Group{}
}
processes.Start(ctx, group, command)
return group.Wait()
}