Optimize uniqueByOwner job query, delete old failed jobs

This should probably be backported into 8.34 hotfix.

Replaced the detached query from getByClassAndOwner with a sort, hopefully there will be some speedup.
Remove old failed jobs (3-4 days+) to keep job status query time down.