go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/analysis/internal/services/buildjoiner/task.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 buildjoiner defines a task to join a completed build with
    16  // the other context (presubmit run and/or ResultDB invocation)
    17  // required for ingestion. The work occurs on a task queue instead of
    18  // directly in the pub/sub handler to allow control over the rate of
    19  // outbound requests to other services.
    20  package buildjoiner
    21  
    22  import (
    23  	"context"
    24  	"fmt"
    25  
    26  	"google.golang.org/protobuf/proto"
    27  
    28  	"go.chromium.org/luci/common/errors"
    29  	"go.chromium.org/luci/server/tq"
    30  
    31  	"go.chromium.org/luci/analysis/internal/ingestion/join"
    32  	"go.chromium.org/luci/analysis/internal/tasks/taskspb"
    33  )
    34  
    35  const (
    36  	joinBuildTaskClass = "join-build"
    37  	joinBuildQueue     = "join-build"
    38  )
    39  
    40  func RegisterTaskClass() {
    41  	// RegisterTaskClass registers the task class for tq dispatcher.
    42  	tq.RegisterTaskClass(tq.TaskClass{
    43  		ID:        joinBuildTaskClass,
    44  		Prototype: &taskspb.JoinBuild{},
    45  		Queue:     joinBuildQueue,
    46  		Kind:      tq.NonTransactional,
    47  		Handler: func(ctx context.Context, payload proto.Message) error {
    48  			task := payload.(*taskspb.JoinBuild)
    49  			_, err := join.JoinBuild(ctx, task.Host, task.Project, task.Id)
    50  			return errors.Annotate(err, "join build %s/%v", task.Host, task.Id).Err()
    51  		},
    52  	})
    53  }
    54  
    55  // Schedule enqueues a task to join the given build.
    56  func Schedule(ctx context.Context, task *taskspb.JoinBuild) error {
    57  	return tq.AddTask(ctx, &tq.Task{
    58  		Title:   fmt.Sprintf("%s-%s-%d", task.Project, task.Host, task.Id),
    59  		Payload: task,
    60  	})
    61  }