From 408b751ae61ce390fcd77f4b6fcb3472e402a443 Mon Sep 17 00:00:00 2001 From: Steven Zou Date: Mon, 29 Oct 2018 15:36:55 +0800 Subject: [PATCH] Return 409 conflict error in liue of 200 with ID of existing one when submmiting periodic job Signed-off-by: Steven Zou --- src/jobservice/api/handler.go | 8 +++++++- src/jobservice/errs/errors.go | 25 ++++++++++++++++++++++++ src/jobservice/period/redis_scheduler.go | 5 +++-- 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/src/jobservice/api/handler.go b/src/jobservice/api/handler.go index 4202cb14a..5f495f4dc 100644 --- a/src/jobservice/api/handler.go +++ b/src/jobservice/api/handler.go @@ -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 } diff --git a/src/jobservice/errs/errors.go b/src/jobservice/errs/errors.go index 3e42916cc..a85015b93 100644 --- a/src/jobservice/errs/errors.go +++ b/src/jobservice/errs/errors.go @@ -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 +} diff --git a/src/jobservice/period/redis_scheduler.go b/src/jobservice/period/redis_scheduler.go index 229a2e1bb..e28a23807 100644 --- a/src/jobservice/period/redis_scheduler.go +++ b/src/jobservice/period/redis_scheduler.go @@ -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()