Skip to content

Commit 08596a8

Browse files
committed
script: Fix lack of newline on retried command in logs
and show the amount of time it took until retries succeeded. Before: scripttest.go:72: # Test command retrying and the flag parsing when retrying. # The 'retrytest' command should run two times and the flags # should not be lost.> * retrytest --not-empty=xyz abc scripttest.go:72: (0.010s) (command "* retrytest --not-empty=xyz abc" failed, retrying in 10ms...) (command "* retrytest --not-empty=xyz abc" succeeded after 1 retries) --- PASS: TestAll/retry (0.01s) After: scripttest.go:72: # Test command retrying and the flag parsing when retrying. # The 'retrytest' command should run two times and the flags # should not be lost. > * retrytest --not-empty=xyz abc retrytest called scripttest.go:72: (command "* retrytest --not-empty=xyz abc" failed, retrying in 10ms...) retrytest called scripttest.go:72: (command "* retrytest --not-empty=xyz abc" succeeded after 1 retries in 0.010s) --- PASS: TestAll/retry (0.01s) Signed-off-by: Jussi Maki <[email protected]>
1 parent 234bb76 commit 08596a8

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

Diff for: script/engine.go

+13-3
Original file line numberDiff line numberDiff line change
@@ -341,12 +341,20 @@ func (e *Engine) Execute(s *State, file string, script *bufio.Reader, log io.Wri
341341
err = e.runCommand(s, cmd, impl)
342342
if err != nil {
343343
if cmd.want == successRetryOnFailure || cmd.want == failureRetryOnSuccess {
344+
retryStart := sectionStart
345+
346+
// Clear the section start to avoid the deferred endSection() printing a timestamp.
347+
sectionStart = time.Time{}
348+
349+
// Append the new line that section start omitted and flush the log.
350+
io.WriteString(log, "\n")
351+
s.FlushLog()
352+
344353
// Command wants retries. Retry the whole section
345354
backoff := exponentialBackoff{max: maxRetryInterval, interval: retryInterval}
346355
for err != nil {
347-
s.FlushLog()
348356
retryDuration := backoff.get()
349-
s.Logf("(command %q failed, retrying in %s...)\n", line, retryDuration)
357+
fmt.Fprintf(log, "(command %q failed, retrying in %s...)\n", line, retryDuration)
350358
select {
351359
case <-s.Context().Done():
352360
s.RetryCount = 0
@@ -360,9 +368,11 @@ func (e *Engine) Execute(s *State, file string, script *bufio.Reader, log io.Wri
360368
break
361369
}
362370
}
371+
s.FlushLog()
363372
}
364-
s.Logf("(command %q succeeded after %d retries)\n", line, s.RetryCount)
373+
fmt.Fprintf(log, "(command %q succeeded after %d retries in %.3fs)\n", line, s.RetryCount, time.Since(retryStart).Seconds())
365374
s.RetryCount = 0
375+
return nil
366376
} else {
367377
if stop := (stopError{}); errors.As(err, &stop) {
368378
// Since the 'stop' command halts execution of the entire script,

Diff for: script/scripttest/scripttest_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ func TestAll(t *testing.T) {
4646
},
4747
},
4848
func(s *script.State, args ...string) (script.WaitFunc, error) {
49+
s.Logf("retrytest called")
4950
notEmpty, err := s.Flags.GetString("not-empty")
5051
if err != nil {
5152
return nil, err

0 commit comments

Comments
 (0)