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 25, 2021
1 parent 0878c94 commit 7d82037
Showing 1 changed file with 29 additions and 4 deletions.
33 changes: 29 additions & 4 deletions components/supervisor/pkg/supervisor/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"os"
"regexp"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -377,9 +379,29 @@ 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)
parentLog = ""
parentElapsed time.Duration = 0
)
if _, err := os.Stat(fileName); err == nil {
// If the file already exists (from a parent prebuild), extract its "elapsed time" before overwriting the prebuild logs.
content, err := ioutil.ReadFile(fileName)
if err == nil {
parentLog = string(content)
reg, err := regexp.Compile(`\n🤙 This task ran as a workspace prebuild\n(🎉 Well done on saving (\d+) minutes?\n)?\n`)
if err == nil {
res := reg.FindStringSubmatch(parentLog)
if res != nil {
parentLog = reg.ReplaceAllString(parentLog, "\n♻️ Re-running this task as an incremental workspace prebuild\n")
parentElapsedInMinutes, err := strconv.Atoi(res[2])
if err == nil {
parentElapsed = time.Duration(parentElapsedInMinutes) * time.Minute
}
}
}
}
}
file, err := os.Create(fileName)
var fileWriter *bufio.Writer
if err != nil {
Expand All @@ -391,12 +413,15 @@ func (tm *tasksManager) watch(task *task, terminal *terminal.Term) {
fileWriter = bufio.NewWriter(file)
defer fileWriter.Flush()
}
if parentLog != "" {
fileWriter.WriteString(parentLog + "\n")
}

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

0 comments on commit 7d82037

Please sign in to comment.