go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/server/tq/internal/reminder/payload.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 reminder
    16  
    17  import (
    18  	"time"
    19  
    20  	taskspb "cloud.google.com/go/cloudtasks/apiv2/cloudtaskspb"
    21  	"cloud.google.com/go/pubsub/apiv1/pubsubpb"
    22  	"google.golang.org/protobuf/proto"
    23  )
    24  
    25  // Payload incapsulates the Reminder's payload.
    26  //
    27  // It is produced by Dispatcher and is ultimately consumed by Submitter. It is
    28  // either produced and consumed in the same process (on a "happy path"), or it
    29  // may travel between processes by being stored in a serialized form inside
    30  // a Reminder (as its RawPayload), see Reminder.AttachPayload.
    31  type Payload struct {
    32  	TaskClass string        // corresponding TaskClass.ID, for metrics
    33  	Created   time.Time     // when AddTask was called, for metrics
    34  	Raw       proto.Message // a proto passed to AddTask, available only on happy path
    35  
    36  	CreateTaskRequest *taskspb.CreateTaskRequest // prepared Cloud Tasks request
    37  	PublishRequest    *pubsubpb.PublishRequest   // prepared PubSub request
    38  }
    39  
    40  // injectReminderID is called when the payload is attached to a reminder.
    41  func (r *Payload) injectReminderID(id string) {
    42  	// We use reminder ID to dedup Cloud Tasks submitted by the sweeper.
    43  	if req := r.CreateTaskRequest; req != nil {
    44  		if req.Task != nil { // may be nil in tests
    45  			req.Task.Name = req.Parent + "/tasks/" + id
    46  		}
    47  	}
    48  	// For PubSub tasks just inject the ID into attributes, for observability.
    49  	if req := r.PublishRequest; req != nil {
    50  		for _, m := range req.Messages {
    51  			m.Attributes["X-Luci-Tq-Reminder-Id"] = id
    52  		}
    53  	}
    54  }