go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/resultdb/internal/services/recorder/finalize_invocation.go (about) 1 // Copyright 2019 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 recorder 16 17 import ( 18 "context" 19 20 "google.golang.org/grpc/codes" 21 22 "go.chromium.org/luci/common/errors" 23 "go.chromium.org/luci/grpc/appstatus" 24 "go.chromium.org/luci/server/span" 25 26 "go.chromium.org/luci/resultdb/internal/invocations" 27 "go.chromium.org/luci/resultdb/internal/tasks" 28 "go.chromium.org/luci/resultdb/pbutil" 29 pb "go.chromium.org/luci/resultdb/proto/v1" 30 ) 31 32 // validateFinalizeInvocationRequest returns a non-nil error if req is determined 33 // to be invalid. 34 func validateFinalizeInvocationRequest(req *pb.FinalizeInvocationRequest) error { 35 if _, err := pbutil.ParseInvocationName(req.Name); err != nil { 36 return errors.Annotate(err, "name").Err() 37 } 38 39 return nil 40 } 41 42 // FinalizeInvocation implements pb.RecorderServer. 43 func (s *recorderServer) FinalizeInvocation(ctx context.Context, in *pb.FinalizeInvocationRequest) (*pb.Invocation, error) { 44 if err := validateFinalizeInvocationRequest(in); err != nil { 45 return nil, appstatus.BadRequest(err) 46 } 47 48 token, err := extractUpdateToken(ctx) 49 if err != nil { 50 return nil, err 51 } 52 53 invID := invocations.MustParseName(in.Name) 54 if err := validateInvocationToken(ctx, token, invID); err != nil { 55 return nil, appstatus.Errorf(codes.PermissionDenied, "invalid update token") 56 } 57 58 var ret *pb.Invocation 59 _, err = span.ReadWriteTransaction(ctx, func(ctx context.Context) error { 60 inv, err := invocations.Read(ctx, invID) 61 if err != nil { 62 return err 63 } 64 ret = inv 65 66 if ret.State != pb.Invocation_ACTIVE { 67 // Idempotent. 68 return nil 69 } 70 71 // Finalize as requested. 72 ret.State = pb.Invocation_FINALIZING 73 tasks.StartInvocationFinalization(ctx, invID, true) 74 return nil 75 }) 76 77 if err != nil { 78 return nil, err 79 } 80 81 return ret, nil 82 }