From 93f8076e21d3cfbea732a7d9aab1cc764773990b Mon Sep 17 00:00:00 2001 From: DanB Date: Tue, 31 Jan 2017 16:58:27 +0100 Subject: [PATCH] Support for *none as fallback file path for Poster --- apier/v1/apier.go | 15 +++++++++------ utils/httpclient.go | 31 ++++++++++++++++++------------- 2 files changed, 27 insertions(+), 19 deletions(-) diff --git a/apier/v1/apier.go b/apier/v1/apier.go index a62ba2805..bdb4bde6c 100644 --- a/apier/v1/apier.go +++ b/apier/v1/apier.go @@ -1720,16 +1720,19 @@ func (v1 *ApierV1) ReplayFailedPosts(args ArgsReplyFailedPosts, reply *string) ( if err != nil { return utils.NewErrServerError(err) } + failoverPath := utils.META_NONE + if failedReqsOutDir != utils.META_NONE { + failoverPath = path.Join(failedReqsOutDir, file.Name()) + } _, err = utils.NewHTTPPoster(v1.Config.HttpSkipTlsVerify, v1.Config.ReplyTimeout).Post(ffn.Address, utils.PosterTransportContentTypes[ffn.Transport], fileContent, - v1.Config.PosterAttempts, path.Join(failedReqsOutDir, file.Name())) - if err != nil { // Got error from HTTPPoster could be that content was not written, we need to write it ourselves - fileOutPath := path.Join(failedReqsOutDir, ffn.AsString()) + v1.Config.PosterAttempts, failoverPath) + if err != nil && failedReqsOutDir != utils.META_NONE { // Got error from HTTPPoster could be that content was not written, we need to write it ourselves _, err := guardian.Guardian.Guard(func() (interface{}, error) { - if _, err := os.Stat(fileOutPath); err == nil || !os.IsNotExist(err) { + if _, err := os.Stat(failoverPath); err == nil || !os.IsNotExist(err) { return 0, err } - fileOut, err := os.Create(fileOutPath) + fileOut, err := os.Create(failoverPath) if err != nil { return 0, err } @@ -1738,7 +1741,7 @@ func (v1 *ApierV1) ReplayFailedPosts(args ArgsReplyFailedPosts, reply *string) ( return 0, err } return 0, nil - }, v1.Config.LockingTimeout, utils.FileLockPrefix+fileOutPath) + }, v1.Config.LockingTimeout, utils.FileLockPrefix+failoverPath) if err != nil { return utils.NewErrServerError(err) } diff --git a/utils/httpclient.go b/utils/httpclient.go index e812fd19e..d06c23d62 100644 --- a/utils/httpclient.go +++ b/utils/httpclient.go @@ -28,6 +28,8 @@ import ( "os" "strings" "time" + + "github.com/cgrates/cgrates/guardian" ) // NewFallbackFileNameFronString will revert the meta information in the fallback file name into original data @@ -136,7 +138,7 @@ type HTTPPoster struct { // Post with built-in failover // Returns also reference towards client so we can close it's connections when done -func (poster *HTTPPoster) Post(addr string, contentType string, content interface{}, attempts int, fallbackFilePath string) ([]byte, error) { +func (poster *HTTPPoster) Post(addr string, contentType string, content interface{}, attempts int, fallbackFilePath string) (respBody []byte, err error) { if !IsSliceMember([]string{CONTENT_JSON, CONTENT_FORM, CONTENT_TEXT}, contentType) { return nil, fmt.Errorf("unsupported ContentType: %s", contentType) } @@ -153,7 +155,6 @@ func (poster *HTTPPoster) Post(addr string, contentType string, content interfac if contentType == CONTENT_JSON { bodyType = "application/json" } - var err error for i := 0; i < attempts; i++ { var resp *http.Response if IsSliceMember([]string{CONTENT_JSON, CONTENT_TEXT}, contentType) { @@ -167,7 +168,7 @@ func (poster *HTTPPoster) Post(addr string, contentType string, content interfac continue } defer resp.Body.Close() - respBody, err := ioutil.ReadAll(resp.Body) + respBody, err = ioutil.ReadAll(resp.Body) if err != nil { Logger.Warning(fmt.Sprintf(" Posting to : <%s>, error: <%s>", addr, err.Error())) time.Sleep(delay()) @@ -180,15 +181,19 @@ func (poster *HTTPPoster) Post(addr string, contentType string, content interfac } return respBody, nil } - Logger.Debug(fmt.Sprintf(" Will failover on path: <%s>", fallbackFilePath)) - // If we got that far, post was not possible, write it on disk - fileOut, err := os.Create(fallbackFilePath) - if err != nil { - return nil, err + if fallbackFilePath != META_NONE { + // If we got that far, post was not possible, write it on disk + _, err = guardian.Guardian.Guard(func() (interface{}, error) { + fileOut, err := os.Create(fallbackFilePath) + if err != nil { + return nil, err + } + defer fileOut.Close() + if _, err := fileOut.Write(body); err != nil { + return nil, err + } + return nil, nil + }, time.Duration(2*time.Second), FileLockPrefix+fallbackFilePath) } - defer fileOut.Close() - if _, err := fileOut.Write(body); err != nil { - return nil, err - } - return nil, nil + return }