Improving erro handling while trying to reserve tasks at async runner

Each time when MQ becomes unreachable HTTP GET /tasks returned HTTP 500
 and code was not handling this case except expecting networking errors.
 After that it tried to unmarshal empty response body that caused another sort of an error.

 This patch triggers error based on http response code, explicitly checking if response code
 is something unexpected (not HTTP 200 OK).

 Response status code for /tasks for changed from 202 Accepted to 200 OK according to swagger doc.
This commit is contained in:
Denis Makogon
2017-07-28 12:52:51 +03:00
parent d3243b3ac9
commit 721c0f1255
2 changed files with 15 additions and 8 deletions

View File

@@ -14,30 +14,37 @@ import (
"sync" "sync"
"time" "time"
"fmt"
"github.com/Sirupsen/logrus" "github.com/Sirupsen/logrus"
"github.com/opentracing/opentracing-go"
"github.com/fnproject/fn/api/models" "github.com/fnproject/fn/api/models"
"github.com/fnproject/fn/api/runner/common" "github.com/fnproject/fn/api/runner/common"
"github.com/fnproject/fn/api/runner/task" "github.com/fnproject/fn/api/runner/task"
"github.com/opentracing/opentracing-go"
) )
func getTask(ctx context.Context, url string) (*models.Task, error) { func getTask(ctx context.Context, url string) (*models.Task, error) {
// TODO shove this ctx into the request? ctx, log := common.LoggerWithFields(ctx, logrus.Fields{"runner": "async"})
span, _ := opentracing.StartSpanFromContext(ctx, "get_task") span, _ := opentracing.StartSpanFromContext(ctx, "get_task")
defer span.Finish() defer span.Finish()
// TODO uh, make a better http client :facepalm: req, _ := http.NewRequest("GET", url, nil)
resp, err := http.Get(url) resp, err := http.DefaultClient.Do(req.WithContext(ctx))
if err != nil {
return nil, err
}
defer func() { defer func() {
io.Copy(ioutil.Discard, resp.Body) io.Copy(ioutil.Discard, resp.Body)
resp.Body.Close() resp.Body.Close()
}() }()
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, errors.New(fmt.Sprintf("Unable to get task. Reason %v", resp.Status))
}
var task models.Task var task models.Task
err = json.NewDecoder(resp.Body).Decode(&task) err = json.NewDecoder(resp.Body).Decode(&task)
if err != nil {
log.WithError(err).Error("Unable to decode task from response object")
}
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@@ -267,7 +267,7 @@ func (s *Server) handleTaskRequest(c *gin.Context) {
handleErrorResponse(c, err) handleErrorResponse(c, err)
return return
} }
c.JSON(http.StatusAccepted, task) c.JSON(http.StatusOK, task)
case "DELETE": case "DELETE":
body, err := ioutil.ReadAll(c.Request.Body) body, err := ioutil.ReadAll(c.Request.Body)
if err != nil { if err != nil {