Skip to content
Snippets Groups Projects
Commit ce2fdfdf authored by Erik Reid's avatar Erik Reid
Browse files

api for checking task status

parent 8e262e25
No related branches found
No related tags found
No related merge requests found
from flask import Blueprint, Response, current_app from flask import Blueprint, Response, current_app, jsonify
from inventory_provider.tasks import worker from inventory_provider.tasks import worker
routes = Blueprint("inventory-data-job-routes", __name__) routes = Blueprint("inventory-data-job-routes", __name__)
...@@ -6,10 +6,9 @@ routes = Blueprint("inventory-data-job-routes", __name__) ...@@ -6,10 +6,9 @@ routes = Blueprint("inventory-data-job-routes", __name__)
@routes.route("/update", methods=['GET', 'POST']) @routes.route("/update", methods=['GET', 'POST'])
def update(): def update():
worker.start_refresh_cache_all( job_ids = worker.launch_refresh_cache_all(
current_app.config["INVENTORY_PROVIDER_CONFIG"]) current_app.config["INVENTORY_PROVIDER_CONFIG"])
return Response("OK") return jsonify(job_ids)
@routes.route("update-interface-statuses") @routes.route("update-interface-statuses")
def update_interface_statuses(): def update_interface_statuses():
...@@ -21,3 +20,7 @@ def update_interface_statuses(): ...@@ -21,3 +20,7 @@ def update_interface_statuses():
def reload_router_config(equipment_name): def reload_router_config(equipment_name):
worker.reload_router_config.delay(equipment_name) worker.reload_router_config.delay(equipment_name)
return Response("OK") return Response("OK")
@routes.route("check-task-status/<task_id>")
def check_task_status(task_id):
return jsonify(worker.check_task_status(task_id))
...@@ -2,7 +2,9 @@ import json ...@@ -2,7 +2,9 @@ import json
import logging import logging
import re import re
from celery import bootsteps, Task, group from celery import bootsteps, Task, group, states
from celery.result import AsyncResult
from collections import defaultdict from collections import defaultdict
from lxml import etree from lxml import etree
...@@ -323,7 +325,7 @@ def _derive_router_hostnames(config): ...@@ -323,7 +325,7 @@ def _derive_router_hostnames(config):
return junosspace_equipment & opsdb_equipment return junosspace_equipment & opsdb_equipment
def start_refresh_cache_all(config): def launch_refresh_cache_all(config):
""" """
utility function intended to be called outside of the worker process utility function intended to be called outside of the worker process
:param config: config structure as defined in config.py :param config: config structure as defined in config.py
...@@ -353,4 +355,17 @@ def start_refresh_cache_all(config): ...@@ -353,4 +355,17 @@ def start_refresh_cache_all(config):
'queueing router refresh jobs for %r' % hostname) 'queueing router refresh jobs for %r' % hostname)
subtasks.append(reload_router_config.s(hostname)) subtasks.append(reload_router_config.s(hostname))
return group(subtasks).apply_async() return [r.id for r in group(subtasks).apply_async()]
def check_task_status(task_id):
r = AsyncResult(task_id, app=app)
return {
'id': task_id,
'status': r.status,
'exception': r.status in states.EXCEPTION_STATES,
'ready': r.status in states.READY_STATES,
'success': r.status == states.SUCCESS,
'result': r.result
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment