Skip to content

Commit

Permalink
[supervisor] Better reflect ♻️ incremental prebuilds in prebuild logs
Browse files Browse the repository at this point in the history
  • Loading branch information
jankeromnes committed May 26, 2021
1 parent 1ca6b44 commit d35e81e
Showing 1 changed file with 51 additions and 4 deletions.
55 changes: 51 additions & 4 deletions components/supervisor/pkg/supervisor/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"fmt"
"io"
"os"
"regexp"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -377,9 +378,14 @@ func (tm *tasksManager) watch(task *task, terminal *terminal.Term) {
go func() {
defer stdout.Close()

fileName := tm.prebuildLogFileName(task)
// TODO(janx): If the file already exists (from a parent prebuild), extract its "time saved", and log that below
// (instead, or in addition to, the incremental prebuild time).
var (
fileName = tm.prebuildLogFileName(task)
oldFileName = fileName + "-old"
)
if _, err := os.Stat(fileName); err == nil {
// If the file already exists (from a parent prebuild), temporarily move it so that it doesn't get truncated.
_ = os.Rename(fileName, oldFileName)
}
file, err := os.Create(fileName)
var fileWriter *bufio.Writer
if err != nil {
Expand All @@ -391,12 +397,14 @@ func (tm *tasksManager) watch(task *task, terminal *terminal.Term) {
fileWriter = bufio.NewWriter(file)
defer fileWriter.Flush()
}
// Import any parent prebuild logs and parse their total duration if available
parentElapsed := importParentLogAndGetDuration(oldFileName, fileWriter)

buf := make([]byte, 4096)
for {
n, err := stdout.Read(buf)
if err == io.EOF {
elapsed := time.Since(start)
elapsed := time.Since(start) + parentElapsed
duration := ""
if elapsed >= 1*time.Minute {
elapsedInMinutes := strconv.Itoa(int(elapsed.Minutes()))
Expand Down Expand Up @@ -425,6 +433,45 @@ func (tm *tasksManager) watch(task *task, terminal *terminal.Term) {
}()
}

func importParentLogAndGetDuration(fileName string, fileWriter *bufio.Writer) time.Duration {
if _, err := os.Stat(fileName); err != nil {
return 0
}
defer os.Remove(fileName)

file, err := os.Open(fileName)
if err != nil {
return 0
}
defer file.Close()

defer fileWriter.WriteString("♻️ Re-running this task as an incremental workspace prebuild\n\n")

scanner := bufio.NewScanner(file)
for scanner.Scan() {
l := scanner.Text()
if strings.Contains(l, "🤙 This task ran as a workspace prebuild") {
break
}
fileWriter.WriteString(l + "\n")
}
if !scanner.Scan() {
return 0
}
reg, err := regexp.Compile(`🎉 Well done on saving (\d+) minute`)
if err != nil {
return 0
}
res := reg.FindStringSubmatch(scanner.Text())
if res == nil {
return 0
}
if elapsedInMinutes, err := strconv.Atoi(res[1]); err == nil {
return time.Duration(elapsedInMinutes) * time.Minute
}
return 0
}

type composeCommandOptions struct {
commands []*string
format string
Expand Down

0 comments on commit d35e81e

Please sign in to comment.