go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/resultdb/internal/tasks/finalization_notify.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 tasks contains task class definitions for ResultDB.
    16  package tasks
    17  
    18  import (
    19  	"context"
    20  
    21  	"google.golang.org/protobuf/encoding/protojson"
    22  	"google.golang.org/protobuf/proto"
    23  
    24  	"go.chromium.org/luci/resultdb/internal/tasks/taskspb"
    25  	pb "go.chromium.org/luci/resultdb/proto/v1"
    26  	"go.chromium.org/luci/server/auth/realms"
    27  	"go.chromium.org/luci/server/tq"
    28  
    29  	// Add support for Spanner transactions in TQ.
    30  	_ "go.chromium.org/luci/server/tq/txn/spanner"
    31  )
    32  
    33  const (
    34  	v1NotifyFinalizedTaskClass = "v1-publish-invocation-finalized"
    35  	v1NotifyFinalizedTopic     = "v1.invocation_finalized"
    36  )
    37  
    38  // NotifyFinalizedPublisher describes how to publish to cloud pub/sub
    39  // notifications that an invocation has been finalized.
    40  var NotifyFinalizedPublisher = tq.RegisterTaskClass(tq.TaskClass{
    41  	ID:        "notify-finalized",
    42  	Topic:     v1NotifyFinalizedTopic,
    43  	Prototype: &taskspb.NotifyInvocationFinalized{},
    44  	Kind:      tq.Transactional,
    45  	Custom: func(ctx context.Context, m proto.Message) (*tq.CustomPayload, error) {
    46  		// Custom serialisation handler needed to control
    47  		// how the message is sent, as the backend is
    48  		// Cloud Pub/Sub and not Cloud Tasks.
    49  		t := m.(*taskspb.NotifyInvocationFinalized)
    50  		notification := t.Message
    51  		blob, err := (protojson.MarshalOptions{Indent: "\t"}).Marshal(notification)
    52  		if err != nil {
    53  			return nil, err
    54  		}
    55  
    56  		// Prepare attributes, which are can be used by subscribers to
    57  		// filter the messages they receive.
    58  		if err := realms.ValidateRealmName(notification.Realm, realms.GlobalScope); err != nil {
    59  			return nil, err
    60  		}
    61  		project, _ := realms.Split(notification.Realm)
    62  		attrs := map[string]string{
    63  			"luci_project": project,
    64  		}
    65  
    66  		return &tq.CustomPayload{
    67  			Meta: attrs,
    68  			Body: blob,
    69  		}, nil
    70  	},
    71  })
    72  
    73  // NotifyInvocationFinalized transactionally enqueues
    74  // a task to publish that the given invocation has been
    75  // finalized.
    76  func NotifyInvocationFinalized(ctx context.Context, message *pb.InvocationFinalizedNotification) {
    77  	tq.MustAddTask(ctx, &tq.Task{
    78  		Payload: &taskspb.NotifyInvocationFinalized{Message: message},
    79  	})
    80  }