From a828c19cf6ada8c977661502a907620e5ab6603c Mon Sep 17 00:00:00 2001 From: Kailash Nadh Date: Mon, 3 Feb 2020 16:47:55 +0530 Subject: [PATCH] Fix bug in no-job response --- client/client_test.go | 4 +++- go.mod | 1 + http.go | 9 +++++++-- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/client/client_test.go b/client/client_test.go index e4d9813..8e64731 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -50,9 +50,11 @@ func TestPostJob(t *testing.T) { } func TestGetJobStatus(t *testing.T) { + _, err := cl.GetJobStatus("aaaa") + assert.Error(t, err) for _, j := range jobs { r, err := cl.GetJobStatus(j.JobID) - assert.NoError(t, err, "error posting job") + assert.NoError(t, err, "error getting job status") assert.Equal(t, j.JobID, r.JobID) } } diff --git a/go.mod b/go.mod index a8ea4a8..7c4cbd0 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,7 @@ require ( github.com/go-chi/chi v3.3.3+incompatible github.com/go-sql-driver/mysql v1.4.0 github.com/golang/protobuf v1.3.1 // indirect + github.com/gomodule/redigo v2.0.0+incompatible github.com/google/uuid v1.1.1 // indirect github.com/googleapis/gax-go/v2 v2.0.5 // indirect github.com/hashicorp/golang-lru v0.5.1 // indirect diff --git a/http.go b/http.go index dc06162..6e49a08 100644 --- a/http.go +++ b/http.go @@ -8,6 +8,7 @@ import ( "github.com/RichardKnop/machinery/v1/tasks" "github.com/go-chi/chi" + "github.com/gomodule/redigo/redis" "github.com/knadh/sql-jobber/models" ) @@ -31,8 +32,12 @@ func handleGetTasksList(w http.ResponseWriter, r *http.Request) { // handleGetJobStatus returns the status of a given jobID. func handleGetJobStatus(w http.ResponseWriter, r *http.Request) { - out, err := jobber.Machinery.GetBackend().GetState(chi.URLParam(r, "jobID")) - if err != nil { + jobID := chi.URLParam(r, "jobID") + out, err := jobber.Machinery.GetBackend().GetState(jobID) + if err == redis.ErrNil { + sendErrorResponse(w, "job not found", http.StatusNotFound) + return + } else if err != nil { sysLog.Printf("error fetching job status: %v", err) sendErrorResponse(w, "error fetching job status", http.StatusInternalServerError) return