Commit ab41bf0c authored by Giorgos Korfiatis's avatar Giorgos Korfiatis

Decouple auth, init and start

Use new statuses 'SETTINGS READY' and 'READY'.
parent 806c8eb3
......@@ -434,7 +434,10 @@ class AgkyraCLI(cmd.Cmd):
self.make_server_if_needed()
client = self.client
status = client.get_status()
if status['code'] == STATUS['PAUSED']:
if status['code'] == STATUS['SETTINGS READY']:
client.init()
status = client.get_status()
if status['code'] in [STATUS['PAUSED'], STATUS['READY']]:
client.start()
sys.stderr.write('Starting syncer ... ')
try:
......@@ -442,7 +445,7 @@ class AgkyraCLI(cmd.Cmd):
sys.stderr.write('OK\n')
except protocol_client.UIClientError as uice:
sys.stderr.write('%s\n' % uice)
else:
elif status['code'] == STATUS['SYNCING']:
sys.stderr.write('Already ')
sys.stderr.flush()
self.do_status(line)
......@@ -501,7 +504,7 @@ class AgkyraCLI(cmd.Cmd):
return
session = self.helper.load_active_session()
if not session:
self._start('')
self.make_server_if_needed()
session = self.helper.wait_session_to_load()
if session:
LOGGER.info('Start new GUI')
......
......@@ -477,7 +477,7 @@ class WebSocketProtocol(WebSocket):
"""Check if settings are enough to setup a syncing proccess"""
return all([self.settings[e] for e in self.essentials])
def init_sync(self):
def init_sync(self, leave_paused=False):
"""Initialize syncer"""
self.set_status(code=STATUS['INITIALIZING'])
sync = self._get_default_sync()
......@@ -531,7 +531,8 @@ class WebSocketProtocol(WebSocket):
break
if local_ok and remote_ok:
syncer_.initiate_probe()
self.set_status(code=STATUS['PAUSED'])
new_status = 'PAUSED' if leave_paused else 'READY'
self.set_status(code=STATUS[new_status])
except pithos_client.ClientError as ce:
LOGGER.debug('backend init failed: %s %s' % (ce, ce.status))
try:
......@@ -556,7 +557,8 @@ class WebSocketProtocol(WebSocket):
# Prepare setting save
could_sync = self.syncer and self.can_sync()
old_status = self.get_status('code')
active = (STATUS['SYNCING'], STATUS['PAUSING'], STATUS['PAUSED'])
ok_not_syncing = [STATUS['READY'], STATUS['PAUSING'], STATUS['PAUSED']]
active = ok_not_syncing + [STATUS['SYNCING']]
must_reset_syncing = self._essentials_changed(new_settings)
if must_reset_syncing and old_status in active:
......@@ -572,14 +574,10 @@ class WebSocketProtocol(WebSocket):
self._load_settings()
can_sync = must_reset_syncing and self.can_sync()
if can_sync:
leave_paused = old_status in ok_not_syncing or \
not self.settings.get('sync_on_start', False)
LOGGER.debug('Restart backend')
self.init_sync()
new_status = self.get_status('code')
if new_status in active:
must_sync = old_status == STATUS['SYNCING'] or (
old_status not in active and (
self.settings.get('sync_on_start', False)))
(self.start_sync if must_sync else self.pause_sync)()
self.init_sync(leave_paused=leave_paused)
def _pause_syncer(self):
syncer_ = self.syncer
......@@ -604,8 +602,7 @@ class WebSocketProtocol(WebSocket):
self.set_status(code=STATUS['INITIALIZING'])
self.syncer.settings.purge_db_archives_and_enable()
self.syncer.initiate_probe()
self.syncer.start_decide()
self.set_status(code=STATUS['SYNCING'])
self.set_status(code=STATUS['READY'])
def send_json(self, msg):
LOGGER.debug('send: %s' % msg)
......@@ -623,6 +620,7 @@ class WebSocketProtocol(WebSocket):
self.clean_db()
return
{
'init': self.init_sync,
'start': self.start_sync,
'pause': self.pause_sync,
'force': self.force_sync
......@@ -632,12 +630,9 @@ class WebSocketProtocol(WebSocket):
self.accepted = True
self.send_json({'ACCEPTED': 202, 'action': 'post ui_id'})
self._load_settings()
if (not self.syncer) and self.can_sync():
self.init_sync()
status = self.get_status('code')
if self.syncer and self.settings['sync_on_start'] and \
status == STATUS["PAUSED"]:
self.start_sync()
status = self.get_status('code')
if self.can_sync() and status == STATUS['UNINITIALIZED']:
self.set_status(code=STATUS['SETTINGS READY'])
else:
action = r.get('path', 'ui_id')
self.send_json({'REJECTED': 401, 'action': 'post %s' % action})
......
......@@ -93,6 +93,7 @@ class UIClient(WebSocketClient):
msg = json.loads('%s' % m)
{
'post ui_id': self.recv_authenticate,
'post init': self.recv_init,
'post start': self.recv_start,
'post pause': self.recv_pause,
'get status': self.recv_get_status,
......@@ -111,6 +112,11 @@ class UIClient(WebSocketClient):
'Client authentication failed', response=msg)
self.ready = True
def recv_init(self, msg):
"""Receive: init response"""
if 'OK' not in msg:
raise UnexpectedResponseError('Init failed', response=msg)
def recv_start(self, msg):
"""Receive: start response"""
if 'OK' not in msg:
......@@ -145,6 +151,11 @@ class UIClient(WebSocketClient):
self.wait_until_ready()
self._post('shutdown')
def init(self):
"""Request: POST INIT"""
self.wait_until_ready()
self._post('init')
def start(self):
"""Request: POST START"""
self.wait_until_ready()
......
......@@ -239,6 +239,8 @@ window.setInterval(function() {
var dialogue_msg = null;
var status_unchanged = (old_status == globals.status.code);
switch(globals.status.code) {
case STATUS['SETTINGS READY']:
if (!status_unchanged) post_init(socket);
case STATUS['UNINITIALIZED']:
case STATUS['INITIALIZING']:
case STATUS['SHUTING DOWN']:
......@@ -247,6 +249,10 @@ window.setInterval(function() {
new_pause = COMMON.MENU.INACTIVE;
tray_icon_off = true;
break;
case STATUS['READY']:
if (status_unchanged) break;
post_start(socket);
break;
case STATUS['SYNCING']:
notify('info');
activate_menu();
......
......@@ -79,6 +79,11 @@ function post_start(socket) {
send_json(socket, {'method': 'post', 'path': 'start'});
} // expected response: {"OK": 200}
function post_init(socket) {
log_debug('SEND post init');
send_json(socket, {'method': 'post', 'path': 'init'});
} // expected response: {"OK": 200}
function post_force(socket) {
log_debug('SEND post force');
send_json(socket, {'method': 'post', 'path': 'force'});
......@@ -130,6 +135,7 @@ socket.onmessage = function(e) {
break;
case 'post start':
case 'post pause':
case 'post init':
log_debug('RECV ' + r['OK']);
if (r['OK'] === 200) {
get_status(this);
......
......@@ -3,6 +3,8 @@
"UNINITIALIZED": 0,
"INITIALIZING": 1,
"SHUTTING DOWN": 2,
"SETTINGS READY": 5,
"READY": 10,
"SYNCING": 100,
"PAUSING": 101,
"PAUSED": 102,
......@@ -17,6 +19,8 @@
"0": "Ανενεργό",
"1": "Εκκίνηση ...",
"2": "Κλείσιμο",
"5": "Ανενεργό",
"10": "Σε παύση",
"100": "Σε συγχρονισμό",
"101": "Σε διαδικασία παύσης",
"102": "Σε παύση",
......
......@@ -3,6 +3,8 @@
"UNINITIALIZED": 0,
"INITIALIZING": 1,
"SHUTTING DOWN": 2,
"SETTINGS READY": 5,
"READY": 10,
"SYNCING": 100,
"PAUSING": 101,
"PAUSED": 102,
......@@ -17,6 +19,8 @@
"0": "Not initialized",
"1": "Initializing ...",
"2": "Shutting down",
"5": "Not initialized",
"10": "Paused",
"100": "Syncing",
"101": "Pausing",
"102": "Paused",
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment