refactor: fixed in-line documentation and added line-wraps where needed

This commit is contained in:
ChrisERo 2022-01-10 19:00:37 -05:00 committed by chrrodri
parent f67669662b
commit 0df6e005d3
2 changed files with 20 additions and 14 deletions

View File

@ -174,9 +174,9 @@ var (
}, []string{"status"})
)
// Channels for ensuring hooks execute at least once before terminating when --one-time flag is set.
// Should be nil if and only if corresponding hook is not defined and if initialised, will only every get written
// to once.
// Channels for ensuring hooks execute at least once before terminating when
// --one-time flag is set. Should be nil if and only if corresponding hook is
// not defined and if initialised, will only ever get written to once.
var exechookChannel, webhookChannel chan bool
const (
@ -475,8 +475,8 @@ func main() {
run: cmdRunner,
}
// This context is used only for git credentials initialization. There are no long-running operations like
// `git clone`, so hopefully 30 seconds will be enough.
// This context is used only for git credentials initialization. There are
// no long-running operations like `git clone`, so hopefully 30 seconds will be enough.
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
if *flUsername != "" {
@ -638,9 +638,11 @@ func main() {
if initialSync {
// Determine if git-sync should terminate for one of several reasons
if *flOneTime {
// Wait for hooks to complete at least once, if not nil, before checking whether to stop program
// Assumes that if hook channels are not nil, they will have at least one value before getting closed
exitCode := 0 // if all hooks succeeded, exit code is 0, else set to 1
// Wait for hooks to complete at least once, if not nil, before
// checking whether to stop program.
// Assumes that if hook channels are not nil, they will have at
// least one value before getting closed
exitCode := 0 // is 0 if all hooks succeed, else is 1
if exechookChannel != nil {
exechookChannelFinishedSuccessfully := <-exechookChannel
if !exechookChannelFinishedSuccessfully {

View File

@ -96,8 +96,10 @@ type HookRunner struct {
data *hookData
// Logger
logger *logging.Logger
// hasCompletedOnce is used to send true if and only if first run executed successfully and false otherwise to some receiver.
// should be initialised to a buffered channel of size 1. Is only meant to be used within sendCompletedOnceMessageIfApplicable
// hasCompletedOnce is used to send true if and only if first run executed
// successfully and false otherwise to some receiver. Should be
// initialised to a buffered channel of size 1.
// Is only meant for use within sendCompletedOnceMessageIfApplicable.
hasCompletedOnce chan bool
}
@ -126,7 +128,7 @@ func (r *HookRunner) Run(ctx context.Context) {
if err := r.hook.Do(ctx, hash); err != nil {
r.logger.Error(err, "hook failed")
updateHookRunCountMetric(r.hook.Name(), "error")
// don't want to sleep unnecessarily if we are going to terminate anyways
// don't want to sleep unnecessarily terminating anyways
r.sendCompletedOnceMessageIfApplicable(false)
time.Sleep(r.backoff)
} else {
@ -139,9 +141,11 @@ func (r *HookRunner) Run(ctx context.Context) {
}
}
// If hasCompletedOnce is nil, does nothing. Otherwise, forwards the caller provided success status (as a boolean) of
// HookRunner to receivers of hasCompletedOnce, closes said chanel, and sets hasCompletedOnce to nil.
// Using this function to write to hasCompletedOnce ensures it is only every written to once.
// If hasCompletedOnce is nil, does nothing. Otherwise, forwards the caller
// provided success status (as a boolean) of HookRunner to receivers of
// hasCompletedOnce, closes said chanel, and sets hasCompletedOnce to nil.
// Using this function to write to hasCompletedOnce ensures it is only ever
// written to once.
func (r *HookRunner) sendCompletedOnceMessageIfApplicable(completedSuccessfully bool) {
if r.hasCompletedOnce != nil {
r.hasCompletedOnce <- completedSuccessfully