From fd16a1b637498f510a5b993a5f512c3aad6d55b2 Mon Sep 17 00:00:00 2001 From: Jan Eitzinger Date: Fri, 25 Nov 2022 15:15:05 +0100 Subject: [PATCH] Fix cnt query scan. Cosmetic changes. --- internal/api/rest.go | 2 +- internal/repository/job.go | 18 +++++++++++++----- test/integration_test.go | 4 ++-- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/internal/api/rest.go b/internal/api/rest.go index 97324c7..985e4ea 100644 --- a/internal/api/rest.go +++ b/internal/api/rest.go @@ -630,7 +630,7 @@ func (api *RestApi) deleteJobByRequest(rw http.ResponseWriter, r *http.Request) // deleteJobBefore godoc // @summary Remove a job from the sql database // @tags remove -// @description Job to stop is specified by database ID. This will not remove the job from the job archive. +// @description Remove all jobs with start time before timestamp. The jobs will not be removed from the job archive. // @produce json // @param ts path int true "Unix epoch timestamp" // @success 200 {object} api.DeleteJobApiResponse "Success message" diff --git a/internal/repository/job.go b/internal/repository/job.go index 49aa680..d1b2af2 100644 --- a/internal/repository/job.go +++ b/internal/repository/job.go @@ -248,16 +248,24 @@ func (r *JobRepository) Stop( func (r *JobRepository) DeleteJobsBefore(startTime int64) (int, error) { var cnt int - q := sq.Select("COUNT(*)").From("job").Where("job.start_time < ?", startTime) - q.QueryRow().Scan(&cnt) - - _, err := r.DB.Exec(`DELETE FROM job WHERE job.start_time < ?`, startTime) + qs := fmt.Sprintf("SELECT count(*) FROM job WHERE job.start_time < %d", startTime) + err := r.DB.Get(&cnt, qs) //ignore error as it will also occur in delete statement + _, err = r.DB.Exec(`DELETE FROM job WHERE job.start_time < ?`, startTime) + if err != nil { + log.Warnf(" DeleteJobsBefore(%d): error %v", startTime, err) + } else { + log.Infof("DeleteJobsBefore(%d): Deleted %d jobs", startTime, cnt) + } return cnt, err } func (r *JobRepository) DeleteJobById(id int64) error { - _, err := r.DB.Exec(`DELETE FROM job WHERE job.id = ?`, id) + if err != nil { + log.Warnf("DeleteJobById(%d): error %v", id, err) + } else { + log.Infof("DeleteJobById(%d): Success", id) + } return err } diff --git a/test/integration_test.go b/test/integration_test.go index a7753fd..d4455ac 100644 --- a/test/integration_test.go +++ b/test/integration_test.go @@ -343,7 +343,7 @@ func TestRestApi(t *testing.T) { restapi.MountRoutes(r) const startJobBody string = `{ -"jobId": 123, + "jobId": 123, "user": "testuser", "project": "testproj", "cluster": "testcluster", @@ -542,7 +542,7 @@ func subtestLetJobFail(t *testing.T, restapi *api.RestApi, r *mux.Router) { } const stopJobBody string = `{ -"jobId": 12345, + "jobId": 12345, "cluster": "testcluster", "jobState": "failed",