go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/cv/internal/prjmanager/state/side_effect.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/common/eventbox"
    25  	"go.chromium.org/luci/cv/internal/prjmanager/clpurger"
    26  	"go.chromium.org/luci/cv/internal/prjmanager/cltriggerer"
    27  	"go.chromium.org/luci/cv/internal/prjmanager/prjpb"
    28  )
    29  
    30  // SideEffect describes action to be done transactionally with updating state in
    31  // Datastore.
    32  //
    33  // It may consist of several `SideEffect`s, which are executed sequentially.
    34  //
    35  // Semantically, this is translatable to eventbox.SideEffectFn,
    36  // but is easy to assert for in tests of this package.
    37  type SideEffect interface {
    38  	// Do is the eventbox.SideEffectFn.
    39  	Do(context.Context) error
    40  }
    41  
    42  func SideEffectFn(s SideEffect) eventbox.SideEffectFn {
    43  	if s == nil {
    44  		return nil
    45  	}
    46  	return s.Do
    47  }
    48  
    49  // SideEffects combines 2+ `SideEffect`s.
    50  type SideEffects struct {
    51  	items []SideEffect
    52  }
    53  
    54  // Do implements SideEffect interface.
    55  func (s SideEffects) Do(ctx context.Context) error {
    56  	for _, it := range s.items {
    57  		if err := it.Do(ctx); err != nil {
    58  			return err
    59  		}
    60  	}
    61  	return nil
    62  }
    63  
    64  // NewSideEffects returns composite SideEffect.
    65  //
    66  // At least 2 items must be provided.
    67  // Provided arg slice must not be mutated.
    68  func NewSideEffects(items ...SideEffect) SideEffect {
    69  	if len(items) < 2 {
    70  		panic("at least 2 required")
    71  	}
    72  	se := &SideEffects{}
    73  	for _, item := range items {
    74  		if item != nil {
    75  			se.items = append(se.items, item)
    76  		}
    77  	}
    78  	if len(se.items) == 0 {
    79  		return nil
    80  	}
    81  	return se
    82  }
    83  
    84  // concurrency is how many goroutines may an individual SideEffect run at the
    85  // same time.
    86  const concurrency = 16
    87  
    88  // UpdateIncompleteRunsConfig sends UpdateConfig events to incomplete Runs.
    89  type UpdateIncompleteRunsConfig struct {
    90  	RunNotifier RunNotifier
    91  	RunIDs      common.RunIDs
    92  	Hash        string
    93  	EVersion    int64
    94  }
    95  
    96  // Do implements SideEffect interface.
    97  func (u *UpdateIncompleteRunsConfig) Do(ctx context.Context) error {
    98  	err := parallel.WorkPool(concurrency, func(work chan<- func() error) {
    99  		for _, id := range u.RunIDs {
   100  			id := id
   101  			work <- func() error {
   102  				return u.RunNotifier.UpdateConfig(ctx, id, u.Hash, u.EVersion)
   103  			}
   104  		}
   105  	})
   106  	return common.MostSevereError(err)
   107  }
   108  
   109  // CancelIncompleteRuns sends Cancel event to incomplete Runs.
   110  type CancelIncompleteRuns struct {
   111  	RunNotifier RunNotifier
   112  	RunIDs      common.RunIDs
   113  }
   114  
   115  // Do implements SideEffect interface.
   116  func (c *CancelIncompleteRuns) Do(ctx context.Context) error {
   117  	err := parallel.WorkPool(concurrency, func(work chan<- func() error) {
   118  		for _, id := range c.RunIDs {
   119  			id := id
   120  			work <- func() error {
   121  				return c.RunNotifier.Cancel(ctx, id, fmt.Sprintf("CV is disabled for LUCI Project %q", id.LUCIProject()))
   122  			}
   123  		}
   124  	})
   125  	return common.MostSevereError(err)
   126  }
   127  
   128  // TriggerPurgeCLTasks triggers PurgeCLTasks via TQ.
   129  type TriggerPurgeCLTasks struct {
   130  	payloads []*prjpb.PurgeCLTask
   131  	clPurger *clpurger.Purger
   132  }
   133  
   134  // Do implements SideEffect interface.
   135  func (t *TriggerPurgeCLTasks) Do(ctx context.Context) error {
   136  	err := parallel.WorkPool(concurrency, func(work chan<- func() error) {
   137  		for _, p := range t.payloads {
   138  			p := p
   139  			work <- func() error {
   140  				return t.clPurger.Schedule(ctx, p)
   141  			}
   142  		}
   143  	})
   144  	return common.MostSevereError(err)
   145  }
   146  
   147  // ScheduleTriggeringCLDepsTasks schedules TriggeringCLDepsTask(s) via TQ.
   148  type ScheduleTriggeringCLDepsTasks struct {
   149  	payloads    []*prjpb.TriggeringCLDepsTask
   150  	clTriggerer *cltriggerer.Triggerer
   151  }
   152  
   153  // Do implements SideEffect interface.
   154  func (t *ScheduleTriggeringCLDepsTasks) Do(ctx context.Context) error {
   155  	if len(t.payloads) == 0 {
   156  		return nil
   157  	}
   158  	err := parallel.WorkPool(concurrency, func(work chan<- func() error) {
   159  		for _, p := range t.payloads {
   160  			p := p
   161  			work <- func() error {
   162  				return t.clTriggerer.Schedule(ctx, p)
   163  			}
   164  		}
   165  	})
   166  	return common.MostSevereError(err)
   167  }