version function, ui version tracking, blocks behind blockchain during startup, cleaner set_vars

-adds version() function, which can be called after the daemon is
running and returns lbrynet, lbryum, and lbry ui version information.

-daemon_status now returns how far behind its behind the blockchain
when in the loading_wallet phase of startup

-cleans up set_vars by moving to init
This commit is contained in:
Jack 2016-04-11 22:28:46 -04:00
parent aa8c362cb8
commit 31fa974379
3 changed files with 130 additions and 106 deletions

View file

@ -902,6 +902,7 @@ class LBRYumWallet(LBRYWallet):
self._start_check = None
self._catch_up_check = None
self._caught_up_counter = 0
self.blocks_behind_alert = 0
def _start(self):
@ -996,6 +997,7 @@ class LBRYumWallet(LBRYWallet):
if self._caught_up_counter == 0:
alert.info('Catching up to the blockchain...showing blocks left...')
if self._caught_up_counter % 30 == 0:
self.blocks_behind_alert = remote_height - local_height
alert.info('%d...', (remote_height - local_height))
self._caught_up_counter += 1

View file

@ -23,7 +23,8 @@ from decimal import Decimal
from appdirs import user_data_dir
from urllib2 import urlopen
from lbrynet import __version__
from lbrynet import __version__ as lbrynet_version
from lbryum.version import ELECTRUM_VERSION as lbryum_version
from lbrynet.core.PaymentRateManager import PaymentRateManager
from lbrynet.core.server.BlobAvailabilityHandler import BlobAvailabilityHandlerFactory
from lbrynet.core.server.BlobRequestHandler import BlobRequestHandlerFactory
@ -65,7 +66,6 @@ STARTUP_STAGES = [
('initializing', 'Initializing...'),
('loading_db', 'Loading databases...'),
('loading_wallet', 'Catching up with blockchain... %s'),
('loading_session', 'Starting session'),
('loading_file_manager', 'Setting up file manager'),
('loading_server', 'Starting lbrynet'),
('started', 'Started lbrynet')
@ -88,75 +88,11 @@ class LBRYDaemon(jsonrpc.JSONRPC):
"""
LBRYnet daemon, a jsonrpc interface to lbry functions
"""
isLeaf = True
def render(self, request):
request.content.seek(0, 0)
# Unmarshal the JSON-RPC data.
content = request.content.read()
parsed = jsonrpclib.loads(content)
functionPath = parsed.get("method")
args = parsed.get('params')
id = parsed.get('id')
version = parsed.get('jsonrpc')
if version:
version = int(float(version))
elif id and not version:
version = jsonrpclib.VERSION_1
else:
version = jsonrpclib.VERSION_PRE1
# XXX this all needs to be re-worked to support logic for multiple
# versions...
if not self.announced_startup:
if functionPath not in ['is_running', 'is_first_run',
'get_time_behind_blockchain', 'stop',
'daemon_status', 'get_start_notice']:
return server.failure
try:
function = self._getFunction(functionPath)
except jsonrpclib.Fault, f:
self._cbRender(f, request, id, version)
else:
request.setHeader("Access-Control-Allow-Origin", "*")
request.setHeader("content-type", "text/json")
if args == [{}]:
d = defer.maybeDeferred(function)
else:
d = defer.maybeDeferred(function, *args)
d.addErrback(self._ebRender, id)
d.addCallback(self._cbRender, request, id, version)
return server.NOT_DONE_YET
def _cbRender(self, result, request, id, version):
if isinstance(result, Handler):
result = result.result
if isinstance(result, dict):
result = result['result']
if version == jsonrpclib.VERSION_PRE1:
if not isinstance(result, jsonrpclib.Fault):
result = (result,)
# Convert the result (python) to JSON-RPC
try:
s = jsonrpclib.dumps(result, version=version)
except:
f = jsonrpclib.Fault(self.FAILURE, "can't serialize output")
s = jsonrpclib.dumps(f, version=version)
request.setHeader("content-length", str(len(s)))
request.write(s)
request.finish()
def _ebRender(self, failure, id):
if isinstance(failure.value, jsonrpclib.Fault):
return failure.value
log.error(failure)
return jsonrpclib.Fault(self.FAILURE, "error")
def setup(self, wallet_type, check_for_updates):
def _set_vars(wallet_type, check_for_updates):
def __init__(self, wallet_type, check_for_updates, ui_version_info):
jsonrpc.JSONRPC.__init__(self)
reactor.addSystemEventTrigger('before', 'shutdown', self._shutdown)
self.log_file = LOG_FILENAME
@ -169,7 +105,6 @@ class LBRYDaemon(jsonrpc.JSONRPC):
self.db_dir = os.path.join(os.path.expanduser("~"), ".lbrynet")
else:
self.db_dir = user_data_dir("LBRY")
# self.db_dir = os.path.join(os.path.expanduser("~"), "Library/Application Support/lbrynet")
self.blobfile_dir = os.path.join(self.db_dir, "blobfiles")
self.peer_port = 3333
self.dht_node_port = 4444
@ -237,9 +172,77 @@ class LBRYDaemon(jsonrpc.JSONRPC):
'max_download': 0.0,
'upload_log': True
}
self.ui_version = ui_version_info.replace('\n', '')
return defer.succeed(None)
def render(self, request):
request.content.seek(0, 0)
# Unmarshal the JSON-RPC data.
content = request.content.read()
parsed = jsonrpclib.loads(content)
functionPath = parsed.get("method")
args = parsed.get('params')
#TODO convert args to correct types if possible
id = parsed.get('id')
version = parsed.get('jsonrpc')
if version:
version = int(float(version))
elif id and not version:
version = jsonrpclib.VERSION_1
else:
version = jsonrpclib.VERSION_PRE1
# XXX this all needs to be re-worked to support logic for multiple
# versions...
if not self.announced_startup:
if functionPath not in ['is_running', 'is_first_run',
'get_time_behind_blockchain', 'stop',
'daemon_status', 'get_start_notice']:
return server.failure
try:
function = self._getFunction(functionPath)
except jsonrpclib.Fault, f:
self._cbRender(f, request, id, version)
else:
request.setHeader("Access-Control-Allow-Origin", "*")
request.setHeader("content-type", "text/json")
if args == [{}]:
d = defer.maybeDeferred(function)
else:
d = defer.maybeDeferred(function, *args)
d.addErrback(self._ebRender, id)
d.addCallback(self._cbRender, request, id, version)
return server.NOT_DONE_YET
def _cbRender(self, result, request, id, version):
if isinstance(result, Handler):
result = result.result
if isinstance(result, dict):
result = result['result']
if version == jsonrpclib.VERSION_PRE1:
if not isinstance(result, jsonrpclib.Fault):
result = (result,)
# Convert the result (python) to JSON-RPC
try:
s = jsonrpclib.dumps(result, version=version)
except:
f = jsonrpclib.Fault(self.FAILURE, "can't serialize output")
s = jsonrpclib.dumps(f, version=version)
request.setHeader("content-length", str(len(s)))
request.write(s)
request.finish()
def _ebRender(self, failure, id):
if isinstance(failure.value, jsonrpclib.Fault):
return failure.value
log.error(failure)
return jsonrpclib.Fault(self.FAILURE, "error")
def setup(self):
def _log_starting_vals():
def _get_lbry_files_json():
r = []
@ -266,14 +269,13 @@ class LBRYDaemon(jsonrpc.JSONRPC):
def _announce_startup():
self.announced_startup = True
self.startup_status = STARTUP_STAGES[6]
self.startup_status = STARTUP_STAGES[5]
log.info("[" + str(datetime.now()) + "] Started lbrynet-daemon")
return defer.succeed(None)
log.info("[" + str(datetime.now()) + "] Starting lbrynet-daemon")
d = defer.Deferred()
d.addCallback(lambda _:_set_vars(wallet_type, check_for_updates))
d.addCallback(lambda _: self._initial_setup())
d.addCallback(self._set_daemon_settings)
d.addCallback(lambda _: threads.deferToThread(self._setup_data_directory))
@ -299,7 +301,9 @@ class LBRYDaemon(jsonrpc.JSONRPC):
msg = {
"processor": platform.processor(),
"python version: ": platform.python_version(),
"lbrynet version: ": __version__,
"lbrynet version: ": lbrynet_version,
"lbryum version: ": lbryum_version,
"ui_version": self.ui_version,
# 'ip': json.load(urlopen('http://jsonip.com'))['ip'],
}
if sys.platform == "darwin":
@ -358,7 +362,7 @@ class LBRYDaemon(jsonrpc.JSONRPC):
return self._start_server()
return defer.succeed(True)
self.startup_status = STARTUP_STAGES[5]
self.startup_status = STARTUP_STAGES[4]
dl = self.settings.get_server_running_status()
dl.addCallback(restore_running_status)
@ -542,7 +546,7 @@ class LBRYDaemon(jsonrpc.JSONRPC):
return d
def _setup_lbry_file_manager(self):
self.startup_status = STARTUP_STAGES[4]
self.startup_status = STARTUP_STAGES[3]
self.lbry_file_metadata_manager = DBLBRYFileMetadataManager(self.db_dir)
d = self.lbry_file_metadata_manager.setup()
@ -597,12 +601,11 @@ class LBRYDaemon(jsonrpc.JSONRPC):
return r
def create_session(results):
self.startup_status = STARTUP_STAGES[2]
self.session = LBRYSession(results['default_data_payment_rate'], db_dir=self.db_dir, lbryid=self.lbryid,
blob_dir=self.blobfile_dir, dht_node_port=self.dht_node_port,
known_dht_nodes=self.known_dht_nodes, peer_port=self.peer_port,
use_upnp=self.use_upnp, wallet=results['wallet'])
self.startup_status = STARTUP_STAGES[3]
self.startup_status = STARTUP_STAGES[2]
dl = defer.DeferredList([d1, d2], fireOnOneErrback=True)
dl.addCallback(combine_results)
@ -868,6 +871,11 @@ class LBRYDaemon(jsonrpc.JSONRPC):
"""
r = {'status_code': self.startup_status[0], 'status_message': self.startup_status[1]}
try:
if self.startup_status[0] == 'loading_wallet':
r['status_message'] = r['status_message'] % (str(self.session.wallet.blocks_behind_alert) + " blocks behind")
except:
pass
log.info("[" + str(datetime.now()) + "] daemon status: " + str(r))
return self._render_response(r, OK_CODE)
@ -901,6 +909,18 @@ class LBRYDaemon(jsonrpc.JSONRPC):
else:
self._render_response(self.startup_message, OK_CODE)
def jsonrpc_version(self):
"""
Get lbry version information
"""
msg = {
"lbrynet version: ": lbrynet_version,
"lbryum version: ": lbryum_version,
"ui_version": self.ui_version,
}
return self._render_response(msg, OK_CODE)
def jsonrpc_get_settings(self):
"""
Get LBRY payment settings

