Skip to content

Commit 75b6088

Browse files
author
Djalal Harouni
committed
fleetctl: add tryWaitForUnitStates() and getBlockAttempts()
* tryWaitForUnitStates() tries to wait for units to reach the desired state. * getBlockAttempts() gets the correct value of how many attempts to try before giving up on an operation. These helpers will be used to make the code more consistent and clean. We do not intended to change any behaviour here. ---
1 parent 8d8f775 commit 75b6088

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

fleetctl/fleetctl.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,56 @@ func setTargetStateOfUnits(units []string, state job.JobState) ([]*schema.Unit,
745745
return triggered, nil
746746
}
747747

748+
// getBlockAttempts gets the correct value of how many attempts to try
749+
// before giving up on an operation.
750+
// It returns a negative value which means try forever, if zero is
751+
// returned then do not make any attempt, and if a positive value is
752+
// returned then try up to that value
753+
func getBlockAttempts() int {
754+
// By default we wait forever
755+
var attempts int = -1
756+
757+
// Up to BlockAttempts
758+
if sharedFlags.BlockAttempts > 0 {
759+
attempts = sharedFlags.BlockAttempts
760+
}
761+
762+
// NoBlock we do not wait
763+
if sharedFlags.NoBlock {
764+
attempts = 0
765+
}
766+
767+
return attempts
768+
}
769+
770+
// tryWaitForUnitStates tries to wait for units to reach the desired state.
771+
// It takes 5 arguments, the units to wait for, the desired state, the
772+
// desired JobState, how many attempts before timing out and a writer
773+
// interface.
774+
// tryWaitForUnitStates polls each of the indicated units until they
775+
// reach the desired state. If maxAttempts is zero, then it will not
776+
// wait, it will assume that all units reached their desired state.
777+
// If maxAttempts is negative tryWaitForUnitStates will retry forever, and
778+
// if it is greater than zero, it will retry up to the indicated value.
779+
// It returns 0 on success or 1 on errors.
780+
func tryWaitForUnitStates(units []string, state string, js job.JobState, maxAttempts int, out io.Writer) (ret int) {
781+
// We do not wait just assume we reached the desired state
782+
if maxAttempts == 0 {
783+
for _, name := range units {
784+
stdout("Triggered unit %s %s", name, state)
785+
}
786+
return
787+
}
788+
789+
errchan := waitForUnitStates(units, js, maxAttempts, out)
790+
for err := range errchan {
791+
stderr("Error waiting for units: %v", err)
792+
ret = 1
793+
}
794+
795+
return
796+
}
797+
748798
// waitForUnitStates polls each of the indicated units until each of their
749799
// states is equal to that which the caller indicates, or until the
750800
// polling operation times out. waitForUnitStates will retry forever, or

0 commit comments

Comments
 (0)