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