go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/bisection/culpritverification/task/task.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 task handle task scheduling for culprit verification.
    16  package task
    17  
    18  import (
    19  	"context"
    20  	"fmt"
    21  
    22  	tpb "go.chromium.org/luci/bisection/task/proto"
    23  	"go.chromium.org/luci/server/tq"
    24  	"google.golang.org/protobuf/proto"
    25  )
    26  
    27  // CompileFailureTasks describes how to route compile failure culprit verification tasks.
    28  var CompileFailureTasks = tq.RegisterTaskClass(tq.TaskClass{
    29  	ID:        "culprit-verification",
    30  	Prototype: (*tpb.CulpritVerificationTask)(nil),
    31  	Queue:     "culprit-verification",
    32  	Kind:      tq.NonTransactional,
    33  })
    34  
    35  // TestFailureTasks describes how to route test failure culprit verification tasks.
    36  var TestFailureTasks = tq.RegisterTaskClass(tq.TaskClass{
    37  	ID:        "test-failure-culprit-verification",
    38  	Prototype: (*tpb.TestFailureCulpritVerificationTask)(nil),
    39  	Queue:     "test-failure-culprit-verification",
    40  	Kind:      tq.NonTransactional,
    41  })
    42  
    43  // RegisterTaskClass registers the task class for tq dispatcher
    44  func RegisterTaskClass(compileHandler, testHandler func(ctx context.Context, payload proto.Message) error) {
    45  	if compileHandler != nil {
    46  		CompileFailureTasks.AttachHandler(compileHandler)
    47  	}
    48  	if testHandler != nil {
    49  		TestFailureTasks.AttachHandler(testHandler)
    50  	}
    51  }
    52  
    53  // ScheduleTestFailureTask schedules a task for test failure culprit verification.
    54  func ScheduleTestFailureTask(ctx context.Context, analysisID int64) error {
    55  	return tq.AddTask(ctx, &tq.Task{
    56  		Payload: &tpb.TestFailureCulpritVerificationTask{
    57  			AnalysisId: analysisID,
    58  		},
    59  		Title: fmt.Sprintf("test_failure_culprit_verification_%d", analysisID),
    60  	})
    61  }