Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 408e21e

Browse files
Merge pull request #10769 from cli/babakks/fix-job-log-resolution
Fix job log resolution to skip legacy logs in favour of normal/new ones
2 parents 2a26781 + f337ce9 commit 408e21e

File tree

3 files changed

+42
-17
lines changed

3 files changed

+42
-17
lines changed
498 Bytes
Binary file not shown.

pkg/cmd/run/view/view.go

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -555,15 +555,25 @@ func getJobNameForLogFilename(name string) string {
555555
return sanitizedJobName
556556
}
557557

558+
// A job run log file is a top-level .txt file whose name starts with an ordinal
559+
// number; e.g., "0_jobname.txt".
558560
func jobLogFilenameRegexp(job shared.Job) *regexp.Regexp {
559561
sanitizedJobName := getJobNameForLogFilename(job.Name)
560-
re := fmt.Sprintf(`^-?\d+_%s\.txt`, regexp.QuoteMeta(sanitizedJobName))
562+
re := fmt.Sprintf(`^\d+_%s\.txt$`, regexp.QuoteMeta(sanitizedJobName))
563+
return regexp.MustCompile(re)
564+
}
565+
566+
// A legacy job run log file is a top-level .txt file whose name starts with a
567+
// negative number which is the ID of the run; e.g., "-2147483648_jobname.txt".
568+
func legacyJobLogFilenameRegexp(job shared.Job) *regexp.Regexp {
569+
sanitizedJobName := getJobNameForLogFilename(job.Name)
570+
re := fmt.Sprintf(`^-\d+_%s\.txt$`, regexp.QuoteMeta(sanitizedJobName))
561571
return regexp.MustCompile(re)
562572
}
563573

564574
func stepLogFilenameRegexp(job shared.Job, step shared.Step) *regexp.Regexp {
565575
sanitizedJobName := getJobNameForLogFilename(job.Name)
566-
re := fmt.Sprintf(`^%s\/%d_.*\.txt`, regexp.QuoteMeta(sanitizedJobName), step.Number)
576+
re := fmt.Sprintf(`^%s\/%d_.*\.txt$`, regexp.QuoteMeta(sanitizedJobName), step.Number)
567577
return regexp.MustCompile(re)
568578
}
569579

@@ -662,24 +672,29 @@ func truncateAsUTF16(str string, max int) string {
662672
// where the ID can apparently be negative.
663673
func attachRunLog(rlz *zip.Reader, jobs []shared.Job) {
664674
for i, job := range jobs {
665-
re := jobLogFilenameRegexp(job)
666-
for _, file := range rlz.File {
667-
if re.MatchString(file.Name) {
668-
jobs[i].Log = file
669-
break
670-
}
675+
// As a highest priority, we try to use the step logs first. We have seen zips that surprisingly contain
676+
// step logs, normal job logs and legacy job logs. In this case, both job logs would be ignored. We have
677+
// never seen a zip containing both job logs and no step logs, however, it may be possible. In that case
678+
// let's prioritise the normal log over the legacy one.
679+
jobLog := matchFileInZIPArchive(rlz, jobLogFilenameRegexp(job))
680+
if jobLog == nil {
681+
jobLog = matchFileInZIPArchive(rlz, legacyJobLogFilenameRegexp(job))
671682
}
683+
jobs[i].Log = jobLog
672684

673685
for j, step := range job.Steps {
674-
re := stepLogFilenameRegexp(job, step)
675-
for _, file := range rlz.File {
676-
if re.MatchString(file.Name) {
677-
jobs[i].Steps[j].Log = file
678-
break
679-
}
680-
}
686+
jobs[i].Steps[j].Log = matchFileInZIPArchive(rlz, stepLogFilenameRegexp(job, step))
687+
}
688+
}
689+
}
690+
691+
func matchFileInZIPArchive(zr *zip.Reader, re *regexp.Regexp) *zip.File {
692+
for _, file := range zr.File {
693+
if re.MatchString(file.Name) {
694+
return file
681695
}
682696
}
697+
return nil
683698
}
684699

685700
func displayRunLog(w io.Writer, jobs []shared.Job, failed bool) error {

pkg/cmd/run/view/view_test.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2039,8 +2039,9 @@ func TestViewRun(t *testing.T) {
20392039
// ├── 2_cool job with no step logs.txt
20402040
// ├── 3_sad job with no step logs.txt
20412041
// ├── -9999999999_legacy cool job with no step logs.txt
2042-
// └── -9999999999_legacy sad job with no step logs.txt
2043-
2042+
// ├── -9999999999_legacy sad job with no step logs.txt
2043+
// ├── 4_cool job with both legacy and new logs.txt
2044+
// └── -9999999999_cool job with both legacy and new logs.txt
20442045
func Test_attachRunLog(t *testing.T) {
20452046
tests := []struct {
20462047
name string
@@ -2149,6 +2150,15 @@ func Test_attachRunLog(t *testing.T) {
21492150
wantJobFilename: "-9999999999_legacy cool job with no step logs.txt",
21502151
wantStepMatch: false,
21512152
},
2153+
{
2154+
name: "matching job name with both normal and legacy filename",
2155+
job: shared.Job{
2156+
Name: "cool job with both legacy and new logs",
2157+
},
2158+
wantJobMatch: true,
2159+
wantJobFilename: "4_cool job with both legacy and new logs.txt",
2160+
wantStepMatch: false,
2161+
},
21522162
{
21532163
name: "one job name is a suffix of another",
21542164
job: shared.Job{

0 commit comments

Comments
 (0)