From e7910a6dff258c2803a29a1c3edd8ef81e2ad691 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jean-Michel=20Nirgal=20Vourg=C3=A8re?= Date: Wed, 10 Nov 2010 22:59:24 +0000 Subject: [PATCH] Added a proper job_detail page that display information, and reloads itself. Improved error message on jobrunner not running problems. --- bin/djais/urls.py | 3 ++- bin/djais/views.py | 35 +++++++++++++++++++++++++++++------ bin/jobrunner.py | 16 +++++++++++++--- html_templates/job.html | 29 +++++++++++++++++++++++++++++ html_templates/jobs.html | 2 +- 5 files changed, 74 insertions(+), 11 deletions(-) create mode 100644 html_templates/job.html diff --git a/bin/djais/urls.py b/bin/djais/urls.py index 3f1d1aa..2b55a94 100644 --- a/bin/djais/urls.py +++ b/bin/djais/urls.py @@ -30,7 +30,8 @@ urlpatterns = patterns('', (r'^user/(?P[a-zA-Z0-9_]+)/change_password$', 'ais.djais.views.user_change_password'), (r'^user/(?P[a-zA-Z0-9_]+)/delete$', 'ais.djais.views.user_delete'), (r'^job/$', 'ais.djais.views.jobs_index'), - (r'^job/(?P[A-Z0-9]+)$', 'ais.djais.views.job_get'), + (r'^job/(?P[A-Z0-9]+)/$', 'ais.djais.views.job_detail'), + (r'^job/(?P[A-Z0-9]+)/download$', 'ais.djais.views.job_get'), (r'^source/$', 'ais.djais.views.sources_index'), (r'^source/stats$', 'ais.djais.views.sources_stats'), (r'^news/(?P\d*)$', 'ais.djais.views.news'), diff --git a/bin/djais/views.py b/bin/djais/views.py index 33363ff..d9dce9d 100644 --- a/bin/djais/views.py +++ b/bin/djais/views.py @@ -1,6 +1,8 @@ # -*- coding: utf-8 -*- +#TODO #from __future__ import division +#Normalize 403 errors import os from datetime import * @@ -416,8 +418,10 @@ def vessel_history(request, strmmsi, format=None): job.command = command job.extension = u'kmz' job.save() - request.user.info('Request queued as job %s' % job.id) - jobrunner.wakeup_daemon() + #request.user.info('Request queued as job %s' % job.id) + if not jobrunner.wakeup_daemon(): + return HttpResponseServerError(jobrunner.DAEMON_WAKEUP_ERROR) + return HttpResponseRedirect('/job/%s/' % job.id) else: value = kml_to_kmz(format_boat_track(nmea_iterator)) response = HttpResponse(value, mimetype="application/vnd.google-earth.kml") @@ -432,8 +436,10 @@ def vessel_history(request, strmmsi, format=None): job.command = command job.extension = u'kmz' job.save() - request.user.info('Request queued as job %s' % job.id) - jobrunner.wakeup_daemon() + #request.user.info('Request queued as job %s' % job.id) + if not jobrunner.wakeup_daemon(): + return HttpResponseServerError(jobrunner.DAEMON_WAKEUP_ERROR) + return HttpResponseRedirect('/job/%s/' % job.id) else: value = kml_to_kmz(format_boat_intime(nmea_iterator)) response = HttpResponse(value, mimetype="application/vnd.google-earth.kml") @@ -448,8 +454,10 @@ def vessel_history(request, strmmsi, format=None): job.command = command job.extension = u'csv' job.save() - request.user.info('Request queued as job %s' % job.id) - jobrunner.wakeup_daemon() + #request.user.info('Request queued as job %s' % job.id) + if not jobrunner.wakeup_daemon(): + return HttpResponseServerError(jobrunner.DAEMON_WAKEUP_ERROR) + return HttpResponseRedirect('/job/%s/' % job.id) else: value = StringIO() output = csv.writer(value) @@ -671,6 +679,21 @@ def jobs_index(request): jobs = request.user.job_set.filter(archive_time__isnull=True) return render_to_response('jobs.html', {'jobs': jobs, 'archive': show_archive }, RequestContext(request)) +@http_authenticate(auth, 'ais') +def job_detail(request, jobid): + job = get_object_or_404(Job, id=jobid) + if job.user != request.user: + return HttpResponseForbidden('403 Forbidden') + response = render_to_response('job.html', {'job': job}, RequestContext(request)) + if not job.finish_time: + response['Refresh'] = 5 + elif not job.archive_time: + # finished but not archived: + response['Refresh'] = '0;url=https://ais.nirgal.com/job/%s/download' % job.id + job.archive_time = datetime.utcnow() + job.save() + return response + @http_authenticate(auth, 'ais') def job_get(request, jobid): job = get_object_or_404(Job, id=jobid) diff --git a/bin/jobrunner.py b/bin/jobrunner.py index 038a2b3..065808b 100755 --- a/bin/jobrunner.py +++ b/bin/jobrunner.py @@ -2,6 +2,7 @@ __all__ = [ \ 'wakeup_daemon', + 'DAEMON_WAKEUP_ERROR', ] import sys @@ -16,8 +17,17 @@ SOCK_FILENAME = '/var/run/ais/jobrunner.wakeup' def wakeup_daemon(): client = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM) - client.connect(SOCK_FILENAME) - client.send('') + try: + client.connect(SOCK_FILENAME) + client.send('') + return True + except: + return False + +DAEMON_WAKEUP_ERROR = """ +Your job has been queued, but there was an error contacting the task +scheduler. Please repport the error. +""" def runjob(): """ @@ -59,7 +69,7 @@ def runjob(): dbcommit() logging.info('Job complete: result=%s', returncode) - sqlexec(u"INSERT INTO user_message (user_id, user_message_category_id, txt) VALUES(%(user_id)s, 'info', %(msg)s)", {'user_id':user_id, 'msg':('Your job %(jobid)s is complete.' % {'jobid': jobid}) }) + sqlexec(u"INSERT INTO user_message (user_id, user_message_category_id, txt) VALUES(%(user_id)s, 'info', %(msg)s)", {'user_id':user_id, 'msg':('Your job %(jobid)s is complete.' % {'jobid': jobid}) }) dbcommit() return 2 diff --git a/html_templates/job.html b/html_templates/job.html new file mode 100644 index 0000000..1262f08 --- /dev/null +++ b/html_templates/job.html @@ -0,0 +1,29 @@ +{% extends "base.html" %} + +{% block tab_active_job %} id=tabactive{% endblock %} + +{% block breadcrumbs %} +{{ block.super }} +/ {{ job.id }} +{% endblock %} + +{% block content %} + +

Job {{ job.id }}

+{{ job.nice_command }}
+{% if job.finish_time %} +Status: Completed at {{ job.finish_time|date:"Y-m-d H:i:s" }} UTC in {{ job.process_time }}
+Result: {% if job.result %}Error {{ job.result }}{% else %}Success
+download{% endif %}
+{% else %} + {% if job.start_time %} + Status: Running since {{ job.start_time }}.
+ Pid: {{ job.pid }}
+ {% else %} + Status: Queued since {{ job.queue_time }}.
+ Position in jobs queue: {{ job.queue_rank }}
+ {% endif %} +{% endif %} + + +{% endblock %} diff --git a/html_templates/jobs.html b/html_templates/jobs.html index 8583541..5816836 100644 --- a/html_templates/jobs.html +++ b/html_templates/jobs.html @@ -19,7 +19,7 @@ {% if job.finish_time %} Status: Completed at {{ job.finish_time|date:"Y-m-d H:i:s" }} UTC in {{ job.process_time }}
Result: {% if job.result %}Error {{ job.result }}{% else %}Success
-download{% endif %}
+download{% endif %}
{% else %} {% if job.start_time %} Status:Running since {{ job.start_time }}.
-- 2.30.2