Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
the tiniest mesos scheduler
import logging
import uuid
import time
from mesos.interface import Scheduler
from mesos.native import MesosSchedulerDriver
from mesos.interface import mesos_pb2
def new_task(offer):
task = mesos_pb2.TaskInfo()
id = uuid.uuid4()
task.task_id.value = str(id)
task.slave_id.value = offer.slave_id.value = "task {}".format(str(id))
cpus = task.resources.add() = "cpus"
cpus.type = mesos_pb2.Value.SCALAR
cpus.scalar.value = 1
mem = task.resources.add() = "mem"
mem.type = mesos_pb2.Value.SCALAR
mem.scalar.value = 1
return task
class HelloWorldScheduler(Scheduler):
def registered(self, driver, framework_id, master_info):"Registered with framework id: {}".format(framework_id))
def resourceOffers(self, driver, offers):"Recieved resource offers: {}".format([ for o in offers]))
# whenever we get an offer, we accept it and use it to launch a task that
# just echos hello world to stdout
for offer in offers:
task = new_task(offer)
task.command.value = "echo hello world"
time.sleep(2)"Launching task {task} "
"using offer {offer}.".format(task=task.task_id.value,
tasks = [task]
driver.launchTasks(, tasks)
if __name__ == '__main__':
framework = mesos_pb2.FrameworkInfo()
framework.user = "" # Have Mesos fill in the current user. = "hello-world"
driver = MesosSchedulerDriver(
"zk://localhost:2181/mesos" # assumes running on the master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment