implement quit api. indicate if backend is offline

This commit is contained in:
j 2016-01-08 10:02:24 +05:30
parent 67ad0a82e9
commit 5d4ed8ffbc
8 changed files with 83 additions and 35 deletions

2
ctl
View file

@ -90,7 +90,7 @@ if [ "$1" == "debug" ]; then
exit 1 exit 1
fi fi
shift shift
exec python3 oml server $@ exec python3 oml server debug $PID
fi fi
if [ "$1" == "stop" ]; then if [ "$1" == "stop" ]; then
remove_loginscript remove_loginscript

View file

@ -9,7 +9,6 @@ import os
import ox import ox
from oxtornado import actions from oxtornado import actions
import item.api import item.api
import user.api import user.api
import update import update
@ -77,3 +76,20 @@ def autocompleteFolder(data):
'items': ox.sorted_strings(folders) 'items': ox.sorted_strings(folders)
} }
actions.register(autocompleteFolder, cache=False) 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)

View file

@ -6,8 +6,7 @@ from ox.cache import get_json, store
import ox.web.google import ox.web.google
import stdnum.isbn import stdnum.isbn
from oml.utils import get_language from .utils import find_isbns, get_language
from .utils import find_isbns
import logging import logging
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)

View file

@ -5,6 +5,7 @@
import re import re
import stdnum.isbn import stdnum.isbn
import ox
def normalize_isbn(value): def normalize_isbn(value):
return ''.join([s for s in value if s.isdigit() or s == 'X']) return ''.join([s for s in value if s.isdigit() or s == 'X'])
@ -21,3 +22,5 @@ def find_isbns(text):
'0' * 13, '0' * 13,
)] )]
def get_language(lang):
return ox.iso.codeToLang(lang.split('-')[0]) or lang

View file

@ -54,12 +54,43 @@ def log_request(handler):
log_method("%d %s %.2fms", handler.get_status(), log_method("%d %s %.2fms", handler.get_status(),
handler._request_summary(), request_time) 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(): def run():
setup.create_db() setup.create_db()
PID = sys.argv[2] if len(sys.argv) > 2 else None 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' log_format='%(asctime)s:%(levelname)s:%(name)s:%(message)s'
if not PID: if debug:
logging.basicConfig(level=logging.DEBUG, format=log_format) logging.basicConfig(level=logging.DEBUG, format=log_format)
else: else:
logging.basicConfig(level=logging.DEBUG, logging.basicConfig(level=logging.DEBUG,
@ -102,6 +133,8 @@ def run():
with open(PID, 'w') as pid: with open(PID, 'w') as pid:
pid.write('%s' % os.getpid()) pid.write('%s' % os.getpid())
state.PID = PID
state.http_server = http_server
state.main = IOLoop.instance() state.main = IOLoop.instance()
state.cache = Cache(ttl=60) state.cache = Cache(ttl=60)
state.tasks = tasks.Tasks() state.tasks = tasks.Tasks()
@ -134,32 +167,6 @@ def run():
print('open browser at %s' % url) print('open browser at %s' % url)
logger.debug('Starting OML %s at %s', settings.VERSION, 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) signal.signal(signal.SIGTERM, shutdown)
try: try:

View file

@ -29,7 +29,7 @@ from Crypto.PublicKey import RSA
from Crypto.Util.asn1 import DerSequence 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 import logging
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -121,9 +121,6 @@ def get_position_by_id(list, key):
return i return i
return -1 return -1
def get_language(lang):
return ox.iso.codeToLang(lang.split('-')[0]) or lang
def valid(key, value, sig): def valid(key, value, sig):
''' '''
validate that value was signed by key validate that value was signed by key

View file

@ -476,6 +476,10 @@ oml.ui.mainMenu = function() {
} else { } else {
Ox.print('no way to download multiple right now'); Ox.print('no way to download multiple right now');
} }
} else if (id == 'quit') {
oml.api.quit(function() {
//
});
} else { } else {
Ox.print('MAIN MENU DOES NOT YET HANDLE', id); Ox.print('MAIN MENU DOES NOT YET HANDLE', id);
} }

View file

@ -128,6 +128,28 @@
} catch(e) {} } catch(e) {}
} }
removeScreen(); removeScreen();
oml.bindEvent({
close: function() {
if(!$('#offline').length) {
$('<div>')
.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();
}
});
}); });
} }