go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/buildbucket/appengine/tasks/cancel_backend.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 tasks
    16  
    17  import (
    18  	"context"
    19  
    20  	"google.golang.org/api/googleapi"
    21  
    22  	"go.chromium.org/luci/common/errors"
    23  	"go.chromium.org/luci/common/retry/transient"
    24  	"go.chromium.org/luci/server/tq"
    25  
    26  	"go.chromium.org/luci/buildbucket/appengine/internal/clients"
    27  	"go.chromium.org/luci/buildbucket/appengine/internal/config"
    28  
    29  	pb "go.chromium.org/luci/buildbucket/proto"
    30  )
    31  
    32  func HandleCancelBackendTask(ctx context.Context, project, target, taskID string) error {
    33  	// Send the cancelation request
    34  	globalCfg, err := config.GetSettingsCfg(ctx)
    35  	if err != nil {
    36  		return errors.Annotate(err, "could not get global settings config").Err()
    37  	}
    38  	backendClient, err := clients.NewBackendClient(ctx, project, target, globalCfg)
    39  	if err != nil {
    40  		return tq.Fatal.Apply(errors.Annotate(err, "failed to connect to backend service").Err())
    41  	}
    42  	_, err = backendClient.CancelTasks(ctx, &pb.CancelTasksRequest{
    43  		TaskIds: []*pb.TaskID{
    44  			&pb.TaskID{
    45  				Id:     taskID,
    46  				Target: target,
    47  			},
    48  		},
    49  	})
    50  	if err != nil {
    51  		if apiErr, ok := err.(*googleapi.Error); ok && apiErr.Code >= 500 {
    52  			return errors.Annotate(err, "transient error in canceling task %s for target %s", taskID, target).Tag(transient.Tag).Err()
    53  		}
    54  		return errors.Annotate(err, "fatal error in cancelling task %s for target %s", taskID, target).Tag(tq.Fatal).Err()
    55  	}
    56  	return nil
    57  }