go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/cv/internal/run/impl/handler/post_action.go (about)

     1  // Copyright 2023 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 handler
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"time"
    21  
    22  	"google.golang.org/protobuf/types/known/timestamppb"
    23  
    24  	"go.chromium.org/luci/common/clock"
    25  
    26  	"go.chromium.org/luci/cv/internal/configs/prjcfg"
    27  	"go.chromium.org/luci/cv/internal/run"
    28  	"go.chromium.org/luci/cv/internal/run/eventpb"
    29  	"go.chromium.org/luci/cv/internal/run/impl/state"
    30  	"go.chromium.org/luci/cv/internal/run/postaction"
    31  )
    32  
    33  const maxPostActionExecutionDuration = 8 * time.Minute
    34  
    35  func (impl *Impl) onCompletedPostAction(ctx context.Context, rs *state.RunState, op *run.OngoingLongOps_Op, opResult *eventpb.LongOpCompleted) (*Result, error) {
    36  	opID := opResult.GetOperationId()
    37  	rs = rs.ShallowCopy()
    38  	rs.RemoveCompletedLongOp(opID)
    39  	summary := opResult.GetExecutePostAction().GetSummary()
    40  	label := fmt.Sprintf("PostAction[%s]", op.GetExecutePostAction().GetName())
    41  
    42  	switch st := opResult.GetStatus(); {
    43  	// If there is a summary reported by the executor, add it to the run log,
    44  	// regardless of the stauts. For example, the op could be EXPIRED after
    45  	// a number of votes succeeded.
    46  	case len(summary) > 0:
    47  		rs.LogInfo(ctx, label, summary)
    48  
    49  	// If len(summary) == 0, it implies that the long op was completed before
    50  	// the op was dispatched to the PostAction execution handler.
    51  	//
    52  	// The PostAction handler should set Summary if postaction.Executor.Do()
    53  	// is called.
    54  	case st == eventpb.LongOpCompleted_EXPIRED:
    55  		rs.LogInfo(ctx, label, "the execution deadline was exceeded")
    56  	case st == eventpb.LongOpCompleted_FAILED:
    57  		rs.LogInfo(ctx, label, "the execution failed")
    58  	case st == eventpb.LongOpCompleted_CANCELLED:
    59  		rs.LogInfo(ctx, label, "the execution was cancelled")
    60  	case st == eventpb.LongOpCompleted_SUCCEEDED:
    61  		rs.LogInfo(ctx, label, "the execution succeeded")
    62  	default:
    63  		panic(fmt.Errorf("unexpected LongOpCompleted status: %s", st))
    64  	}
    65  	return &Result{State: rs}, nil
    66  }
    67  
    68  // enqueueExecutePostActionTask enqueues internal long ops for post action and
    69  // the post action defined in the config, of which triggering conditions are
    70  // met.
    71  //
    72  // The payload is made of the project config associated with the Run
    73  // at the time of this call. Even if a new project config is pushed with new
    74  // PostAction configs, the changes won't affect the ongoing PostActions.
    75  //
    76  // PostActions are executed for ended Runs. New config changes have no impact
    77  // on ended Runs, and new project configs with PostAction shouldn't affect
    78  // already ended Runs and their ongoing PostActions, either.
    79  func enqueueExecutePostActionTask(ctx context.Context, rs *state.RunState, cg *prjcfg.ConfigGroup) {
    80  	// enqueue internal post actions.
    81  	rs.EnqueueLongOp(&run.OngoingLongOps_Op{
    82  		Deadline: timestamppb.New(clock.Now(ctx).UTC().Add(maxPostActionExecutionDuration)),
    83  		Work: &run.OngoingLongOps_Op_ExecutePostAction{
    84  			ExecutePostAction: &run.OngoingLongOps_Op_ExecutePostActionPayload{
    85  				Name: postaction.CreditRunQuotaPostActionName,
    86  				Kind: &run.OngoingLongOps_Op_ExecutePostActionPayload_CreditRunQuota_{
    87  					CreditRunQuota: &run.OngoingLongOps_Op_ExecutePostActionPayload_CreditRunQuota{},
    88  				},
    89  			},
    90  		},
    91  	})
    92  	// enqueue actions defined in the configuration.
    93  	for _, pacfg := range cg.Content.GetPostActions() {
    94  		if postaction.IsTriggeringConditionMet(pacfg, &rs.Run) {
    95  			rs.EnqueueLongOp(&run.OngoingLongOps_Op{
    96  				Deadline: timestamppb.New(clock.Now(ctx).UTC().Add(maxPostActionExecutionDuration)),
    97  				Work: &run.OngoingLongOps_Op_ExecutePostAction{
    98  					ExecutePostAction: &run.OngoingLongOps_Op_ExecutePostActionPayload{
    99  						Name: pacfg.GetName(),
   100  						Kind: &run.OngoingLongOps_Op_ExecutePostActionPayload_ConfigAction{
   101  							ConfigAction: pacfg,
   102  						},
   103  					},
   104  				},
   105  			})
   106  		}
   107  	}
   108  }