go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/cv/internal/prjmanager/state/util.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 state
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  
    21  	"go.chromium.org/luci/common/sync/parallel"
    22  
    23  	"go.chromium.org/luci/cv/internal/common"
    24  	"go.chromium.org/luci/cv/internal/configs/prjcfg"
    25  )
    26  
    27  // pokeRuns pokes Run Manager for each incomplete Run.
    28  //
    29  // Doesn't have to be called in a transaction.
    30  func (h *Handler) pokeRuns(ctx context.Context, s *State) error {
    31  	err := parallel.WorkPool(concurrency, func(work chan<- func() error) {
    32  		for _, id := range s.PB.IncompleteRuns() {
    33  			id := id
    34  			work <- func() error {
    35  				return h.RunNotifier.PokeNow(ctx, id)
    36  			}
    37  		}
    38  	})
    39  	return common.MostSevereError(err)
    40  }
    41  
    42  // indexOfConfigGroup returns index of the externed Config Group name, which
    43  // must exist.
    44  func (s *State) indexOfConfigGroup(id prjcfg.ConfigGroupID) int32 {
    45  	want := id.Name()
    46  	// This can be optimized by lazily creating and caching a map from
    47  	// ConfigGroupID to its index, but most projects have <10 groups,
    48  	// so iterating a slice is faster & good enough.
    49  	names := s.PB.GetConfigGroupNames()
    50  	for i, name := range names {
    51  		if name == want {
    52  			return int32(i)
    53  		}
    54  	}
    55  	panic(fmt.Errorf("%s doesn't match any in known %s ConfigGroupNames", id, names))
    56  }