Try to download queued job result ASAP.
[ais.git] / bin / djais / views.py
index 25f9b17e136b6ca8132c44fa0eaabaaad63fd1ef..0b48b767d71951f8ff78732df172ec194ec34c1b 100644 (file)
@@ -8,7 +8,7 @@ import os
 import os.path
 from datetime import *
 import re
-from time import time as get_timestamp
+from time import time as get_timestamp, sleep
 import logging
 import crack
 import struct
@@ -422,7 +422,7 @@ def vessel_history(request, strmmsi, format=None):
             #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)
+            return HttpResponseRedirect('/job/%s/download' % job.id)
         else:
             value = kml_to_kmz(format_boat_track(nmea_iterator))
             response = HttpResponse(value, mimetype="application/vnd.google-earth.kml")
@@ -440,7 +440,7 @@ def vessel_history(request, strmmsi, format=None):
             #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)
+            return HttpResponseRedirect('/job/%s/download' % job.id)
         else:
             value = kml_to_kmz(format_boat_intime(nmea_iterator))
             response = HttpResponse(value, mimetype="application/vnd.google-earth.kml")
@@ -458,7 +458,7 @@ def vessel_history(request, strmmsi, format=None):
             #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)
+            return HttpResponseRedirect('/job/%s/download' % job.id)
         else:
             value = StringIO()
             output = csv.writer(value)
@@ -700,9 +700,19 @@ def job_get(request, jobid):
     job = get_object_or_404(Job, id=jobid)
     if job.user != request.user:
         return HttpResponseForbidden('403 Forbidden')
+    total_wait = 0
+    while not job.finish_time:
+        sleep(1)
+        total_wait += 1
+        if total_wait > 30:
+            request.user.info('Your query is slow. Please be patient.')
+            return HttpResponseRedirect('/job/%s/' % job.id)
+        job = get_object_or_404(Job, id=jobid)
+    
     if not job.archive_time:
         job.archive_time = datetime.utcnow()
         job.save()
+
     extension = os.path.splitext(job.friendly_filename)[-1]
     return HttpResponseRedirect('/job_result/%s%s/%s' % (job.id, extension, job.friendly_filename))