MediumSchedulingPython 3

Multi-tenant Job Scheduler

Dispatch queued jobs by priority while preventing one tenant from monopolizing workers.

45m3 sample tests6 hidden tests

Implement FairJobScheduler, a priority scheduler with tenant fairness and cancellation.

Requirements

  • Constructor receives max_consecutive_per_org. Raise ValueError if max_consecutive_per_org <= 0.
  • enqueue(org, job_id, priority) adds a job. Lower priority number runs first. Raise ValueError if job_id is already active (pending, including soft-canceled jobs that have not yet been reaped).
  • cancel(job_id) marks a pending job canceled and returns whether it was still active. After a job has been successfully dispatched, later cancel returns False.
  • dispatch(count) returns up to count job IDs.
  • FIFO order breaks ties within the same priority.
  • When the fairness cap blocks the next job from last_org, pick the highest-priority pending job from another org (FIFO among equals).
  • If another org has pending work, dispatch may not return more than max_consecutive_per_org jobs from the same org in a row. The fairness streak is global and persists across separate dispatch calls.
  • Canceled jobs are skipped and reaped (their IDs leave the active set) when dropped from the pending queue.

Example

Although b1 has lower priority than a3, the two-job fairness cap gives organization b a turn before more work from a.

python
1s = FairJobScheduler(2) 2s.enqueue("a", "a1", 1) 3s.enqueue("a", "a2", 1) 4s.enqueue("a", "a3", 1) 5s.enqueue("b", "b1", 5) 6assert s.dispatch(4) == ["a1", "a2", "b1", "a3"]

Constraints

  • Keep state in memory.
  • Optimize for correctness and clear invariants before heap efficiency.

Editor