cache layer

This commit is contained in:
dave 2019-08-17 10:50:31 -07:00
parent 55db207c11
commit 741147015f
2 changed files with 63 additions and 7 deletions

View File

@ -4,11 +4,55 @@ import logging
from jinja2 import Environment, FileSystemLoader, select_autoescape from jinja2 import Environment, FileSystemLoader, select_autoescape
from deluge_client import DelugeRPCClient from deluge_client import DelugeRPCClient
from urllib.parse import urlparse from urllib.parse import urlparse
from pprint import pprint
from threading import Thread
from time import sleep
from queue import Queue
from dataclasses import dataclass, field
APPROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "../")) APPROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "../"))
@dataclass
class Cache:
torrents: dict = field(default_factory=dict)
class ClientCache(object):
def __init__(self, client):
self.client = client
self.data = Cache()
self.q = Queue()
self.background_t = Thread(target=self.background, daemon=True)
self.background_t.start()
self.timer_t = Thread(target=self.timer, daemon=True)
self.timer_t.start()
def refresh(self):
self.q.put(None)
def background(self):
while True:
self.q.get() # block until we need to do something
logging.info("performing background tasks...")
self.data.torrents = self.client.core.get_torrents_status({"label": "sickrage"},
['name', 'label', 'save_path', 'is_seed',
'is_finished', 'progress'])
self.q.task_done()
logging.info("background tasks complete")
def timer(self):
while True:
self.refresh()
logging.info("sleeping...")
sleep(300) # TODO configurable task interval
class MediaWeb(object): class MediaWeb(object):
def __init__(self, rpc, templater, uioptions): def __init__(self, rpc, templater, uioptions):
self.tpl = templater self.tpl = templater
@ -25,17 +69,21 @@ class MediaWeb(object):
return {} return {}
@cherrypy.expose @cherrypy.expose
def index(self): def index(self, action=None):
torrents = self.rpc.core.get_torrents_status({"label": "sickrage"}, ['name', 'label', 'save_path', 'is_seed', 'is_finished', 'progress']) if action:
return self.render("index.html", torrents=torrents) if action == "update":
self.rpc.refresh()
raise cherrypy.HTTPRedirect("/")
return self.render("index.html", torrents=self.rpc.data.torrents)
@cherrypy.expose @cherrypy.expose
def move(self, thash, dest=None, otherdest=None): def move(self, thash, dest=None, otherdest=None):
torrent = self.rpc.core.get_torrent_status(thash, []) # TODO reduce to needed fields torrent = self.rpc.client.core.get_torrent_status(thash, []) # TODO reduce to needed fields
if cherrypy.request.method == "POST" and (dest or otherdest): if cherrypy.request.method == "POST" and (dest or otherdest):
target = otherdest or dest target = otherdest or dest
self.rpc.core.move_storage([thash], target) self.rpc.client.core.move_storage([thash], target)
self.rpc.refresh()
raise cherrypy.HTTPRedirect("/") raise cherrypy.HTTPRedirect("/")
return self.render("moveform.html", torrent=torrent) return self.render("moveform.html", torrent=torrent)
@ -83,7 +131,9 @@ def main():
assert uri.scheme == "deluge" assert uri.scheme == "deluge"
rpc = DelugeRPCClient(uri.hostname, uri.port if uri.port else 58846, uri.username, uri.password, decode_utf8=True) rpc = DelugeRPCClient(uri.hostname, uri.port if uri.port else 58846, uri.username, uri.password, decode_utf8=True)
web = MediaWeb(rpc, tpl, uioptions) rpc_cache = ClientCache(rpc)
web = MediaWeb(rpc_cache, tpl, uioptions)
cherrypy.tree.mount(web, '/', {'/': {'tools.auth_basic.on': True, cherrypy.tree.mount(web, '/', {'/': {'tools.auth_basic.on': True,
'tools.auth_basic.realm': 'mediaweb', 'tools.auth_basic.realm': 'mediaweb',
'tools.auth_basic.checkpassword': validate_password, }}) 'tools.auth_basic.checkpassword': validate_password, }})

View File

@ -2,6 +2,12 @@
{% block body %} {% block body %}
<div class="torrents"> <div class="torrents">
<div class="toolbar">
<form action="/" method="post">
<input name="action" type="submit" value="refresh">
<input name="action" type="submit" value="update">
</form>
</div>
<h2>Completed</h2> <h2>Completed</h2>
<table> <table>
<tr> <tr>
@ -32,7 +38,7 @@
<th>hash</th> <th>hash</th>
<th>name</th> <th>name</th>
<th>path</th> <th>path</th>
<th>percent</th> <th>progress</th>
<th>actions</th> <th>actions</th>
</tr> </tr>
{% for torid, tor in torrents.items() %}{% if not tor.is_finished %} {% for torid, tor in torrents.items() %}{% if not tor.is_finished %}