go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/resultdb/internal/tasks/finalization.go (about)

     1  // Copyright 2020 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  	"go.chromium.org/luci/server/span"
    21  	"go.chromium.org/luci/server/tq"
    22  
    23  	"go.chromium.org/luci/resultdb/internal/invocations"
    24  	"go.chromium.org/luci/resultdb/internal/spanutil"
    25  	"go.chromium.org/luci/resultdb/internal/tasks/taskspb"
    26  	pb "go.chromium.org/luci/resultdb/proto/v1"
    27  
    28  	// Add support for Spanner transactions in TQ.
    29  	_ "go.chromium.org/luci/server/tq/txn/spanner"
    30  )
    31  
    32  // FinalizationTasks describes how to route finalization tasks.
    33  //
    34  // The handler is implemented in internal/services/finalizer.
    35  var FinalizationTasks = tq.RegisterTaskClass(tq.TaskClass{
    36  	ID:            "try-finalize-inv",
    37  	Prototype:     &taskspb.TryFinalizeInvocation{},
    38  	Kind:          tq.Transactional,
    39  	Queue:         "finalizer",                 // use a dedicated queue
    40  	RoutingPrefix: "/internal/tasks/finalizer", // for routing to "finalizer" service
    41  })
    42  
    43  // StartInvocationFinalization changes invocation state to FINALIZING
    44  // if updateInv is set, and enqueues a TryFinalizeInvocation task.
    45  //
    46  // The caller is responsible for ensuring that the invocation is active.
    47  //
    48  // TODO(nodir): this package is not a great place for this function, but there
    49  // is no better package at the moment. Keep it here for now, but consider a
    50  // new package as the code base grows.
    51  func StartInvocationFinalization(ctx context.Context, id invocations.ID, updateInv bool) {
    52  	if updateInv {
    53  		span.BufferWrite(ctx, spanutil.UpdateMap("Invocations", map[string]any{
    54  			"InvocationId": id,
    55  			"State":        pb.Invocation_FINALIZING,
    56  		}))
    57  	}
    58  	tq.MustAddTask(ctx, &tq.Task{
    59  		Payload: &taskspb.TryFinalizeInvocation{InvocationId: string(id)},
    60  		Title:   string(id),
    61  	})
    62  }