From b0403da93bc7e8a13ab369e6bed366c856f9b3b7 Mon Sep 17 00:00:00 2001 From: Jan Eitzinger Date: Tue, 15 Feb 2022 11:33:59 +0100 Subject: [PATCH] Add error handling to stop job --- api/rest.go | 5 ++++- repository/job.go | 7 +++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/api/rest.go b/api/rest.go index f7e3663..f421a1f 100644 --- a/api/rest.go +++ b/api/rest.go @@ -339,10 +339,13 @@ func (api *RestApi) stopJob(rw http.ResponseWriter, r *http.Request) { } } - api.JobRepository.Stop(job.ID, job.Duration, req.State) + err = api.JobRepository.Stop(job.ID, job.Duration, req.State) + job.State = req.State rw.Header().Add("Content-Type", "application/json") rw.WriteHeader(http.StatusOK) json.NewEncoder(rw).Encode(job) + handleError(fmt.Errorf("Stop job (dbid: %d) failed: %s", job.ID, err.Error()), http.StatusInternalServerError, rw) + // handleError(fmt.Errorf("archiving failed: %w", err), http.StatusInternalServerError, rw) } func (api *RestApi) getJobMetrics(rw http.ResponseWriter, r *http.Request) { diff --git a/repository/job.go b/repository/job.go index 1f09c7d..86306de 100644 --- a/repository/job.go +++ b/repository/job.go @@ -78,16 +78,15 @@ func (r *JobRepository) Start(job *schema.JobMeta) (id int64, err error) { func (r *JobRepository) Stop( jobId int64, duration int32, - state schema.JobState) { + state schema.JobState) (err error) { stmt := sq.Update("job"). Set("job_state", state). Set("duration", duration). Where("job.id = ?", jobId) - if _, err := stmt.RunWith(r.DB).Exec(); err != nil { - log.Errorf("Stop job (dbid: %d) failed: %s", jobId, err.Error()) - } + _, err = stmt.RunWith(r.DB).Exec() + return } // Stop updates the job with the database id jobId using the provided arguments.