As discussed on the dev list , allow Jobs to specify a priority and be queued accordingly by the JobPoller so that important jobs can be prioritized over normal jobs, and low priority jobs can be left until last.
This is a first pass implementation with just the main changes. We will still need to allow the priority to be set in the JobManager and LocalDispatcher's "schedule" methods before jobs generated at runtime can be prioritized. IMO the existing schedule methods are a mess and need to be converted into a builder pattern before adding any more job attributes. The new priority field also needs to be added to the webtools UI wherever appropriate.
Changes are as follows:
- Add a "priority" field to JobSandbox entity (numeric/Long)
- Add JobPriority constants class containing fields LOW (0), NORMAL (50) and HIGH (100)
- Add getPriority method to the Job interface and implement methods for AbstractJob (returns NORMAL), PersistedServiceJob (returns JobSandbox.priority) and PurgeJob (returns LOW)
- Change the JobPoller executor's queue to use PriorityBlockingQueue (unbounded) instead of LinkedBlockingQueue (bounded)
- Implement custom Comparator for the priority queue to sort by priority descending and then runTime ascending
- Change the poll size per poll to be (queueSize() - queue.size) instead of queue.remainingCapacity() due to the new queue being unbounded
- I've also opted to limit the database poll query to the poll size using maxRows() because it seemed dangerous to me to use an unconstrained query on this table
- Ensured recurring jobs receive the default (NORMAL) priority when being rescheduled so that they're sorted correctly on the next time they show up in the database poll
- Ensured jobs generated at runtime are given a default priority of NORMAL