Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improving to make it more idiomatic #63

Merged
merged 8 commits into from
Dec 31, 2023
Prev Previous commit
Next Next commit
Feat Helper Functions
- [+] refactor(helper.go): refactor logTaskStart function to combine emojis with a space for readability
- [+] refactor(helper.go): refactor logErrorWithFields function to combine emojis with a space for readability
- [+] feat(helper.go): add withRetries function to handle retrying an operation with a specified number of attempts and delay
- [+] feat(helper.go): add attemptOperation function to attempt an operation and handle errors
- [+] feat(helper.go): add waitForNextAttempt function to wait for the next attempt with a specified delay
  • Loading branch information
H0llyW00dzZ committed Dec 31, 2023
commit a06f670f9898b2aa284daf5e7b82da2893b69d09
46 changes: 44 additions & 2 deletions worker/helper.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package worker

import (
"context"
"fmt"
"time"

"github.com/H0llyW00dzZ/K8sBlackPearl/language"
"github.com/H0llyW00dzZ/K8sBlackPearl/navigator"
"github.com/H0llyW00dzZ/K8sBlackPearl/worker/configuration"
"github.com/H0llyW00dzZ/go-urlshortner/logmonitor/constant"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -92,7 +95,9 @@ func getParamAsInt(params map[string]interface{}, key string) (int, error) {
// message string: The message to log, which should describe the task being started.
// fields []zap.Field: A slice of zap.Field items that provide additional context for the log entry.
func logTaskStart(message string, fields []zap.Field) {
navigator.LogInfoWithEmoji(language.PirateEmoji, message, fields...)
// Combine emojis with a space for readability.
emojiField := fmt.Sprintf("%s %s", language.CompassEmoji, language.PirateEmoji)
navigator.LogInfoWithEmoji(emojiField, message, fields...)
}

// createLogFieldsForRunnerTask generates a slice of zap.Field items for structured logging.
Expand All @@ -117,7 +122,9 @@ func createLogFieldsForRunnerTask(task configuration.Task, shipsNamespace string
// err error: The error to log.
// fields []zap.Field: A slice of zap.Field items that provide additional context for the error log entry.
func logErrorWithFields(err error, fields []zap.Field) {
navigator.LogErrorWithEmojiRateLimited(language.PirateEmoji, err.Error(), fields...)
// Combine emojis with a space for readability.
emojiField := fmt.Sprintf("%s %s", constant.ErrorEmoji, language.PirateEmoji)
navigator.LogErrorWithEmojiRateLimited(emojiField, err.Error(), fields...)
}

// logResultsFromChannel logs messages received from a channel.
Expand All @@ -130,3 +137,38 @@ func logResultsFromChannel(results chan string, fields []zap.Field) {
navigator.LogInfoWithEmoji(language.PirateEmoji, result, fields...)
}
}

func withRetries(ctx context.Context, maxRetries int, retryDelay time.Duration, operation func() (string, error)) error {
for attempt := 0; attempt < maxRetries; attempt++ {
taskName, err := attemptOperation(ctx, attempt, operation)
if err == nil {
return nil // The operation was successful, return nil error.
}
if ctx.Err() != nil {
return ctx.Err() // The context has been cancelled, return the context error.
}
logRetryAttempt(taskName, attempt, err, maxRetries)
if attempt < maxRetries-1 && !waitForNextAttempt(ctx, retryDelay) {
// Only wait for the next attempt if we have more retries left and context is not done.
return ctx.Err() // Context was cancelled during wait, return the context error.
}
}
return fmt.Errorf(language.ErrorSailingShips, maxRetries)
}

func attemptOperation(ctx context.Context, attempt int, operation func() (string, error)) (string, error) {
taskName, err := operation()
if err != nil {
return taskName, fmt.Errorf(language.ErrorAttemptFailed, attempt, err)
}
return taskName, nil
}

func waitForNextAttempt(ctx context.Context, retryDelay time.Duration) bool {
select {
case <-ctx.Done():
return false
case <-time.After(retryDelay):
return true
}
}