Skip to content
This repository has been archived by the owner on Dec 5, 2024. It is now read-only.

Commit

Permalink
Fix bufio.Scanner: token too long error in bootstrap (issue IMCO-1219)
Browse files Browse the repository at this point in the history
  • Loading branch information
pbanos committed Jul 29, 2022
1 parent 9aa6307 commit c048966
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
10 changes: 5 additions & 5 deletions bootstrapping/bootstrapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const (
defaultSplaySeconds = 300 // 300 seconds = 5 minutes
// defaultThresholdLines is the default maximum number of lines a
// batch of policyfile application output report can have
defaultThresholdLines = 10
defaultThresholdLines = 20
// defaultApplyAfterIterations is the default number of iterations
// the continuous bootstrap command can go without attempting to apply
// policyfiles
Expand Down Expand Up @@ -225,19 +225,19 @@ func runBootstrapPeriodically(ctx context.Context, c *cli.Context, formatter for
}
} else {
log.Info(
"Configuration has not changed since last successful application and not %d iterations have passed since last application",
applyAfterIterations)
fmt.Sprintf("Configuration has not changed since last successful application and not %d iterations have passed since last application",
applyAfterIterations))
}
} else {
log.Info("Error ocurred while retrieving configuration to apply: %v", err)
log.Info(fmt.Sprintf("Error ocurred while retrieving configuration to apply: %v", err))
}
noPolicyfileApplicationIterations++

// Sleep for a configured amount of time plus a random amount of time (10 minutes plus 0 to 5 minutes, for
// instance)
sleepSeconds := interval + r.Intn(int(splay))
ticker := time.NewTicker(time.Duration(sleepSeconds) * time.Second)
log.Info("Sleeping for %d seconds before checking for updates or reattempting", sleepSeconds)
log.Info(fmt.Sprintf("Sleeping for %d seconds before checking for updates or reattempting", sleepSeconds))
select {
case <-ticker.C:
log.Debug("ticker")
Expand Down
21 changes: 15 additions & 6 deletions utils/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,12 +282,21 @@ func RunContinuousCmd(fn func(chunk string) error, command string, thresholdTime
chunk := ""
nLines, nTime := 0, 0
timeStart := time.Now()

scanner := bufio.NewScanner(bufio.NewReader(stdout))
for scanner.Scan() {
chunk = strings.Join([]string{chunk, scanner.Text(), "\n"}, "")
reader := bufio.NewReader(stdout)
line, incomplete, err := reader.ReadLine()
for ; err == nil; line, incomplete, err = reader.ReadLine() {
eol := "\n"
if incomplete {
eol = "[...]\n"
}
chunk = strings.Join([]string{chunk, string(line), eol}, "")
if incomplete {
for incomplete && err == nil {
_, incomplete, err = reader.ReadLine()
}
}
nLines++
nTime = int(time.Now().Sub(timeStart).Seconds())
nTime = int(time.Since(timeStart).Seconds())
if (thresholdTime > 0 && nTime >= thresholdTime) || (thresholdLines > 0 && nLines >= thresholdLines) {
if err := fn(chunk); err == nil {
chunk = ""
Expand All @@ -297,7 +306,7 @@ func RunContinuousCmd(fn func(chunk string) error, command string, thresholdTime
}
}

if err := scanner.Err(); err != nil {
if err != nil && err != io.EOF {
log.Error("==> Error: ", err.Error())
chunk = strings.Join([]string{chunk, err.Error()}, "")
}
Expand Down

0 comments on commit c048966

Please sign in to comment.