diff --git a/src/jobservice/models/models.go b/src/jobservice/models/models.go index 46c0d9bac..c3f68f4ae 100644 --- a/src/jobservice/models/models.go +++ b/src/jobservice/models/models.go @@ -71,9 +71,10 @@ type JobActionRequest struct { //JobStatusChange is designed for reporting the status change via hook. type JobStatusChange struct { - JobID string `json:"job_id"` - Status string `json:"status"` - CheckIn string `json:"check_in,omitempty"` + JobID string `json:"job_id"` + Status string `json:"status"` + CheckIn string `json:"check_in,omitempty"` + Metadata *JobStatData `json:"metadata,omitempty"` } //Message is designed for sub/pub messages diff --git a/src/jobservice/opm/redis_job_stats_mgr.go b/src/jobservice/opm/redis_job_stats_mgr.go index 4af40bd33..ded38e4a5 100644 --- a/src/jobservice/opm/redis_job_stats_mgr.go +++ b/src/jobservice/opm/redis_job_stats_mgr.go @@ -339,6 +339,20 @@ func (rjs *RedisJobStatsManager) reportStatus(jobID string, hookURL, status, che Status: status, CheckIn: checkIn, } + //Return the whole metadata of the job. + //To support forward compatibility, keep the original fields `Status` and `CheckIn`. + //TODO: If querying job stats causes performance issues, a two-level cache should be enabled. + jobStats, err := rjs.getJobStats(jobID) + if err != nil { + //Just logged + logger.Warningf("Retrieving stats of job %s for hook reporting failed with error: %s", jobID, err) + } else { + //Override status/check in message + //Just double confirmation + jobStats.Stats.CheckIn = checkIn + jobStats.Stats.Status = status + reportingStatus.Metadata = jobStats.Stats + } return DefaultHookClient.ReportStatus(hookURL, reportingStatus) } diff --git a/src/jobservice/opm/redis_job_stats_mgr_test.go b/src/jobservice/opm/redis_job_stats_mgr_test.go index 09716551a..88492425a 100644 --- a/src/jobservice/opm/redis_job_stats_mgr_test.go +++ b/src/jobservice/opm/redis_job_stats_mgr_test.go @@ -6,6 +6,7 @@ import ( "encoding/json" "errors" "fmt" + "io/ioutil" "net/http" "net/http/httptest" "os" @@ -175,6 +176,24 @@ func TestCheckIn(t *testing.T) { //Start http server ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + data, err := ioutil.ReadAll(r.Body) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + return + } + defer r.Body.Close() + + statusReport := &models.JobStatusChange{} + if err := json.Unmarshal(data, statusReport); err != nil { + w.WriteHeader(http.StatusInternalServerError) + return + } + + if statusReport.Metadata == nil || statusReport.Metadata.JobID != "fake_job_ID" { + w.WriteHeader(http.StatusInternalServerError) + return + } + fmt.Fprintln(w, "ok") })) defer ts.Close() @@ -204,7 +223,7 @@ func TestCheckIn(t *testing.T) { func getRedisHost() string { redisHost := os.Getenv(testingRedisHost) if redisHost == "" { - redisHost = "10.160.178.186" //for local test + redisHost = "localhost" //for local test } return redisHost