go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/cv/internal/tryjob/execute/log.go (about)

     1  // Copyright 2022 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package execute
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  
    21  	"google.golang.org/protobuf/types/known/timestamppb"
    22  
    23  	"go.chromium.org/luci/common/clock"
    24  
    25  	"go.chromium.org/luci/cv/internal/tryjob"
    26  )
    27  
    28  func (e *Executor) logRequirementChanged(ctx context.Context) {
    29  	e.log(&tryjob.ExecutionLogEntry{
    30  		Time: timestamppb.New(clock.Now(ctx).UTC()),
    31  		Kind: &tryjob.ExecutionLogEntry_RequirementChanged_{
    32  			RequirementChanged: &tryjob.ExecutionLogEntry_RequirementChanged{},
    33  		},
    34  	})
    35  }
    36  
    37  func (e *Executor) logTryjobDiscarded(ctx context.Context, def *tryjob.Definition, exec *tryjob.ExecutionState_Execution, reason string) {
    38  	latestAttempt := tryjob.LatestAttempt(exec)
    39  	if latestAttempt != nil {
    40  		e.log(&tryjob.ExecutionLogEntry{
    41  			Time: timestamppb.New(clock.Now(ctx).UTC()),
    42  			Kind: &tryjob.ExecutionLogEntry_TryjobDiscarded_{
    43  				TryjobDiscarded: &tryjob.ExecutionLogEntry_TryjobDiscarded{
    44  					Snapshot: makeLogTryjobSnapshotFromAttempt(def, latestAttempt),
    45  					Reason:   reason,
    46  				},
    47  			},
    48  		})
    49  	}
    50  }
    51  
    52  func (e *Executor) logRetryDenied(ctx context.Context, execState *tryjob.ExecutionState, failedIndices []int, reason string) {
    53  	snapshots := make([]*tryjob.ExecutionLogEntry_TryjobSnapshot, len(failedIndices))
    54  	for i, idx := range failedIndices {
    55  		snapshots[i] = makeLogTryjobSnapshotFromAttempt(
    56  			execState.GetRequirement().GetDefinitions()[idx],
    57  			tryjob.LatestAttempt(execState.GetExecutions()[idx]))
    58  	}
    59  	e.log(&tryjob.ExecutionLogEntry{
    60  		Time: timestamppb.New(clock.Now(ctx).UTC()),
    61  		Kind: &tryjob.ExecutionLogEntry_RetryDenied_{
    62  			RetryDenied: &tryjob.ExecutionLogEntry_RetryDenied{
    63  				Tryjobs: snapshots,
    64  				Reason:  reason,
    65  			},
    66  		},
    67  	})
    68  }
    69  
    70  // log adds a new execution log entry.
    71  func (e *Executor) log(entry *tryjob.ExecutionLogEntry) {
    72  	if entry.GetTime() == nil {
    73  		panic(fmt.Errorf("log entry must provide time; got %s", entry))
    74  	}
    75  	// add the entry at the end first and then move it to the right location to
    76  	// ensure logEntries are ordered from oldest to newest.
    77  	e.logEntries = append(e.logEntries, entry)
    78  	for i := len(e.logEntries) - 1; i > 0; i-- {
    79  		if !e.logEntries[i].GetTime().AsTime().Before(e.logEntries[i-1].GetTime().AsTime()) {
    80  			return // found the right spot.
    81  		}
    82  		e.logEntries[i], e.logEntries[i-1] = e.logEntries[i-1], e.logEntries[i]
    83  	}
    84  }