Don't Panic in getComposeStatus and skip invalid jobs in fsjobqueue New

This handles corrupt job json files by skipping them. They still exist,
and errors are logged, but the system keeps working.

If one or more of the json files in /var/lib/osbuild-composer/jobs/
becomes corrupt they can stop the osbuild-composer service from
starting, or stop commands like 'composer-cli compose status' from
working because they quit on the first error and miss any job that
aren't broken.
This commit is contained in:
Brian C. Lane 2023-11-06 13:21:18 -08:00 committed by Ondřej Budai
parent e969a9dc3c
commit aca748bc14
6 changed files with 291 additions and 18 deletions

View file

@ -106,7 +106,8 @@ func New(dir string) (*fsJobQueue, error) {
}
j, err := q.readJob(jobId)
if err != nil {
return nil, err
// Skip invalid jobs, leaving them in place for later examination
continue
}
// If a job is running, and not cancelled, track the token

View file

@ -1,6 +1,8 @@
package fsjobqueue_test
import (
"os"
"path"
"testing"
"github.com/osbuild/osbuild-composer/pkg/jobqueue"
@ -28,3 +30,15 @@ func TestNonExistant(t *testing.T) {
require.Error(t, err)
require.Nil(t, q)
}
func TestJobQueueBadJSON(t *testing.T) {
dir := t.TempDir()
// Write a purposfully invalid JSON file into the queue
err := os.WriteFile(path.Join(dir, "/4f1cf5f8-525d-46b7-aef4-33c6a919c038.json"), []byte("{invalid json content"), 0600)
require.Nil(t, err)
q, err := fsjobqueue.New(dir)
require.Nil(t, err)
require.NotNil(t, q)
}