From 5d4ed8ffbc1f99be013f433e8a33b9eb66a3052e Mon Sep 17 00:00:00 2001 From: j Date: Fri, 8 Jan 2016 10:02:24 +0530 Subject: [PATCH] implement quit api. indicate if backend is offline --- ctl | 2 +- oml/api.py | 18 ++++++++++++- oml/meta/google.py | 3 +-- oml/meta/utils.py | 3 +++ oml/server.py | 61 ++++++++++++++++++++++++------------------- oml/utils.py | 5 +--- static/js/mainMenu.js | 4 +++ static/js/oml.js | 22 ++++++++++++++++ 8 files changed, 83 insertions(+), 35 deletions(-) diff --git a/ctl b/ctl index 9a72505..2bd19a9 100755 --- a/ctl +++ b/ctl @@ -90,7 +90,7 @@ if [ "$1" == "debug" ]; then exit 1 fi shift - exec python3 oml server $@ + exec python3 oml server debug $PID fi if [ "$1" == "stop" ]; then remove_loginscript diff --git a/oml/api.py b/oml/api.py index 8fedf29..0cf4b55 100644 --- a/oml/api.py +++ b/oml/api.py @@ -9,7 +9,6 @@ import os import ox from oxtornado import actions - import item.api import user.api import update @@ -77,3 +76,20 @@ def autocompleteFolder(data): 'items': ox.sorted_strings(folders) } actions.register(autocompleteFolder, cache=False) + +def quit(data): + ''' + takes { + } + returns { + } + ''' + ''' + import server + server.shutdown() + ''' + import settings + subprocess.Popen([os.path.join(settings.base_dir, 'ctl'), 'stop'], + close_fds=True, start_new_session=True) + return {} +actions.register(quit, cache=False) diff --git a/oml/meta/google.py b/oml/meta/google.py index edc98c7..677087e 100644 --- a/oml/meta/google.py +++ b/oml/meta/google.py @@ -6,8 +6,7 @@ from ox.cache import get_json, store import ox.web.google import stdnum.isbn -from oml.utils import get_language -from .utils import find_isbns +from .utils import find_isbns, get_language import logging logger = logging.getLogger(__name__) diff --git a/oml/meta/utils.py b/oml/meta/utils.py index 3133884..652e98f 100644 --- a/oml/meta/utils.py +++ b/oml/meta/utils.py @@ -5,6 +5,7 @@ import re import stdnum.isbn +import ox def normalize_isbn(value): return ''.join([s for s in value if s.isdigit() or s == 'X']) @@ -21,3 +22,5 @@ def find_isbns(text): '0' * 13, )] +def get_language(lang): + return ox.iso.codeToLang(lang.split('-')[0]) or lang diff --git a/oml/server.py b/oml/server.py index 5062862..8b6c3c1 100644 --- a/oml/server.py +++ b/oml/server.py @@ -54,12 +54,43 @@ def log_request(handler): log_method("%d %s %.2fms", handler.get_status(), handler._request_summary(), request_time) +def shutdown(): + if state.tor: + state.tor._shutdown = True + if state.downloads: + logger.debug('shutdown downloads') + state.downloads.join() + if state.scraping: + logger.debug('shutdown scraping') + state.scraping.join() + logger.debug('shutdown http_server') + state.http_server.stop() + if state.tasks: + logger.debug('shutdown tasks') + state.tasks.join() + if state.nodes: + logger.debug('shutdown nodes') + state.nodes.join() + if state.node: + state.node.stop() + if state.tor: + logger.debug('shutdown tor') + state.tor.shutdown() + if state.PID and os.path.exists(state.PID): + logger.debug('remove %s', state.PID) + os.unlink(state.PID) + def run(): setup.create_db() PID = sys.argv[2] if len(sys.argv) > 2 else None + if len(sys.argv) > 3 and sys.argv[2] == 'debug': + PID = sys.argv[3] + debug = True + else: + debug = False log_format='%(asctime)s:%(levelname)s:%(name)s:%(message)s' - if not PID: + if debug: logging.basicConfig(level=logging.DEBUG, format=log_format) else: logging.basicConfig(level=logging.DEBUG, @@ -102,6 +133,8 @@ def run(): with open(PID, 'w') as pid: pid.write('%s' % os.getpid()) + state.PID = PID + state.http_server = http_server state.main = IOLoop.instance() state.cache = Cache(ttl=60) state.tasks = tasks.Tasks() @@ -134,32 +167,6 @@ def run(): print('open browser at %s' % url) logger.debug('Starting OML %s at %s', settings.VERSION, url) - def shutdown(): - if state.tor: - state.tor._shutdown = True - if state.downloads: - logger.debug('shutdown downloads') - state.downloads.join() - if state.scraping: - logger.debug('shutdown scraping') - state.scraping.join() - logger.debug('shutdown http_server') - http_server.stop() - if state.tasks: - logger.debug('shutdown tasks') - state.tasks.join() - if state.nodes: - logger.debug('shutdown nodes') - state.nodes.join() - if state.node: - state.node.stop() - if state.tor: - logger.debug('shutdown tor') - state.tor.shutdown() - if PID and os.path.exists(PID): - logger.debug('remove %s', PID) - os.unlink(PID) - signal.signal(signal.SIGTERM, shutdown) try: diff --git a/oml/utils.py b/oml/utils.py index 7e4c39f..4dd1261 100644 --- a/oml/utils.py +++ b/oml/utils.py @@ -29,7 +29,7 @@ from Crypto.PublicKey import RSA from Crypto.Util.asn1 import DerSequence -from meta.utils import normalize_isbn, find_isbns +from meta.utils import normalize_isbn, find_isbns, get_language import logging logger = logging.getLogger(__name__) @@ -121,9 +121,6 @@ def get_position_by_id(list, key): return i return -1 -def get_language(lang): - return ox.iso.codeToLang(lang.split('-')[0]) or lang - def valid(key, value, sig): ''' validate that value was signed by key diff --git a/static/js/mainMenu.js b/static/js/mainMenu.js index d9a9f8a..b689477 100644 --- a/static/js/mainMenu.js +++ b/static/js/mainMenu.js @@ -476,6 +476,10 @@ oml.ui.mainMenu = function() { } else { Ox.print('no way to download multiple right now'); } + } else if (id == 'quit') { + oml.api.quit(function() { + // + }); } else { Ox.print('MAIN MENU DOES NOT YET HANDLE', id); } diff --git a/static/js/oml.js b/static/js/oml.js index 8c62523..efc91d3 100644 --- a/static/js/oml.js +++ b/static/js/oml.js @@ -128,6 +128,28 @@ } catch(e) {} } removeScreen(); + oml.bindEvent({ + close: function() { + if(!$('#offline').length) { + $('
') + .attr({id: 'offline'}) + .css({ + left: 0, + right: 0, + top: 0, + bottom: 0, + position: 'absolute', + opacity: 0.5, + backgroundColor: 'black', + zIndex: 1000 + }) + .appendTo(Ox.$body); + } + }, + open: function() { + $('#offline').remove(); + } + }); }); }