Skip to content

Commit 7d82037

Browse files
committed
[supervisor] Better reflect ♻️ incremental prebuilds in prebuild logs
1 parent 0878c94 commit 7d82037

File tree

1 file changed

+29
-4
lines changed
  • components/supervisor/pkg/supervisor

1 file changed

+29
-4
lines changed

components/supervisor/pkg/supervisor/tasks.go

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import (
99
"context"
1010
"fmt"
1111
"io"
12+
"io/ioutil"
1213
"os"
14+
"regexp"
1315
"strconv"
1416
"strings"
1517
"sync"
@@ -377,9 +379,29 @@ func (tm *tasksManager) watch(task *task, terminal *terminal.Term) {
377379
go func() {
378380
defer stdout.Close()
379381

380-
fileName := tm.prebuildLogFileName(task)
381-
// TODO(janx): If the file already exists (from a parent prebuild), extract its "time saved", and log that below
382-
// (instead, or in addition to, the incremental prebuild time).
382+
var (
383+
fileName = tm.prebuildLogFileName(task)
384+
parentLog = ""
385+
parentElapsed time.Duration = 0
386+
)
387+
if _, err := os.Stat(fileName); err == nil {
388+
// If the file already exists (from a parent prebuild), extract its "elapsed time" before overwriting the prebuild logs.
389+
content, err := ioutil.ReadFile(fileName)
390+
if err == nil {
391+
parentLog = string(content)
392+
reg, err := regexp.Compile(`\n🤙 This task ran as a workspace prebuild\n(🎉 Well done on saving (\d+) minutes?\n)?\n`)
393+
if err == nil {
394+
res := reg.FindStringSubmatch(parentLog)
395+
if res != nil {
396+
parentLog = reg.ReplaceAllString(parentLog, "\n♻️ Re-running this task as an incremental workspace prebuild\n")
397+
parentElapsedInMinutes, err := strconv.Atoi(res[2])
398+
if err == nil {
399+
parentElapsed = time.Duration(parentElapsedInMinutes) * time.Minute
400+
}
401+
}
402+
}
403+
}
404+
}
383405
file, err := os.Create(fileName)
384406
var fileWriter *bufio.Writer
385407
if err != nil {
@@ -391,12 +413,15 @@ func (tm *tasksManager) watch(task *task, terminal *terminal.Term) {
391413
fileWriter = bufio.NewWriter(file)
392414
defer fileWriter.Flush()
393415
}
416+
if parentLog != "" {
417+
fileWriter.WriteString(parentLog + "\n")
418+
}
394419

395420
buf := make([]byte, 4096)
396421
for {
397422
n, err := stdout.Read(buf)
398423
if err == io.EOF {
399-
elapsed := time.Since(start)
424+
elapsed := time.Since(start) + parentElapsed
400425
duration := ""
401426
if elapsed >= 1*time.Minute {
402427
elapsedInMinutes := strconv.Itoa(int(elapsed.Minutes()))

0 commit comments

Comments
 (0)