task manager using messages

* First pass, adapt singleton task manager to process messages and run
jobs based on events instead of a busy loop.
* Still need to make message handing run in celery, not in a consumption
loop
This commit is contained in:
Chris Meyers
2016-09-20 10:14:38 -04:00
parent 609a3e6f2f
commit cc90204b0f
8 changed files with 386 additions and 250 deletions

View File

@@ -31,6 +31,9 @@ except:
# Pexpect
import pexpect
# Kombu
from kombu import Connection, Exchange, Queue, Producer
# Celery
from celery import Task, task
from celery.signals import celeryd_init
@@ -202,6 +205,18 @@ def _send_notification_templates(instance, status_str):
for n in all_notification_templates],
job_id=instance.id)
def _send_job_complete_msg(instance):
connection = Connection(settings.BROKER_URL)
exchange = Exchange(settings.SCHEDULER_QUEUE, type='topic')
producer = Producer(connection)
producer.publish({ 'job_id': instance.id, 'msg_type': 'job_complete' },
serializer='json',
compression='bzip2',
exchange=exchange,
declare=[exchange],
routing_key='scheduler.job.complete')
@task(bind=True, queue='default')
def handle_work_success(self, result, task_actual):
instance = UnifiedJob.get_instance_by_type(task_actual['type'], task_actual['id'])
@@ -210,6 +225,8 @@ def handle_work_success(self, result, task_actual):
_send_notification_templates(instance, 'succeeded')
_send_job_complete_msg(instance)
@task(bind=True, queue='default')
def handle_work_error(self, task_id, subtasks=None):
print('Executing error task id %s, subtasks: %s' %
@@ -238,6 +255,9 @@ def handle_work_error(self, task_id, subtasks=None):
if first_instance:
_send_notification_templates(first_instance, 'failed')
if first_instance:
_send_job_complete_msg(first_instance)
@task(queue='default')
def update_inventory_computed_fields(inventory_id, should_update_hosts=True):