Fix cnt query scan. Cosmetic changes.

This commit is contained in:
Jan Eitzinger 2022-11-25 15:15:05 +01:00
parent 948da8f10b
commit fd16a1b637
3 changed files with 16 additions and 8 deletions

View File

@ -630,7 +630,7 @@ func (api *RestApi) deleteJobByRequest(rw http.ResponseWriter, r *http.Request)
// deleteJobBefore godoc // deleteJobBefore godoc
// @summary Remove a job from the sql database // @summary Remove a job from the sql database
// @tags remove // @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 // @produce json
// @param ts path int true "Unix epoch timestamp" // @param ts path int true "Unix epoch timestamp"
// @success 200 {object} api.DeleteJobApiResponse "Success message" // @success 200 {object} api.DeleteJobApiResponse "Success message"

View File

@ -248,16 +248,24 @@ func (r *JobRepository) Stop(
func (r *JobRepository) DeleteJobsBefore(startTime int64) (int, error) { func (r *JobRepository) DeleteJobsBefore(startTime int64) (int, error) {
var cnt int var cnt int
q := sq.Select("COUNT(*)").From("job").Where("job.start_time < ?", startTime) qs := fmt.Sprintf("SELECT count(*) FROM job WHERE job.start_time < %d", startTime)
q.QueryRow().Scan(&cnt) 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)
_, 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 return cnt, err
} }
func (r *JobRepository) DeleteJobById(id int64) error { func (r *JobRepository) DeleteJobById(id int64) error {
_, err := r.DB.Exec(`DELETE FROM job WHERE job.id = ?`, id) _, 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 return err
} }

View File

@ -343,7 +343,7 @@ func TestRestApi(t *testing.T) {
restapi.MountRoutes(r) restapi.MountRoutes(r)
const startJobBody string = `{ const startJobBody string = `{
"jobId": 123, "jobId": 123,
"user": "testuser", "user": "testuser",
"project": "testproj", "project": "testproj",
"cluster": "testcluster", "cluster": "testcluster",
@ -542,7 +542,7 @@ func subtestLetJobFail(t *testing.T, restapi *api.RestApi, r *mux.Router) {
} }
const stopJobBody string = `{ const stopJobBody string = `{
"jobId": 12345, "jobId": 12345,
"cluster": "testcluster", "cluster": "testcluster",
"jobState": "failed", "jobState": "failed",