Merge pull request #6169 from steven-zou/fix_issue_of_periodic_conflicts

Return 409 conflict error in liue of 200 with ID of existing one
This commit is contained in:
Steven Zou 2018-10-29 16:18:40 +08:00 committed by GitHub
commit dde6c39e80
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 3 deletions

View File

@ -83,7 +83,13 @@ func (dh *DefaultHandler) HandleLaunchJobReq(w http.ResponseWriter, req *http.Re
// Pass request to the controller for the follow-up.
jobStats, err := dh.controller.LaunchJob(jobReq)
if err != nil {
dh.handleError(w, req, http.StatusInternalServerError, errs.LaunchJobError(err))
if errs.IsConflictError(err) {
// Conflict error
dh.handleError(w, req, http.StatusConflict, err)
} else {
// General error
dh.handleError(w, req, http.StatusInternalServerError, errs.LaunchJobError(err))
}
return
}

View File

@ -17,6 +17,7 @@ package errs
import (
"encoding/json"
"fmt"
)
const (
@ -50,6 +51,8 @@ const (
NoObjectFoundErrorCode
// UnAuthorizedErrorCode is code for the error of unauthorized accessing
UnAuthorizedErrorCode
// ResourceConflictsErrorCode is code for the error of resource conflicting
ResourceConflictsErrorCode
)
// baseError ...
@ -183,6 +186,22 @@ func NoObjectFoundError(object string) error {
}
}
// conflictError is designed for the case of resource conflicting
type conflictError struct {
baseError
}
// ConflictError is error for the case of resource conflicting
func ConflictError(object string) error {
return conflictError{
baseError{
Code: ResourceConflictsErrorCode,
Err: "conflict",
Description: fmt.Sprintf("the submitting resource is conflicted with existing one %s", object),
},
}
}
// IsJobStoppedError return true if the error is jobStoppedError
func IsJobStoppedError(err error) bool {
_, ok := err.(jobStoppedError)
@ -200,3 +219,9 @@ func IsObjectNotFoundError(err error) bool {
_, ok := err.(objectNotFoundError)
return ok
}
// IsConflictError returns true if the error is conflictError
func IsConflictError(err error) bool {
_, ok := err.(conflictError)
return ok
}

View File

@ -120,8 +120,9 @@ func (rps *RedisPeriodicScheduler) Schedule(jobName string, params models.Parame
// Check existing
// If existing, treat as a succeed submitting and return the exitsing id
if score, ok := rps.exists(string(rawJSON)); ok {
id, err := rps.getIDByScore(score)
return id, 0, err
// Ignore error
id, _ := rps.getIDByScore(score)
return "", 0, errs.ConflictError(id)
}
uuid, score := utils.MakePeriodicPolicyUUID()