From b644d3de08c60f248ee34aa52df562a1966c67a3 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Sep 23 2018 10:58:40 +0000 Subject: Make the internal endpoint return a task id and the client query this task Signed-off-by: Pierre-Yves Chibon --- diff --git a/pagure/internal/__init__.py b/pagure/internal/__init__.py index 48862d1..ba562c9 100644 --- a/pagure/internal/__init__.py +++ b/pagure/internal/__init__.py @@ -296,29 +296,16 @@ def get_pull_request_ready_branch(): ) response.status_code = 400 return response - result = pagure.lib.tasks.pull_request_ready_branch.delay( + task = pagure.lib.tasks.pull_request_ready_branch.delay( namespace=args_namespace, name=args_reponame, user=args_user, ) - try: - message = result.get(timeout=100) - except celery.exceptions.TimeoutError: - result.forget() - response = flask.jsonify( - { - "code": "ERROR", - "message": "Timed out while waiting for results", - } - ) - response.status_code = 400 - return response - return flask.jsonify( { "code": "OK", - "message": message, + "task": task.id, } ) diff --git a/pagure/templates/repo_master.html b/pagure/templates/repo_master.html index 0c2b400..654a3c8 100644 --- a/pagure/templates/repo_master.html +++ b/pagure/templates/repo_master.html @@ -394,76 +394,90 @@ window.addEventListener("load", function(event) { {% if g.authenticated and not g.repo_obj.is_empty %} {% if g.repo_committer %} -function process_async(url, _data, callback) { - $.post(url, _data, null, dataType='json') - .done(function(data) { - callback(data); +var _cnt = 0; + +function process_task_results(_data, callback) { + var _url = '{{ url_for("internal_ns.task_info", taskid='') }}' + _data.task; + $.ajax({ + url: _url , + type: 'GET', + data: { + js: 1, + count: _cnt, + }, + dataType: 'JSON', + success: function(res) { + callback(res.results); $("#spinnergif").hide(); - }) + }, + error: function(res) { + _cnt += 1; + if (_cnt < 600) { + window.setTimeout(process_task_results, 1000, _data, callback); + } + } + }); } + function generate_branch_list(data) { - if (data.code == 'OK') { - var _b = $("#dropdown-item-list"); - var first_item = true; - for (branch in data.message.new_branch){ - if (first_item){ - _b.prepend("") - first_item = false; - } - var url = "{{ url_for( - 'ui_ns.new_request_pull', - repo=repo.name, - username=repo.user.user if repo.is_fork else None, - namespace=repo.namespace, - branch_to='-', - branch_from='-') }}"; - url = url.slice(0, -4) + data.message.new_branch[branch]['target_branch'] + '..' + branch; - html = '' - + '' - + ' ' - + '{{ repo.name }}' - + ' ' - + '' - + ' ' - + branch - + ' '; - _b.prepend(html); + var _b = $("#dropdown-item-list"); + var first_item = true; + for (branch in data.new_branch){ + if (first_item){ + _b.prepend("") + first_item = false; } - _b.show(); - } + var url = "{{ url_for( + 'ui_ns.new_request_pull', + repo=repo.name, + username=repo.user.user if repo.is_fork else None, + namespace=repo.namespace, + branch_to='-', + branch_from='-') }}"; + url = url.slice(0, -4) + data.new_branch[branch]['target_branch'] + '..' + branch; + html = '' + + '' + + ' ' + + '{{ repo.name }}' + + ' ' + + '' + + ' ' + + branch + + ' '; + _b.prepend(html); + } + _b.show(); } function generate_branch_list_fork(data) { - if (data.code == 'OK') { - var _b = $("#dropdown-item-list"); - var first_item = true; - for (branch in data.message.new_branch){ - if (first_item){ - _b.prepend("") - first_item = false; - } - var url = "{{ url_for( - 'ui_ns.new_request_pull', - repo=repo.name, - username=g.fas_user.username, - namespace=repo.namespace, - branch_to='-', - branch_from='-') }}"; - url = url.slice(0, -4) + data.message.new_branch[branch]['target_branch'] + '..' + branch; - html = '' - + '' - + ' ' - + '{{ g.fas_user.username }}/{{ repo.name }} ' - + ' ' - + '' - + ' ' - + branch - + ' '; - _b.prepend(html); + var _b = $("#dropdown-item-list"); + var first_item = true; + for (branch in data.new_branch){ + if (first_item){ + _b.prepend("") + first_item = false; } - _b.show(); - } + var url = "{{ url_for( + 'ui_ns.new_request_pull', + repo=repo.name, + username=g.fas_user.username, + namespace=repo.namespace, + branch_to='-', + branch_from='-') }}"; + url = url.slice(0, -4) + data.new_branch[branch]['target_branch'] + '..' + branch; + html = '' + + '' + + ' ' + + '{{ g.fas_user.username }}/{{ repo.name }} ' + + ' ' + + '' + + ' ' + + branch + + ' '; + _b.prepend(html); + } + _b.show(); } $("#pr-button").one("click", @@ -497,14 +511,20 @@ $("#pr-button").one("click", repouser: "", csrf_token: "{{ g.confirmationform.csrf_token.current_token }}", }; - process_async(_pr_url, data, generate_branch_list); + $.post(_pr_url, data, null, dataType='json').done( + function(data) { + process_task_results(data, generate_branch_list); + }) var data = { repo: "{{ repo.name }}", namespace: "{{ repo.namespace if repo.namespace }}", repouser: "{{ g.fas_user.username }}", csrf_token: "{{ g.confirmationform.csrf_token.current_token }}", }; - process_async(_pr_url, data, generate_branch_list_fork); + $.post(_pr_url, data, null, dataType='json').done( + function(data) { + process_task_results(data, generate_branch_list); + }) _b.prepend(new_pr); } );