diff --git a/inventory_provider/routes/jobs.py b/inventory_provider/routes/jobs.py index 87a9f29fd7a7111869548cc5c5f8270021502eb1..5d0bd12616bf234113cceba106ecdea4c71804d8 100644 --- a/inventory_provider/routes/jobs.py +++ b/inventory_provider/routes/jobs.py @@ -1,3 +1,4 @@ +import json import logging from distutils.util import strtobool @@ -75,5 +76,54 @@ def check_update_status(): @routes.route("log", methods=['GET', 'POST']) @common.require_accepts_json def load_task_log(): - tasks = monitor.load_task_log(current_app.config['INVENTORY_PROVIDER_CONFIG']) - return jsonify(tasks) + + FINALIZATION_EVENTS = {'task-succeeded', 'task-failed', 'task-revoked'} + + config = current_app.config['INVENTORY_PROVIDER_CONFIG'] + r = get_current_redis(config) + + cache_key = 'joblog:cached-response' + result = r.get(cache_key) + if result: + result = json.loads(result.decode('utf-8')) + else: + result = { + 'pending': [], + 'warnings': [], + 'errors': [], + 'failed': [] + } + + for task in monitor.load_task_log(current_app.config['INVENTORY_PROVIDER_CONFIG']).values(): + for event in task.get('task-warning', []): + result['warnings'].append(event['message']) + for event in task.get('task-error', []): + result['errors'].append(event['message']) + + # build the description if task-received is available + description = None + if 'task-received' in task: + event = task['task-received'][0] + description = f'{event["name"]}{event["args"]}' + description += f':{event["uuid"]}' + + if 'task-failed' in task: + if not description: + logger.error('found task-failed event without' + f'task-received: {task}') + description = task['task-failed'][0]['uuid'] + result['failed'].append(description) + + if 'task-started' in task: + finished = set(task.keys()) & FINALIZATION_EVENTS + if not finished: + if not description: + logger.error('found task-started event without' + f'task-received: {task}') + description = task['task-started'][0]['uuid'] + result['pending'].append(description) + + if not result['pending']: + r.set(cache_key, json.dumps(result)) + + return jsonify(result)