Skip to content

Commit b6ce008

Browse files
committed
[supervisor] Better reflect ♻️ incremental prebuilds in prebuild logs
1 parent ebfef31 commit b6ce008

File tree

2 files changed

+68
-3
lines changed

2 files changed

+68
-3
lines changed

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@
22

33
All notable changes to this project will be documented in this file.
44

5+
## June 2021
6+
7+
- Better reflect incremental prebuilds in prebuilt workspace logs ([#4293](https://github.com/gitpod-io/gitpod/pull/4293))
8+
- Run shellcheck against scripts ([#4280](https://github.com/gitpod-io/gitpod/pull/4280))
9+
- Implement new Project and Team DB tables and entities ([#4368](https://github.com/gitpod-io/gitpod/pull/4368))
10+
- On gitpod.io 404 redirect to www.gitpod.io ([#4364](https://github.com/gitpod-io/gitpod/pull/4364))
11+
- Fix disk space leak in ws-manager ([#4388](https://github.com/gitpod-io/gitpod/pull/4388))
12+
- Fix memory leak in ws-manager ([#4384](https://github.com/gitpod-io/gitpod/pull/4384))
13+
- Handle GitHub issues page context URL ([#4370](https://github.com/gitpod-io/gitpod/pull/4370))
14+
- Fix issues blocking SSH from local terminal ([#4358](https://github.com/gitpod-io/gitpod/pull/4358))
15+
- Fix remote tracking branch for issue context ([#4367](https://github.com/gitpod-io/gitpod/pull/4367))
16+
- Fix opening empty repositories ([#4337](https://github.com/gitpod-io/gitpod/pull/4337))
517

618
## May 2021
719

components/supervisor/pkg/supervisor/tasks.go

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"fmt"
1111
"io"
1212
"os"
13+
"regexp"
1314
"strconv"
1415
"strings"
1516
"sync"
@@ -377,9 +378,16 @@ func (tm *tasksManager) watch(task *task, terminal *terminal.Term) {
377378
go func() {
378379
defer stdout.Close()
379380

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).
381+
var (
382+
fileName = tm.prebuildLogFileName(task)
383+
oldFileName = fileName + "-old"
384+
)
385+
if _, err := os.Stat(fileName); err == nil {
386+
// If the file already exists (from a parent prebuild), temporarily move it so that it doesn't get truncated.
387+
// On the off chance that renaming fails here, we silently ignore that -- the new prebuild logs simply won't reflect
388+
// the older logs and elapsed time (`importParentLogAndGetDuration` is always safe thanks to its initial `os.Stat`).
389+
_ = os.Rename(fileName, oldFileName)
390+
}
383391
file, err := os.Create(fileName)
384392
var fileWriter *bufio.Writer
385393
if err != nil {
@@ -391,12 +399,17 @@ func (tm *tasksManager) watch(task *task, terminal *terminal.Term) {
391399
fileWriter = bufio.NewWriter(file)
392400
defer fileWriter.Flush()
393401
}
402+
// Import any parent prebuild logs and parse their total duration if available
403+
parentElapsed := importParentLogAndGetDuration(oldFileName, fileWriter)
394404

395405
buf := make([]byte, 4096)
396406
for {
397407
n, err := stdout.Read(buf)
398408
if err == io.EOF {
399409
elapsed := time.Since(start)
410+
if parentElapsed > elapsed {
411+
elapsed = parentElapsed
412+
}
400413
duration := ""
401414
if elapsed >= 1*time.Minute {
402415
elapsedInMinutes := strconv.Itoa(int(elapsed.Minutes()))
@@ -425,6 +438,46 @@ func (tm *tasksManager) watch(task *task, terminal *terminal.Term) {
425438
}()
426439
}
427440

441+
func importParentLogAndGetDuration(fn string, out io.Writer) time.Duration {
442+
if _, err := os.Stat(fn); err != nil {
443+
return 0
444+
}
445+
defer os.Remove(fn)
446+
447+
file, err := os.Open(fn)
448+
if err != nil {
449+
return 0
450+
}
451+
defer file.Close()
452+
453+
defer out.Write([]byte("♻️ Re-running task as an incremental workspace prebuild\n\n"))
454+
455+
scanner := bufio.NewScanner(file)
456+
for scanner.Scan() {
457+
l := scanner.Text()
458+
if strings.Contains(l, "🤙 This task ran as a workspace prebuild") {
459+
break
460+
}
461+
out.Write([]byte(l + "\n"))
462+
}
463+
if !scanner.Scan() {
464+
return 0
465+
}
466+
reg, err := regexp.Compile(`🎉 Well done on saving (\d+) minute`)
467+
if err != nil {
468+
return 0
469+
}
470+
res := reg.FindStringSubmatch(scanner.Text())
471+
if res == nil {
472+
return 0
473+
}
474+
elapsedInMinutes, err := strconv.Atoi(res[1])
475+
if err != nil {
476+
return 0
477+
}
478+
return time.Duration(elapsedInMinutes) * time.Minute
479+
}
480+
428481
type composeCommandOptions struct {
429482
commands []*string
430483
format string

0 commit comments

Comments
 (0)