Fix bug in archiving

This commit is contained in:
Lou Knauer 2021-12-08 11:50:47 +01:00
parent eb2df5aa1c
commit 45dc12cc0c
2 changed files with 13 additions and 7 deletions

View File

@ -20,13 +20,17 @@ import (
// For a given job, return the path of the `data.json`/`meta.json` file. // For a given job, return the path of the `data.json`/`meta.json` file.
// TODO: Implement Issue ClusterCockpit/ClusterCockpit#97 // TODO: Implement Issue ClusterCockpit/ClusterCockpit#97
func getPath(job *model.Job, file string) (string, error) { func getPath(job *model.Job, file string, checkLegacy bool) (string, error) {
id, err := strconv.Atoi(strings.Split(job.JobID, ".")[0]) id, err := strconv.Atoi(strings.Split(job.JobID, ".")[0])
if err != nil { if err != nil {
return "", err return "", err
} }
lvl1, lvl2 := fmt.Sprintf("%d", id/1000), fmt.Sprintf("%03d", id%1000) lvl1, lvl2 := fmt.Sprintf("%d", id/1000), fmt.Sprintf("%03d", id%1000)
if !checkLegacy {
return filepath.Join(JobArchivePath, job.ClusterID, lvl1, lvl2, strconv.FormatInt(job.StartTime.Unix(), 10), file), nil
}
legacyPath := filepath.Join(JobArchivePath, job.ClusterID, lvl1, lvl2, file) legacyPath := filepath.Join(JobArchivePath, job.ClusterID, lvl1, lvl2, file)
if _, err := os.Stat(legacyPath); errors.Is(err, os.ErrNotExist) { if _, err := os.Stat(legacyPath); errors.Is(err, os.ErrNotExist) {
return filepath.Join(JobArchivePath, job.ClusterID, lvl1, lvl2, strconv.FormatInt(job.StartTime.Unix(), 10), file), nil return filepath.Join(JobArchivePath, job.ClusterID, lvl1, lvl2, strconv.FormatInt(job.StartTime.Unix(), 10), file), nil
@ -37,7 +41,7 @@ func getPath(job *model.Job, file string) (string, error) {
// Assuming job is completed/archived, return the jobs metric data. // Assuming job is completed/archived, return the jobs metric data.
func loadFromArchive(job *model.Job) (schema.JobData, error) { func loadFromArchive(job *model.Job) (schema.JobData, error) {
filename, err := getPath(job, "data.json") filename, err := getPath(job, "data.json", true)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -63,7 +67,7 @@ func UpdateTags(job *model.Job, tags []*model.JobTag) error {
return nil return nil
} }
filename, err := getPath(job, "meta.json") filename, err := getPath(job, "meta.json", true)
if err != nil { if err != nil {
return err return err
} }
@ -106,7 +110,7 @@ func UpdateTags(job *model.Job, tags []*model.JobTag) error {
// Helper to metricdata.LoadAverages(). // Helper to metricdata.LoadAverages().
func loadAveragesFromArchive(job *model.Job, metrics []string, data [][]schema.Float) error { func loadAveragesFromArchive(job *model.Job, metrics []string, data [][]schema.Float) error {
filename, err := getPath(job, "meta.json") filename, err := getPath(job, "meta.json", true)
if err != nil { if err != nil {
return err return err
} }
@ -191,7 +195,7 @@ func ArchiveJob(job *model.Job, ctx context.Context) error {
} }
} }
dirPath, err := getPath(job, "") dirPath, err := getPath(job, "", false)
if err != nil { if err != nil {
return err return err
} }
@ -218,7 +222,7 @@ func ArchiveJob(job *model.Job, ctx context.Context) error {
return err return err
} }
writer = bufio.NewWriter(f) writer = bufio.NewWriter(f)
if err := json.NewEncoder(writer).Encode(metaData); err != nil { if err := json.NewEncoder(writer).Encode(jobData); err != nil {
return err return err
} }
if err := writer.Flush(); err != nil { if err := writer.Flush(); err != nil {

View File

@ -126,7 +126,9 @@ func stopJob(rw http.ResponseWriter, r *http.Request) {
return return
} }
if _, err := db.Exec(`UPDATE job SET job_state = ? WHERE job.id = ?`, model.JobStateCompleted, job.ID); err != nil { if _, err := db.Exec(
`UPDATE job SET job_state = ?, duration = ? WHERE job.id = ?`,
model.JobStateCompleted, job.Duration, job.ID); err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError) http.Error(rw, err.Error(), http.StatusInternalServerError)
return return
} }