jobqueue: Add DeleteJob function

This allows jobs to be deleted from the database.
Currently only implemented by fsjobqueue. The function for
dbjobqueue currently returns nil.

This will remove all the job files used by the root job UUID as long as
no other job depends on them. ie. It starts at the top, and moves down
the dependency tree until it finds a job that is also used by another
job, removes the job to be deleted from its dependants list, and moves
back up the tree only deleting jobs with empty dependants lists.

Related: RHEL-60120
This commit is contained in:
Brian C. Lane 2024-04-16 10:46:28 -07:00 committed by Tomáš Hozza
parent 5961b69caa
commit d8285a0b74
4 changed files with 134 additions and 0 deletions

View file

@ -726,3 +726,48 @@ func (q *fsJobQueue) AllRootJobIDs() ([]uuid.UUID, error) {
return jobIDs, nil
}
// DeleteJob will delete a job and all of its dependencies
// If a dependency has multiple depenents it will only delete the parent job from
// the dependants list and then re-save the job instead of removing it.
//
// This assumes that the jobs have been created correctly, and that they have
// no dependency loops. Shared Dependants are ok, but a job cannot have a dependancy
// on any of its parents (this should never happen).
func (q *fsJobQueue) DeleteJob(_ context.Context, id uuid.UUID) error {
// Start it off with an empty parent
return q.deleteJob(uuid.UUID{}, id)
}
// deleteJob will delete jobs as far down the list as possible
// missing dependencies are ignored, it deletes as much as it can.
// A missing parent (the first call) will be returned as an error
func (q *fsJobQueue) deleteJob(parent, id uuid.UUID) error {
var j job
_, err := q.db.Read(id.String(), &j)
if err != nil {
return err
}
// Delete the parent uuid from the Dependents list
var deps []uuid.UUID
for _, d := range j.Dependents {
if d == parent {
continue
}
deps = append(deps, d)
}
j.Dependents = deps
// This job can only be deleted when the Dependents list is empty
// Otherwise it needs to be saved with the new Dependents list
if len(j.Dependents) > 0 {
return q.db.Write(id.String(), j)
}
// Recursively delete the dependencies of this job
for _, dj := range j.Dependencies {
_ = q.deleteJob(id, dj)
}
return q.db.Delete(id.String())
}