Support for *none as fallback file path for Poster

This commit is contained in:
DanB
2017-01-31 16:58:27 +01:00
parent 66f4f9a46e
commit 93f8076e21
2 changed files with 27 additions and 19 deletions

View File

@@ -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)
}

View File

@@ -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("<HTTPPoster> 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("<HTTPPoster> 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
}