go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/cv/internal/run/bq/exporter.go (about)

     1  // Copyright 2021 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 bq
    16  
    17  import (
    18  	"context"
    19  
    20  	"google.golang.org/protobuf/proto"
    21  
    22  	"go.chromium.org/luci/common/logging"
    23  	"go.chromium.org/luci/server/tq"
    24  
    25  	"go.chromium.org/luci/cv/internal/common"
    26  	cvbq "go.chromium.org/luci/cv/internal/common/bq"
    27  )
    28  
    29  const exportRunToBQTaskClass = "bq-export"
    30  
    31  // Exporter sends finished Run data to BigQuery.
    32  type Exporter struct {
    33  	tqd *tq.Dispatcher
    34  	bqc cvbq.Client
    35  }
    36  
    37  // NewExporter creates a new Exporter, registering it in the given TQ
    38  // dispatcher.
    39  func NewExporter(tqd *tq.Dispatcher, bqc cvbq.Client, env *common.Env) *Exporter {
    40  	exporter := &Exporter{tqd, bqc}
    41  	tqd.RegisterTaskClass(tq.TaskClass{
    42  		ID:           exportRunToBQTaskClass,
    43  		Prototype:    &ExportRunToBQTask{},
    44  		Queue:        "bq-export",
    45  		Quiet:        true,
    46  		QuietOnError: true,
    47  		// BQ Export should be done in a transaction, because we want
    48  		// BQ exported if and only if Run Status is changed in datastore.
    49  		Kind: tq.Transactional,
    50  		Handler: func(ctx context.Context, payload proto.Message) error {
    51  			task := payload.(*ExportRunToBQTask)
    52  			ctx = logging.SetField(ctx, "run", task.GetRunId())
    53  			err := send(ctx, env, bqc, common.RunID(task.GetRunId()))
    54  			return common.TQifyError(ctx, err)
    55  		},
    56  	})
    57  	return exporter
    58  }
    59  
    60  // Schedule enqueues a task to send a row to BQ for a Run.
    61  func (s *Exporter) Schedule(ctx context.Context, runID common.RunID) error {
    62  	return s.tqd.AddTask(ctx, &tq.Task{
    63  		Title:   string(runID),
    64  		Payload: &ExportRunToBQTask{RunId: string(runID)},
    65  	})
    66  }