Merge pull request #5762 from steven-zou/enhance_js_webhook

Return more data of the job when reporting status info via webhook
This commit is contained in:
Steven Zou 2018-08-30 16:40:13 +08:00 committed by GitHub
commit 35603288e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 4 deletions

View File

@ -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

View File

@ -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)
}

View File

@ -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