1
0
mirror of https://github.com/moparisthebest/SickRage synced 2024-11-14 21:35:03 -05:00

Testing threaded async webui code

This commit is contained in:
echel0n 2014-12-08 00:17:55 -08:00
parent c6c0f9600b
commit a5c961e3eb

View File

@ -17,6 +17,7 @@
# along with SickRage. If not, see <http://www.gnu.org/licenses/>. # along with SickRage. If not, see <http://www.gnu.org/licenses/>.
from __future__ import with_statement from __future__ import with_statement
import threading
import traceback import traceback
import os import os
@ -75,7 +76,7 @@ except ImportError:
from Cheetah.Template import Template from Cheetah.Template import Template
from tornado.routes import route from tornado.routes import route
from tornado.web import RequestHandler, authenticated from tornado.web import RequestHandler, authenticated, asynchronous
from bug_tracker import BugTracker from bug_tracker import BugTracker
@ -90,6 +91,23 @@ def page_not_found(rh):
rh.set_status(404) rh.set_status(404)
rh.write('Wrong API key used') rh.write('Wrong API key used')
class Worker(threading.Thread):
def __init__(self, func, params=None, callback=None, *args, **kwargs):
super(Worker, self).__init__(*args, **kwargs)
self.callback = callback
self.func = func
self.params = params
def run(self):
# Get response
resp = self.func(**self.params)
if resp:
# Issue callback
try:
self.callback(ek.ss(resp).encode('utf-8', 'xmlcharrefreplace'))
except:
self.callback(resp)
class PageTemplate(Template): class PageTemplate(Template):
def __init__(self, rh, *args, **kwargs): def __init__(self, rh, *args, **kwargs):
kwargs['file'] = os.path.join(sickbeard.PROG_DIR, "gui/" + sickbeard.GUI_NAME + "/interfaces/default/", kwargs['file']) kwargs['file'] = os.path.join(sickbeard.PROG_DIR, "gui/" + sickbeard.GUI_NAME + "/interfaces/default/", kwargs['file'])
@ -235,6 +253,8 @@ class WebHandler(BaseHandler):
</body> </body>
</html>""" % (error, error, </html>""" % (error, error,
trace_info, request_info)) trace_info, request_info))
@asynchronous
@authenticated @authenticated
def get(self, route, *args, **kwargs): def get(self, route, *args, **kwargs):
route = route.strip('/') route = route.strip('/')
@ -249,25 +269,23 @@ class WebHandler(BaseHandler):
page_not_found(self) page_not_found(self)
return return
try:
# Sanitize argument lists: # Sanitize argument lists:
params = self.request.arguments params = self.request.arguments
for arg, value in params.items(): for arg, value in params.items():
if len(value) == 1: if len(value) == 1:
params[arg] = value[0] params[arg] = value[0]
resp = route(**params) Worker(route, params, self.worker_done).start()
if resp:
try:
resp = ek.ss(resp).encode('utf-8', 'xmlcharrefreplace')
except Exception, e:
pass
self.write(resp) def worker_done(self, value):
try:
self.write(value)
except: except:
logger.log("Failed doing web request '%s': %s" % (route, traceback.format_exc()), logger.ERROR) logger.log("Failed doing web request '%s': %s" % (route, traceback.format_exc()), logger.ERROR)
self.write({'success': False, 'error': 'Failed returning results'}) self.write({'success': False, 'error': 'Failed returning results'})
self.finish()
# link post to get # link post to get
post = get post = get