implement quit api. indicate if backend is offline
This commit is contained in:
parent
67ad0a82e9
commit
5d4ed8ffbc
8 changed files with 83 additions and 35 deletions
2
ctl
2
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
|
||||
|
|
18
oml/api.py
18
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)
|
||||
|
|
|
@ -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__)
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -128,6 +128,28 @@
|
|||
} catch(e) {}
|
||||
}
|
||||
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();
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue