Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[supervisor] Better reflect incremental prebuilds in prebuild logs #4293

Merged
merged 1 commit into from
Jun 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@

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

## June 2021

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

## May 2021

Expand Down
59 changes: 56 additions & 3 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,16 @@ 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.
// On the off chance that renaming fails here, we silently ignore that -- the new prebuild logs simply won't reflect
// the older logs and elapsed time (`importParentLogAndGetDuration` is always safe thanks to its initial `os.Stat`).
_ = os.Rename(fileName, oldFileName)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if the rename fails? Considering the error is ignored here, a comment as to why ignoring it is fine would be handy

}
file, err := os.Create(fileName)
var fileWriter *bufio.Writer
if err != nil {
Expand All @@ -391,12 +399,17 @@ 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)
if parentElapsed > elapsed {
elapsed = parentElapsed
}
duration := ""
if elapsed >= 1*time.Minute {
elapsedInMinutes := strconv.Itoa(int(elapsed.Minutes()))
Expand Down Expand Up @@ -425,6 +438,46 @@ func (tm *tasksManager) watch(task *task, terminal *terminal.Term) {
}()
}

func importParentLogAndGetDuration(fn string, out io.Writer) time.Duration {
if _, err := os.Stat(fn); err != nil {
return 0
}
defer os.Remove(fn)

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

defer out.Write([]byte("♻️ Re-running 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
}
out.Write([]byte(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
}
elapsedInMinutes, err := strconv.Atoi(res[1])
if err != nil {
return 0
}
return time.Duration(elapsedInMinutes) * time.Minute
}

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