Write a Job Dispatcher-Executor system.
You are tasked with implementing a simple job dispatcher that distributes incoming jobs to a pool of executors (i.e., workers). Each job is represented as an integer (its ID), and executors are dynamically added and removed during runtime.
You must define two classes:
- Executor – Represents a worker that can accept jobs.
- LoadBalancer – Maintains a list of executors and distributes jobs to them.
class Executor
Each executor has:
- A method assign_job(job_id: int) which simply stores the job.
- A method execute_next_job() which removes the next job from the executor's job store
class JobDispatcher
The job dispatcher has:
- A method add_executor(executor: Executor) to add an executor.
- A method remove_executor(executor_id: str) to remove an executor.
- A method dispatch(job_id: int) to assign a job to one of the executors.
- A method get_state() that returns a mapping of executor_id → job list.
Pay attention to how you design the dispatch mechanism, because it will affect your final implementation. The strategy is lowest job load > round-robin.
By default the executors all handle the same type of task, so you don't need to think about the actual task-processing logic.
Also, when you remove an executor, remember to take the jobs currently queued on it and redistribute them to the remaining executors.
Discussion
Loading comments…