Commit 772e7ca6 authored by Florent Cayré's avatar Florent Cayré
Browse files

[views] Prefer celery reported state over DB state for progress-related views

This makes views mostly independant from celery monitor to be up and running.
Only the task's workflow relies on it. This db state is however useful for
celery tasks which have been whipped from the result backend.
parent 118a80f2aa13
......@@ -15,63 +15,63 @@ with this program. If not, see <http://www.gnu.org/licenses/>.
cw.celerytask = {
autorefreshprogress: function(eid) {
var oldState = null;
function refresh() {
$.getJSON(BASE_URL, {eid: eid, vid: 'celerytask.jsonexport'}, function(task) {
var prg = document.getElementById('js-cw-celerytask-'+eid);
prg.value = task.progress || 0;
prg.textContent = 100 * prg.value + ' %';
if ((task.state != 'done') && (task.state != 'failed')) {
setTimeout(refresh, 1000);
} else {
console.log('Emit EVENT');
var event = new Event('celerytask-statechanged', {bubbles:true});
prg.dispatchEvent(event);
console.log('Emited EVENT');
}
$.getJSON(BASE_URL, {eid: eid, vid: 'celerytask.jsonexport'}, function(task) {
console.log('autorefreshprogress', task);
if (oldState === null) {
oldState = task.state;
}
var prg = document.getElementById('js-cw-celerytask-'+eid);
if (task.progress !== null) {
prg.value = task.progress;
prg.textContent = 100 * prg.value + ' %';
} else {
prg.removeAttribute('value');
prg.textContent = 'unknown';
}
if (oldState !== task.state) {
console.log('Emit EVENT', oldState, task.state);
var event = new CustomEvent(
'celerytask-statechanged',
{detail: {oldState: oldState, newState: task.state},
bubbles:true});
oldState = task.state;
prg.dispatchEvent(event);
console.log('Emitted EVENT');
}
if ((task.state != 'SUCCESS') && (task.state != 'FAILURE')) {
setTimeout(refresh, 3000);
}
});
}
refresh();
},
autorefreshincontext: function(eid, elt) {
console.log('autorefreshincontext');
function refresh() {
$.getJSON(BASE_URL, {eid: eid, vid: 'celerytask.jsonexport'}, function(task) {
console.log('Incontext REFRESH: Task', task);
if ((task.state == 'waiting') || (task.state == 'queued')) {
setTimeout(refresh, 1000);
} else {
cw.celerytask.refreshincontext(eid, elt);
}
});
}
refresh();
refresh();
},
refreshincontext: function(eid, elt) {
console.log('refreshincontext');
$(elt).loadxhtml(
AJAX_BASE_URL,
ajaxFuncArgs('view', {eid: eid, vid: 'incontext'}),
null, 'swap');
console.log('refreshincontext');
$(elt).loadxhtml(
AJAX_BASE_URL,
ajaxFuncArgs('view', {eid: eid, vid: 'incontext'}),
null, 'swap');
},
setupincontext: function(eid, state) {
console.log("setupincontext", eid, state);
var elt = document.getElementById('js-celerytask-incontext-'+eid);
if ((state == 'waiting') || (state == 'queued')) {
cw.celerytask.autorefreshincontext(eid, elt);
}
elt.addEventListener('celerytask-statechanged',
function () {
cw.celerytask.refreshincontext(eid, elt);
},
false);
elt.addEventListener('celerytask-statechanged', function () {
cw.celerytask.refreshincontext(eid, elt);
}, false);
},
autorefreshprimary: function() {
document.querySelector('body').addEventListener(
'celerytask-statechanged', function() {window.location.reload();});
'celerytask-statechanged', function(event) {
console.log('(primary view) state changed: ', event.detail);
if (event.detail.newState == 'SUCCESS'
|| event.detail.newState == 'FAILURE') {
window.location.reload();
}
});
}
};
......@@ -122,7 +122,8 @@ class CeleryTaskProgressBarView(EntityView):
progratio = adapted.progress
progress = u'%.0f %%' % (100 * progratio)
self.w(
tags.tag('progress')(progress, max=u'1', value=unicode(progratio),
tags.tag('progress')(progress, min=u'0', max=u'1',
value=unicode(progratio),
id=u'js-cw-celerytask-%s' % entity.eid))
......@@ -131,16 +132,20 @@ class CeleryTaskJsonView(JsonMixIn, EntityView):
__regid__ = 'celerytask.jsonexport'
def entity_call(self, entity, **kwargs):
state = entity.cw_adapt_to('IWorkflowable').state
adapted = entity.cw_adapt_to('ICeleryTask')
state = adapted.state
if state == 'PENDING':
progress = None
elif adapted.finished:
progress = 1
else:
progress = adapted.progress
result = {'eid': entity.eid,
'task_id': adapted.task_id,
'task_name': adapted.task_name,
'state': state,
'progress': 1,
'progress': progress,
}
adapted = entity.cw_adapt_to('ICeleryTask')
if not adapted.finished:
result['progress'] = adapted.progress
self.wdata(result)
......@@ -149,18 +154,16 @@ class CeleryTaskInContextView(EntityView):
__select__ = EntityView.__select__ & adaptable('ICeleryTask')
def entity_call(self, entity):
state = entity.cw_adapt_to('IWorkflowable').state
if state not in ('done', 'failed'):
adapted = entity.cw_adapt_to('ICeleryTask')
state, finished = adapted.state or _('unknown state'), adapted.finished
html = [tags.a(self._cw._(state),
klass='celerytask-%s' % state.lower(),
href=entity.absolute_url())]
if not finished:
self._cw.add_js(('cubicweb.htmlhelpers.js', 'cubicweb.ajax.js',
'cubes.celerytask.js'))
self._cw.add_onload('cw.celerytask.setupincontext(%s, %s);'
% (entity.eid, json.dumps(state)))
html = [tags.a(self._cw._(state),
klass='celerytask-%s' % state,
href=entity.absolute_url())]
if not entity.cw_adapt_to('ICeleryTask').finished:
html.append(entity.view('celerytask.task_progress_bar', w=None))
self.w(tags.div('\n'.join(html),
id='js-celerytask-incontext-%s' % entity.eid))
Supports Markdown
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