doChecks executes command and returns each attempt.
()
| 95 | |
| 96 | // doChecks executes command and returns each attempt. |
| 97 | func (c Checker) doChecks() types.Attempts { |
| 98 | checks := make(types.Attempts, c.Attempts) |
| 99 | for i := 0; i < c.Attempts; i++ { |
| 100 | start := time.Now() |
| 101 | |
| 102 | ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 103 | defer cancel() |
| 104 | |
| 105 | command := exec.CommandContext(ctx, c.Command, c.Arguments...) |
| 106 | output, err := command.CombinedOutput() |
| 107 | |
| 108 | checks[i].RTT = time.Since(start) |
| 109 | |
| 110 | if err != nil { |
| 111 | stringify := func(s string) string { |
| 112 | if strings.TrimSpace(s) == "" { |
| 113 | return "empty" |
| 114 | } |
| 115 | return s |
| 116 | } |
| 117 | checks[i].Error = fmt.Sprintf("Error: %s\nOutput: %s\n", err.Error(), stringify(string(output))) |
| 118 | continue |
| 119 | } |
| 120 | |
| 121 | if err := c.checkDown(string(output)); err != nil { |
| 122 | checks[i].Error = err.Error() |
| 123 | } |
| 124 | |
| 125 | if c.AttemptSpacing > 0 { |
| 126 | time.Sleep(c.AttemptSpacing) |
| 127 | } |
| 128 | } |
| 129 | return checks |
| 130 | } |
| 131 | |
| 132 | // conclude takes the data in result from the attempts and |
| 133 | // computes remaining values needed to fill out the result. |