Skip to content
Snippets Groups Projects
jobs.py 1.29 KiB
from flask import Blueprint, current_app, jsonify, Response
from inventory_provider.tasks import worker
from inventory_provider.routes import common
from inventory_provider.tasks.common import get_current_redis, get_latch

routes = Blueprint("inventory-data-job-routes", __name__)


@routes.after_request
def after_request(resp):
    return common.after_request(resp)


@routes.route("/update", methods=['GET', 'POST'])
@common.require_accepts_json
def update():

    config = current_app.config['INVENTORY_PROVIDER_CONFIG']
    latch = get_latch(get_current_redis(config))
    if latch and latch['pending']:
        return Response(
            response='an update is already in progress',
            status=503,
            mimetype="text/html")

    job_ids = worker.launch_refresh_cache_all(
        current_app.config["INVENTORY_PROVIDER_CONFIG"])
    return jsonify(job_ids)


@routes.route("reload-router-config/<equipment_name>", methods=['GET', 'POST'])
@common.require_accepts_json
def reload_router_config(equipment_name):
    result = worker.reload_router_config.delay(equipment_name)
    return jsonify([result.id])


@routes.route("check-task-status/<task_id>", methods=['GET', 'POST'])
@common.require_accepts_json
def check_task_status(task_id):
    return jsonify(worker.check_task_status(task_id))