Skip to content

Commit

Permalink
create a handler can get job progress (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
lsy3993 authored and w41ter committed May 10, 2024
1 parent 15ddc34 commit f58dfb8
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions pkg/service/http_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,57 @@ func (s *HttpService) listJobsHandler(w http.ResponseWriter, r *http.Request) {
}
}

// get job progress
func (s *HttpService) jobProgressHandler(w http.ResponseWriter, r *http.Request) {
log.Infof("get job progress")

type result struct {
*defaultResult
JobProgress string `json:"job_progress"`
}

var jobResult *result
defer func() { writeJson(w, jobResult) }()

// Parse the JSON request body
var request CcrCommonRequest
err := json.NewDecoder(r.Body).Decode(&request)
if err != nil {
log.Warnf("get job progress failed: %+v", err)

jobResult = &result{
defaultResult: newErrorResult(err.Error()),
}
return
}

if request.Name == "" {
log.Warnf("get job progress failed: name is empty")

jobResult = &result{
defaultResult: newErrorResult("name is empty"),
}
return
}

if s.redirect(request.Name, w, r) {
return
}

if jobProgress, err := s.db.GetProgress(request.Name); err != nil {
log.Warnf("get job progress failed: %+v", err)
jobResult = &result{
defaultResult: newErrorResult(err.Error()),
}
} else {
jobResult = &result{
defaultResult: newSuccessResult(),
JobProgress: jobProgress,
}
}

}

// get job details
func (s *HttpService) jobDetailHandler(w http.ResponseWriter, r *http.Request) {
log.Infof("get job detail")
Expand Down Expand Up @@ -560,6 +611,7 @@ func (s *HttpService) RegisterHandlers() {
s.mux.HandleFunc("/update_job", s.updateJobHandler)
s.mux.HandleFunc("/list_jobs", s.listJobsHandler)
s.mux.HandleFunc("/job_detail", s.jobDetailHandler)
s.mux.HandleFunc("/job_progress", s.jobProgressHandler)
s.mux.Handle("/metrics", promhttp.Handler())
}

Expand Down

0 comments on commit f58dfb8

Please sign in to comment.