Commit 9aea76c1 authored by Laura Médioni's avatar Laura Médioni
Browse files

[views] display progress bar

Add a property allowing to get task progress from celery in CeleryTask entity class.

This implies that the celery tasks must regularly update their status with the
following structure : {'progress': progress_ratio}, with progress_ratio being
a floating number between 0 and 1.

If it is not the case, the progress bar will pass from grey to blue in one go, when
the task state passes to 'done'.

closes #14983709
parent 74956aa7baf9
/*
This program is free software: you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation, either version 2.1 of the License, or (at your option)
any later version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
details.
You should have received a copy of the GNU Lesser General Public License along
with this program. If not, see <http://www.gnu.org/licenses/>.
*/
cw.celerytask = {
autorefreshprogress: function(eid) {
function refresh() {
$('#js-cw-celerytask').loadxhtml(
AJAX_BASE_URL,
ajaxFuncArgs('view', {eid: eid, vid: 'celerytask.task_progress_bar'}));
}
setTimeout(refresh, 1000);
}
};
......@@ -92,6 +92,25 @@ class ICeleryTask(EntityAdapter):
def result(self):
return AsyncResult(self.task_id)
@property
def progress(self):
result = celery.result.AsyncResult(self.task_id)
if result.state == u'PROGRESS':
return result.info['progress']
elif result.state == u'SUCCESS':
return 1.
else:
return 0.
@property
def state(self):
result = celery.result.AsyncResult(self.task_id)
return result.state
@property
def finished(self):
return self.state in (u'SUCCESS', u'FAILURE', u'REVOKED')
def get_task_id(task):
......
......@@ -21,7 +21,7 @@ import logging
from logilab.mtconverter import xml_escape
from cubicweb import _
from cubicweb import _, tags
from cubicweb.view import EntityView
from cubicweb.predicates import is_instance, match_view
from cubicweb.web.views import uicfg, tabs
......@@ -47,6 +47,10 @@ class CeleryTaskPrimaryTab(tabs.PrimaryTab):
__regid__ = 'celerytask.task_general_information'
__select__ = EntityView.__select__ & is_instance('CeleryTask')
def entity_call(self, entity):
entity.view('celerytask.task_progress_bar', w=self.w)
super(CeleryTaskPrimaryTab, self).entity_call(entity)
class CeleryTaskLogs(tabs.TabsMixin, EntityView):
"""View for CeleryTask logs, only displayed if there are some"""
......@@ -93,3 +97,24 @@ def log_to_table(logs):
except (ValueError, KeyError):
rows[-1][-1] += '\n' + line
return rows
class CeleryTaskProgressBarView(EntityView):
__select__ = EntityView.__select__ & is_instance('CeleryTask')
__regid__ = 'celerytask.task_progress_bar'
def entity_call(self, entity, **kwargs):
adapted = entity.cw_adapt_to('ICeleryTask')
if not adapted.finished:
self._cw.add_js(('cubicweb.htmlhelpers.js', 'cubicweb.ajax.js',
'cubes.celerytask.js'))
self._cw.add_onload(
'cw.celerytask.autorefreshprogress("%s");' % entity.eid)
state = entity.cw_adapt_to('IWorkflowable').state
div_classes = ['cw-celerytask-progress', state]
progratio = adapted.progress
progress = u'%.0f %%' % (100 * progratio)
self.w(tags.div(
tags.tag('progress')(progress, max=u'1', value=unicode(progratio)),
klass=' '.join(div_classes),
title=state, id=u'js-cw-celerytask'))
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment