From 6e7b8367a61585d38392c032dda810cd8a5de4bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Alberto=20D=C3=ADaz=20Orozco?= Date: Tue, 11 Mar 2025 12:46:16 +0100 Subject: [PATCH 1/3] Exit mc mirror when errors occur if retry is disabled and it's a watch operation --- cmd/error.go | 8 ++++++++ cmd/mirror-main.go | 12 ++++++++---- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/cmd/error.go b/cmd/error.go index 39189e31cd..82340560c8 100644 --- a/cmd/error.go +++ b/cmd/error.go @@ -45,6 +45,14 @@ type errorMessage struct { SysInfo map[string]string `json:"sysinfo,omitempty"` } +// errorOrFatal wrapper function to call errorIf or fatalIf based on the boolean value +func errorOrFatal(useFatal bool, err *probe.Error, msg string, data ...interface{}) { + if useFatal { + fatalIf(err, msg, data...) + } + errorIf(err, msg, data...) +} + // fatalIf wrapper function which takes error and selectively prints stack frames if available on debug func fatalIf(err *probe.Error, msg string, data ...interface{}) { if err == nil { diff --git a/cmd/mirror-main.go b/cmd/mirror-main.go index dfa1b687a7..4368bb44ff 100644 --- a/cmd/mirror-main.go +++ b/cmd/mirror-main.go @@ -561,6 +561,10 @@ func (mj *mirrorJob) monitorMirrorStatus(cancel context.CancelFunc) (errDuringMi mj.status.Start() defer mj.status.Finish() + // if the operation is not retriable and is a watch operation, then + // we should exit on the first error. + useFatal := !mj.opts.isRetriable && mj.opts.isWatch + var cancelInProgress bool defer func() { @@ -595,22 +599,22 @@ func (mj *mirrorJob) monitorMirrorStatus(cancel context.CancelFunc) (errDuringMi ignoreErr = true } if !ignoreErr { - errorIf(sURLs.Error.Trace(sURLs.SourceContent.URL.String()), + errorOrFatal(useFatal, sURLs.Error.Trace(sURLs.SourceContent.URL.String()), "Failed to copy `%s`.", sURLs.SourceContent.URL) } } case sURLs.TargetContent != nil: // When sURLs.SourceContent is nil, we know that we have an error related to removing - errorIf(sURLs.Error.Trace(sURLs.TargetContent.URL.String()), + errorOrFatal(useFatal, sURLs.Error.Trace(sURLs.TargetContent.URL.String()), "Failed to remove `%s`.", sURLs.TargetContent.URL.String()) default: if strings.Contains(sURLs.Error.ToGoError().Error(), "Overwrite not allowed") { ignoreErr = true } if sURLs.ErrorCond == differInUnknown { - errorIf(sURLs.Error.Trace(), "Failed to perform mirroring") + errorOrFatal(useFatal, sURLs.Error.Trace(), "Failed to perform mirroring") } else { - errorIf(sURLs.Error.Trace(), + errorOrFatal(useFatal, sURLs.Error.Trace(), "Failed to perform mirroring, with error condition (%s)", sURLs.ErrorCond) } } From 81a378335f42360a0fb10626df3bc12243f604d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Alberto=20D=C3=ADaz=20Orozco?= Date: Tue, 6 May 2025 11:32:26 +0200 Subject: [PATCH 2/3] Add `--fail-on-error` flag to the mirror subcommand --- cmd/mirror-main.go | 5 +++-- cmd/mirror-url.go | 1 + 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/cmd/mirror-main.go b/cmd/mirror-main.go index aa81c856b7..f1eea38496 100644 --- a/cmd/mirror-main.go +++ b/cmd/mirror-main.go @@ -561,9 +561,9 @@ func (mj *mirrorJob) monitorMirrorStatus(cancel context.CancelFunc) (errDuringMi mj.status.Start() defer mj.status.Finish() - // if the operation is not retriable and is a watch operation, then + // if the operation is not retriable and fail-on-error is true, then // we should exit on the first error. - useFatal := !mj.opts.isRetriable && mj.opts.isWatch + useFatal := mj.opts.failOnError && !mj.opts.isRetriable var cancelInProgress bool @@ -1015,6 +1015,7 @@ func runMirror(ctx context.Context, srcURL, dstURL string, cli *cli.Context, enc isMetadata: isMetadata, isSummary: cli.Bool("summary"), isRetriable: cli.Bool("retry"), + failOnError: cli.Bool("fail-on-error"), md5: md5, checksum: checksum, disableMultipart: cli.Bool("disable-multipart"), diff --git a/cmd/mirror-url.go b/cmd/mirror-url.go index 1c71fb8917..d7c4d40707 100644 --- a/cmd/mirror-url.go +++ b/cmd/mirror-url.go @@ -268,6 +268,7 @@ type mirrorOptions struct { isFake, isOverwrite, activeActive bool isWatch, isRemove, isMetadata bool isRetriable bool + failOnError bool isSummary bool skipErrors bool excludeOptions, excludeStorageClasses, excludeBuckets []string From cf8fbed54fefdb34279076029284c855ade7da16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Alberto=20D=C3=ADaz=20Orozco?= Date: Tue, 6 May 2025 11:54:29 +0200 Subject: [PATCH 3/3] Add `--fail-on-error` to the cli flags --- cmd/mirror-main.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/cmd/mirror-main.go b/cmd/mirror-main.go index f1eea38496..236a5998b9 100644 --- a/cmd/mirror-main.go +++ b/cmd/mirror-main.go @@ -136,6 +136,10 @@ var ( Name: "retry", Usage: "if specified, will enable retrying on a per object basis if errors occur", }, + cli.BoolFlag{ + Name: "fail-on-error", + Usage: "if specified, the application will exit if errors occur", + }, cli.BoolFlag{ Name: "summary", Usage: "print a summary of the mirror session",