Private
Public Access
1
0

cleaned up

This commit is contained in:
Sander Roosendaal
2017-11-01 17:34:46 +01:00
parent ac5500a1d8
commit 1847ab37e3
2 changed files with 15 additions and 52 deletions

View File

@@ -29,41 +29,11 @@ def getvalue(data):
return total,done,id,session_key return total,done,id,session_key
class Listener(threading.Thread):
def __init__(self, r, channels):
threading.Thread.__init__(self)
self.redis = r
self.pubsub = self.redis.pubsub()
self.pubsub.subscribe(channels)
def work(self, item):
try:
data = json.loads(item['data'])
total,done,id,session_key = getvalue(data)
perc = 100.*done/total
print perc, '%'
print session_key, id
except TypeError:
print "invalid data"
def run(self):
for item in self.pubsub.listen():
if item['data'] == "KILL":
self.pubsub.unsubscribe()
print self, "unsubscribed and finished"
break
else:
self.work(item)
def longtask(aantal,jobid=None,debug=False, def longtask(aantal,jobid=None,debug=False,
session_key=None): session_key=None):
counter = 0 counter = 0
# if jobid:
# if debug:
# job = celery_result.AsyncResult(jobid)
# else:
# job = Job.fetch(jobid,connection=redis_connection)
channel = 'tasks' channel = 'tasks'
for i in range(aantal): for i in range(aantal):
@@ -73,7 +43,6 @@ def longtask(aantal,jobid=None,debug=False,
counter = 0 counter = 0
if debug: if debug:
progress = 100.*i/aantal progress = 100.*i/aantal
print progress
if jobid != None: if jobid != None:
redis_connection.publish(channel,json.dumps( redis_connection.publish(channel,json.dumps(
{ {

View File

@@ -14,9 +14,7 @@ from django.views.generic.base import TemplateView
from django.db.models import Q from django.db.models import Q
from django import template from django import template
from django.db import IntegrityError, transaction from django.db import IntegrityError, transaction
#from django.contrib.sessions.backends.db import SessionStore
from importlib import import_module
from django.contrib.sessions.models import Session
from django.shortcuts import render from django.shortcuts import render
from django.http import ( from django.http import (
HttpResponse, HttpResponseRedirect, HttpResponse, HttpResponseRedirect,
@@ -143,10 +141,6 @@ from rq import Queue,cancel_job
from django.core.cache import cache from django.core.cache import cache
# Redis related
session_engine = import_module(settings.SESSION_ENGINE)
def getvalue(data): def getvalue(data):
perc = 0 perc = 0
total = 1 total = 1
@@ -178,7 +172,7 @@ class SessionTaskListener(threading.Thread):
data = json.loads(item['data']) data = json.loads(item['data'])
total,done,id,session_key = getvalue(data) total,done,id,session_key = getvalue(data)
perc = 100.*done/total perc = 100.*done/total
cache.set(id,perc) cache.set(id,perc,3600)
except TypeError: except TypeError:
pass pass
@@ -245,7 +239,7 @@ def remove_asynctask(request,id):
newtasks = [] newtasks = []
for task in oldtasks: for task in oldtasks:
if id not in task[0]: if id not in task[0]:
newtasks += [(task[0],task[1],task[2])] newtasks += [(task[0],task[1])]
request.session['async_tasks'] = newtasks request.session['async_tasks'] = newtasks
@@ -340,9 +334,9 @@ def test_job_view(request,aantal=100):
try: try:
request.session['async_tasks'] += [(job.id,'long_test_task',0)] request.session['async_tasks'] += [(job.id,'long_test_task')]
except KeyError: except KeyError:
request.session['async_tasks'] = [(job.id,'long_test_task',0)] request.session['async_tasks'] = [(job.id,'long_test_task')]
url = reverse(session_jobs_status) url = reverse(session_jobs_status)
@@ -394,7 +388,7 @@ def get_stored_tasks_status(request):
taskids = [] taskids = []
taskstatus = [] taskstatus = []
for id,func_name,session_progress in taskids: for id,func_name in taskids:
progress = 0 progress = 0
cached_progress = cache.get(id) cached_progress = cache.get(id)
finished = get_job_status(id)['finished'] finished = get_job_status(id)['finished']
@@ -404,7 +398,7 @@ def get_stored_tasks_status(request):
elif cached_progress>0: elif cached_progress>0:
progress = cached_progress progress = cached_progress
else: else:
progress = session_progress progress = 0
this_task_status = { this_task_status = {
'id':id, 'id':id,
@@ -3345,9 +3339,9 @@ def otwrankings_view(request,theuser=0,
) )
request.session['job_id'] = job.id request.session['job_id'] = job.id
try: try:
request.session['async_tasks'] += [(job.id,'updatecpwater',0)] request.session['async_tasks'] += [(job.id,'updatecpwater')]
except KeyError: except KeyError:
request.session['async_tasks'] = [(job.id,'updatecpwater',0)] request.session['async_tasks'] = [(job.id,'updatecpwater')]
messages.info(request,'New calculation queued. Refresh page or resubmit the date form to get the result') messages.info(request,'New calculation queued. Refresh page or resubmit the date form to get the result')
powerdf = pd.DataFrame({ powerdf = pd.DataFrame({
@@ -3602,9 +3596,9 @@ def oterankings_view(request,theuser=0,
) )
request.session['job_id'] = job.id request.session['job_id'] = job.id
try: try:
request.session['async_tasks'] += [(job.id,'updatecp',0)] request.session['async_tasks'] += [(job.id,'updatecp')]
except KeyError: except KeyError:
request.session['async_tasks'] = [(job.id,'updatecp',0)] request.session['async_tasks'] = [(job.id,'updatecp')]
messages.info(request,'New calculation queued.') messages.info(request,'New calculation queued.')
powerdf = pd.DataFrame({ powerdf = pd.DataFrame({
@@ -5736,9 +5730,9 @@ def workout_otwsetpower_view(request,id=0,message="",successmessage=""):
ratio=r.cpratio) ratio=r.cpratio)
try: try:
request.session['async_tasks'] += [(job.id,'otwsetpower',0)] request.session['async_tasks'] += [(job.id,'otwsetpower')]
except KeyError: except KeyError:
request.session['async_tasks'] = [(job.id,'otwsetpower',0)] request.session['async_tasks'] = [(job.id,'otwsetpower')]
successmessage = 'Your calculations have been submitted. You will receive an email when they are done. You can check the status of your calculations <a href="/rowers/jobs-status/">here</a>' successmessage = 'Your calculations have been submitted. You will receive an email when they are done. You can check the status of your calculations <a href="/rowers/jobs-status/">here</a>'
messages.info(request,successmessage) messages.info(request,successmessage)
@@ -7514,9 +7508,9 @@ def workout_add_chart_view(request,id,plotnr=1):
imagename=imagename imagename=imagename
) )
try: try:
request.session['async_tasks'] += [(jobid,'make_plot',0)] request.session['async_tasks'] += [(jobid,'make_plot')]
except KeyError: except KeyError:
request.session['async_tasks'] = [(jobid,'make_plot',0)] request.session['async_tasks'] = [(jobid,'make_plot')]
try: try:
url = request.session['referer'] url = request.session['referer']