Ensure settings are actually committed

Without committing or closing the db, settings were not being saved.

Also, adds a run_in_thread decorator to simplify the code.
This commit is contained in:
Job Evers-Meltzer 2016-08-12 10:08:21 -05:00
parent ff31ddd2d5
commit 3ed7ce5630

View file

@ -1,15 +1,25 @@
import binascii import binascii
import functools
import json import json
import unqlite
import logging import logging
import os import os
from twisted.internet import threads, defer from twisted.internet import threads, defer
import unqlite
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
def run_in_thread(fn):
@functools.wraps(fn)
def wrapped(*args, **kwargs):
return threads.deferToThread(fn, *args, **kwargs)
return wrapped
class LBRYSettings(object): class LBRYSettings(object):
NAME = "settings.db"
def __init__(self, db_dir): def __init__(self, db_dir):
self.db_dir = db_dir self.db_dir = db_dir
self.db = None self.db = None
@ -18,47 +28,39 @@ class LBRYSettings(object):
return self._open_db() return self._open_db()
def stop(self): def stop(self):
self.db.close()
self.db = None self.db = None
return defer.succeed(True) return defer.succeed(True)
def _open_db(self): def _open_db(self):
log.debug("Opening %s as the settings database", str(os.path.join(self.db_dir, "settings.db"))) filename = os.path.join(self.db_dir, self.NAME)
self.db = unqlite.UnQLite(os.path.join(self.db_dir, "settings.db")) log.debug("Opening %s as the settings database", filename)
self.db = unqlite.UnQLite(filename)
return defer.succeed(True) return defer.succeed(True)
@run_in_thread
def save_lbryid(self, lbryid): def save_lbryid(self, lbryid):
self.db['lbryid'] = binascii.hexlify(lbryid)
self.db.commit()
def save_lbryid(): @run_in_thread
self.db['lbryid'] = binascii.hexlify(lbryid)
return threads.deferToThread(save_lbryid)
def get_lbryid(self): def get_lbryid(self):
if 'lbryid' in self.db:
return binascii.unhexlify(self.db['lbryid'])
else:
return None
def get_lbryid(): @run_in_thread
if 'lbryid' in self.db:
return binascii.unhexlify(self.db['lbryid'])
else:
return None
return threads.deferToThread(get_lbryid)
def get_server_running_status(self): def get_server_running_status(self):
if 'server_running' in self.db:
return json.loads(self.db['server_running'])
else:
return True
def get_status(): @run_in_thread
if 'server_running' in self.db:
return json.loads(self.db['server_running'])
else:
return True
return threads.deferToThread(get_status)
def save_server_running_status(self, running): def save_server_running_status(self, running):
self.db['server_running'] = json.dumps(running)
def save_status(): self.db.commit()
self.db['server_running'] = json.dumps(running)
return threads.deferToThread(save_status)
def get_default_data_payment_rate(self): def get_default_data_payment_rate(self):
return self._get_payment_rate("default_data_payment_rate") return self._get_payment_rate("default_data_payment_rate")
@ -78,35 +80,27 @@ class LBRYSettings(object):
def save_server_crypt_info_payment_rate(self, rate): def save_server_crypt_info_payment_rate(self, rate):
return self._save_payment_rate("server_crypt_info_payment_rate", rate) return self._save_payment_rate("server_crypt_info_payment_rate", rate)
@run_in_thread
def _get_payment_rate(self, rate_type): def _get_payment_rate(self, rate_type):
if rate_type in self.db:
return json.loads(self.db[rate_type])
else:
return None
def get_rate(): @run_in_thread
if rate_type in self.db:
return json.loads(self.db[rate_type])
else:
return None
return threads.deferToThread(get_rate)
def _save_payment_rate(self, rate_type, rate): def _save_payment_rate(self, rate_type, rate):
if rate is not None:
self.db[rate_type] = json.dumps(rate)
elif rate_type in self.db:
del self.db[rate_type]
self.db.commit()
def save_rate(): @run_in_thread
if rate is not None:
self.db[rate_type] = json.dumps(rate)
elif rate_type in self.db:
del self.db[rate_type]
return threads.deferToThread(save_rate)
def get_query_handler_status(self, query_identifier): def get_query_handler_status(self, query_identifier):
if json.dumps(('q_h', query_identifier)) in self.db:
def get_status(): return json.loads(self.db[(json.dumps(('q_h', query_identifier)))])
if json.dumps(('q_h', query_identifier)) in self.db: else:
return json.loads(self.db[(json.dumps(('q_h', query_identifier)))]) return True
else:
return True
return threads.deferToThread(get_status)
def enable_query_handler(self, query_identifier): def enable_query_handler(self, query_identifier):
return self._set_query_handler_status(query_identifier, True) return self._set_query_handler_status(query_identifier, True)
@ -114,7 +108,7 @@ class LBRYSettings(object):
def disable_query_handler(self, query_identifier): def disable_query_handler(self, query_identifier):
return self._set_query_handler_status(query_identifier, False) return self._set_query_handler_status(query_identifier, False)
@run_in_thread
def _set_query_handler_status(self, query_identifier, status): def _set_query_handler_status(self, query_identifier, status):
def set_status(): self.db[json.dumps(('q_h', query_identifier))] = json.dumps(status)
self.db[json.dumps(('q_h', query_identifier))] = json.dumps(status) self.db.commit()
return threads.deferToThread(set_status)