go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/server/tq/txn/datastore/init.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  // Datastore contains Transactional Enqueue support for Cloud Datastore.
    16  //
    17  // Importing this package adds Cloud Datastore transactions support to
    18  // server/tq's AddTask. Depends on "go.chromium.org/luci/gae/filter/txndefer" filter
    19  // installed. It is installed by default in LUCI server contexts.
    20  //
    21  // This package is normally imported unnamed:
    22  //
    23  //	import _ "go.chromium.org/luci/server/tq/txn/datastore"
    24  //
    25  // Will take ownership of entities with kinds "tq.*" (e.g. "tq.Reminder").
    26  package datastore
    27  
    28  import (
    29  	"context"
    30  
    31  	"go.chromium.org/luci/gae/service/datastore"
    32  
    33  	"go.chromium.org/luci/server/gaeemulation"
    34  	"go.chromium.org/luci/server/tq/internal/db"
    35  	"go.chromium.org/luci/server/tq/internal/lessor"
    36  )
    37  
    38  var impl dsDB
    39  
    40  func init() {
    41  	db.Register(db.Impl{
    42  		Kind:   impl.Kind(),
    43  		Module: gaeemulation.ModuleName,
    44  		ProbeForTxn: func(ctx context.Context) db.DB {
    45  			if datastore.CurrentTransaction(ctx) != nil {
    46  				return impl
    47  			}
    48  			return nil
    49  		},
    50  		NonTxn: func(ctx context.Context) db.DB {
    51  			return impl
    52  		},
    53  	})
    54  }
    55  
    56  func init() {
    57  	lessor.Register("datastore", func(context.Context) (lessor.Lessor, error) {
    58  		return &dsLessor{}, nil
    59  	})
    60  }