Beispiel #1
0
 def test_validate(self):
     q = HippoQueue(definition={'id':'foo','queue':{'name':'fooname','type':'footype'}},redis_client=self.redis_client)
     errors = q.validate()
     self.assertIsNotNone(errors)
     q.definition = {'mem': 32, 'cmd': "echo 'foo'", 'container': {'docker': {'image': 'busybox:latest'}},
                     'id': 'fooface', 'cpus': 0.1, 'queue':{'name':'fooname','type':'redisqueue'}}
     errors = q.validate()
     self.assertIsNone(errors)
Beispiel #2
0
 def test_processing(self):
     q = HippoQueue(definition={'mem': 32, 'cmd': "echo 'foo'",
                                'container': {'docker': {'image': 'busybox:latest'}},
                                 'id': 'fooface', 'cpus': 0.1, 'queue':{
                                     'name':'fooname','type':'test'}},
                    redis_client=self.redis_client)
     HippoQueue.process_queues(self.redis_client)
     time.sleep(1)
     HippoQueue.__stop_processing=True
Beispiel #3
0
    def test_hippodatasource(self):
        q = HippoQueue(definition={'id':'foo','cmd':'echo $HIPPO_DATA','env':{'foo':'$HIPPO_DATA_BASE64'},
                                   'queue':{'name':'fooname','last_run_tstamp':time.time(),'frequency_seconds':1}},
                       redis_client=self.redis_client)

        hds = HippoDataSource(q,0,HippoTask,self.redis_client)
        self.assertTrue(hds.too_soon())
        hds.process_source()

        hds.create_tasks(['footask1','footask2'])
        waiting_tasks = HippoTask.waiting_tasks(self.redis_client)
        self.assertEqual(len(waiting_tasks),2)
        self.assertEqual(waiting_tasks[0].definition['cmd'],'echo footask2')
Beispiel #4
0
def single_queue(queue_id):
    q = HippoQueue(id=queue_id, redis_client=app.redis)
    if not q.definition:
        return jsonify({"error": queue_id + " not found"}), 404

    if request.method == 'DELETE':
        q.delete()
        return jsonify({"deleted": queue_id})
    elif request.method in ['POST', 'PUT']:
        data = request.get_json()
        q.definition = data
        if 'id' not in q.definition:
            q.definition['id'] = q.id
        validation_error = q.validate()
        if validation_error:
            return jsonify({"error": validation_error}), 400
        q.save()

    return jsonify(q.definition)
Beispiel #5
0
def single_queue_enable_toggle(queue_id, toggle):
    q = HippoQueue(id=queue_id, redis_client=app.redis)
    if not q.definition:
        return jsonify({"error": queue_id + " not found"}), 404

    if toggle == 'enable':
        q.enable()
        return jsonify({"enabled": queue_id})
    else:
        q.disable()
        return jsonify({"disabled": queue_id})
Beispiel #6
0
def queues():
    if request.method in ['POST', 'PUT']:
        data = request.get_json()
        q = HippoQueue(definition=data, redis_client=app.redis)
        validation_error = q.validate()
        if validation_error:
            q.delete()
            return jsonify({"error": validation_error}), 400
        return jsonify({"id": q.id})
    else:
        all_queues = HippoQueue.all_queues(app.redis)

    return jsonify([q.definition for q in all_queues])
Beispiel #7
0
 def test_create(self):
     q = HippoQueue(definition={'id':'foo','queue':{'name':'fooname'}},redis_client=self.redis_client)
     self.assertEqual(len(HippoQueue.all_queues(self.redis_client)),1)
     q2 = HippoQueue(id='foo',redis_client=self.redis_client)
     self.assertEqual(q2.definition['queue']['name'],'fooname')
     self.assertEqual(len(HippoQueue.all_queues(self.redis_client)),1)
Beispiel #8
0
def leader():
    logging.info('Elected as leader, starting work...')

    redis_client = redis.StrictRedis(host=config.REDIS_HOST, port=config.REDIS_PORT, db=config.REDIS_DB, password=config.REDIS_PW)

    saved_framework_id = redis_client.get('hippo:saved_framework_id')

    framework = Dict()
    framework.user = '******'
    framework.name = "Hippo"
    framework.hostname = os.getenv('HOST',socket.gethostname())
    framework.checkpoint = True
    framework.failover_timeout = 86400.0
    if saved_framework_id:
        framework.id = {"value":saved_framework_id.decode()}


    driver = MesosSchedulerDriver(
        HippoScheduler(redis_client),
        framework,
        config.MESOS_HOST if config.MESOS_HOST else config.ZK_URI,
        use_addict=True,
    )

    def signal_handler(signal, frame):
        driver.stop(failover=True)

    def run_driver_thread():
        driver.run()

    driver_thread = Thread(target=run_driver_thread, args=())
    driver_thread.start()
    logging.info('Started mesos schedule driver thread')

    signal.signal(signal.SIGINT, signal_handler)

    # reconcile will run every 15 minutes in it's own thread
    reconcile_thread = reconcile(driver, redis_client)
    logging.info('Started reconcile task thread')


    # kill task will run every 2 seconds in it's own thread to kill any tasks that need killin'
    kill_thread = kill_task(driver, redis_client)
    logging.info('Started kill task thread')


    # hippo queue will run a thread pool to monitor queues for work and create tasks
    process_queue_thread = HippoQueue.process_queues(redis_client)
    logging.info('Started queue processing thread')

    while driver_thread.is_alive() and \
          reconcile_thread.is_alive() and \
          kill_thread.is_alive() and \
          process_queue_thread.is_alive():
        # main thread just sleeps as long as all the child threads are still running
        time.sleep(1)

        # if registered and not using a saved framework id, save this one
        if driver.framework_id and not saved_framework_id:
            saved_framework_id = driver.framework_id
            redis_client.set('hippo:saved_framework_id',saved_framework_id)
            logging.info('saving framework id ' + driver.framework_id)

    logging.info('...Exiting')
    exit(0)