View file

@ -88,21 +88,23 @@ def start():
if ui_dir:
if os.path.isdir(ui_dir):
log.info("Using user specified UI directory: " + str(ui_dir))
return defer.succeed(ui_dir)
ui_version_info = "user-specified"
return defer.succeed([ui_dir, ui_version_info])
else:
log.info("User specified UI directory doesn't exist: " + str(ui_dir))
def download_ui(dest_dir):
def download_ui(dest_dir, ui_version):
url = urlopen(DIST_URL)
z = ZipFile(StringIO(url.read()))
names = [i for i in z.namelist() if '.DS_Store' not in i and '__MACOSX' not in i]
z.extractall(dest_dir, members=names)
return defer.succeed(dest_dir)
return defer.succeed([dest_dir, ui_version])
data_dir = user_data_dir("LBRY")
version_dir = os.path.join(data_dir, "ui_version_history")
git_version = subprocess.check_output(GIT_CMD_STRING, shell=True)
ui_version_info = git_version
if not os.path.isdir(data_dir):
os.mkdir(data_dir)
@ -128,28 +130,28 @@ def start():
log.info(version_message)
if os.path.isdir(os.path.join(data_dir, "lbry-web-ui")):
return defer.succeed(os.path.join(data_dir, "lbry-web-ui"))
return defer.succeed([os.path.join(data_dir, "lbry-web-ui"), ui_version_info])
else:
return download_ui((os.path.join(data_dir, "lbry-web-ui")))
return download_ui(os.path.join(data_dir, "lbry-web-ui"), ui_version_info)
def setupserver(ui_dir):
def setupserver(ui_dir, ui_version):
root = LBRYindex(ui_dir)
root.putChild("css", static.File(os.path.join(ui_dir, "css")))
root.putChild("font", static.File(os.path.join(ui_dir, "font")))
root.putChild("img", static.File(os.path.join(ui_dir, "img")))
root.putChild("js", static.File(os.path.join(ui_dir, "js")))
root.putChild("view", LBRYFileRender())
return defer.succeed(root)
return defer.succeed([root, ui_version])
def setupapi(root, wallet):
daemon = LBRYDaemon()
def setupapi(root, wallet, ui_version):
daemon = LBRYDaemon(wallet, "False", ui_version)
root.putChild(API_ADDRESS, daemon)
reactor.listenTCP(API_PORT, server.Site(root), interface=API_INTERFACE)
return daemon.setup(wallet, "False")
return daemon.setup()
d = getui(args.ui)
d.addCallback(setupserver)
d.addCallback(lambda r: setupapi(r, args.wallet))
d.addCallback(lambda r: setupserver(r[0], r[1]))
d.addCallback(lambda r: setupapi(r[0], args.wallet, r[1]))
d.addCallback(lambda _: webbrowser.open(UI_ADDRESS))
reactor.run()