From 1f7b1ac9ed94a3626e77edc217f066332e3f10c1 Mon Sep 17 00:00:00 2001 From: Douglas Thrift Date: Mon, 28 Feb 2022 17:18:36 -0800 Subject: [PATCH] Handle eventual consistency issues with fpt run log output --- cmd/fpt/ChangeLog.md | 5 +++-- cmd/fpt/run.go | 37 ++++++++++++++++++++----------------- 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/cmd/fpt/ChangeLog.md b/cmd/fpt/ChangeLog.md index 64a9f4b..63b891c 100644 --- a/cmd/fpt/ChangeLog.md +++ b/cmd/fpt/ChangeLog.md @@ -1,6 +1,7 @@ -Unreleased Changes ------------------- +v1.4.0 / 2022-03-01 +------------------- * Add support for `atob` and `btoa` JavaScript Base64 functions in `fpt script` +* Handle eventual consistency issues with `fpt run` log output which could result in panics v1.3.0 / 2021-03-01 ------------------- diff --git a/cmd/fpt/run.go b/cmd/fpt/run.go index ca6d9e3..c70c9f1 100644 --- a/cmd/fpt/run.go +++ b/cmd/fpt/run.go @@ -96,25 +96,28 @@ func policyTemplateRun(ctx context.Context, cli policy.Client, file string, runO } lastStatus = status - log, logErr := cli.ShowAppliedPolicyLog(ctx, ap.ID, lastEtag) - if logErr != nil { - // technically the Goa client should not consider 304 an error - if strings.Contains(logErr.Error(), "invalid response code 304") { - goto checkStatus + if !noLog { + log, logErr := cli.ShowAppliedPolicyLog(ctx, ap.ID, lastEtag) + if logErr != nil { + // technically the Goa client should not consider 304 an error + if strings.Contains(logErr.Error(), "invalid response code 304") { + goto checkStatus + } + // the log may not be available yet so we retry 404s + if gse, ok := logErr.(*goa.ServiceError); ok && gse.Name == "not_found" { + goto checkStatus + } + return logErr } - // the log may not be available yet so we retry 404s - if gse, ok := logErr.(*goa.ServiceError); ok && gse.Name == "not_found" { - goto checkStatus - } - return logErr - } - if *log.Etag != lastEtag { - lastSize := len(lastLog) - lastEtag = *log.Etag - lastLog = *log.ResponseBody - if !noLog { - fmt.Print(lastLog[lastSize:]) + if *log.Etag != lastEtag { + lastSize := len(lastLog) + lastEtag = *log.Etag + // only print new log data (due to eventual consistency issues, a shorter log may be returned) + if lastSize <= len(*log.ResponseBody) { + lastLog = *log.ResponseBody + fmt.Print(lastLog[lastSize:]) + } } }