Commit edf47a75 authored by Stavros Sachtouris's avatar Stavros Sachtouris Committed by Giorgos Korfiatis
Browse files

Adjust CLI to use common status mechanism

parent f121ea35
......@@ -20,6 +20,21 @@ from agkyra import config, protocol, protocol_client
LOG = logging.getLogger(__name__)
STATUS = protocol.STATUS
NOTIFICATION = {
0: 'Not initialized',
1: 'Initializing ...',
2: 'Shutting down',
100: 'Syncing',
101: 'Pausing',
102: 'Paused',
200: 'Settings are incomplete',
201: 'Cloud URL error',
202: 'Authentication error',
203: 'Local directory error',
204: 'Remote container error',
1000: 'Critical error'
}
class ConfigCommands:
......@@ -209,17 +224,6 @@ class AgkyraCLI(cmd.Cmd):
except AttributeError:
self.do_help('config')
@staticmethod
def _status_string(status=None):
"""Get the status string (Syncing, Paused, Not running)"""
if status:
remain = status.get('unsynced', 0) - status.get('synced', 0)
if status['paused']:
return ('Pausing, %s remain' % remain) if remain else 'Paused'
else:
return 'Syncing, %s remain' % remain
return 'Not running'
def do_status(self, line):
"""Get Agkyra client status. Status may be one of the following:
Syncing There is a process syncing right now
......@@ -227,8 +231,13 @@ class AgkyraCLI(cmd.Cmd):
Not running No active processes
"""
client = self.client
status = client.get_status() if client else None
sys.stdout.write('%s\n' % self._status_string(status))
status, msg = client.get_status() if client else None, 'Not running'
if status:
msg = NOTIFICATION[status['code']]
diff = status['unsynced'] - status['synced']
if diff:
msg = '%s, %s remaining' % (msg, diff)
sys.stdout.write('%s\n' % msg)
sys.stdout.flush()
# def do_start_daemon(self, line):
......@@ -254,15 +263,14 @@ class AgkyraCLI(cmd.Cmd):
sys.stderr.write('OK\n')
else:
status = client.get_status()
if status['paused']:
if status['code'] == STATUS['PAUSED']:
client.start()
sys.stderr.write('Starting syncer ... ')
try:
client.wait_until_syncing()
except AssertionError:
sys.stderr.write('\n')
raise
sys.stderr.write('OK\n')
sys.stderr.write('OK\n')
except AssertionError as ae:
sys.stderr.write('%s\n' % ae)
else:
sys.stderr.write('Already ')
sys.stderr.flush()
......@@ -273,17 +281,16 @@ class AgkyraCLI(cmd.Cmd):
client = self.client
if client:
status = client.get_status()
if status['paused']:
if status['code'] == STATUS['PAUSED']:
sys.stderr.write('Already ')
else:
client.pause()
sys.stderr.write('Pausing syncer ... ')
try:
client.wait_until_paused()
except AssertionError:
sys.stderr.write('Failed\n')
raise
sys.stderr.write('OK\n')
sys.stderr.write('OK\n')
except AssertionError as ae:
sys.stderr.write('%s\n' % ae)
sys.stderr.flush()
self.do_status(line)
......@@ -299,3 +306,7 @@ class AgkyraCLI(cmd.Cmd):
else:
sys.stderr.write('Not running\n')
sys.stderr.flush()
def do_florist(self, line):
"""RUN FLORIST, RUN!"""
sys.stderr.write('RUN: %s\n' % STATUS)
......@@ -38,7 +38,6 @@ with open(os.path.join(CURPATH, 'ui_common.json')) as f:
UI_COMMON = json.load(f)
STATUS = UI_COMMON['STATUS']
def retry_on_locked_db(method, *args, **kwargs):
"""If DB is locked, wait and try again"""
wait = kwargs.get('wait', 0.2)
......
......@@ -17,6 +17,7 @@ from ws4py.client.threadedclient import WebSocketClient
import json
import time
import logging
from protocol import STATUS
LOG = logging.getLogger(__name__)
......@@ -52,20 +53,22 @@ class UIClient(WebSocketClient):
def wait_until_syncing(self, timeout=20):
"""Wait until session reaches syncing status"""
status = self.get_status()
while timeout and status['paused']:
while timeout and status['code'] != STATUS['SYNCING']:
time.sleep(1)
status = self.get_status()
timeout -= 1
assert not status['paused'], 'Timed out, still in paused status'
msg = 'Timed out, still not syncing'
assert status['code'] == STATUS['SYNCING'], msg
def wait_until_paused(self, timeout=20):
"""Wait until session reaches paused status"""
status = self.get_status()
while timeout and not status['paused']:
while timeout and status['code'] != STATUS['PAUSED']:
time.sleep(1)
status = self.get_status()
timeout -= 1
assert status['paused'], 'Timed out, still in syncing status'
msg = 'Timed out, still not paused'
assert status['code'] == STATUS['PAUSED'], msg
def received_message(self, m):
"""handle server responces according to the protocol"""
......@@ -98,7 +101,7 @@ class UIClient(WebSocketClient):
def recv_get_status(self, msg):
"""Receive: GET STATUS"""
assert 'can_sync' in msg, json.dumps(msg)
assert 'code' in msg, json.dumps(msg)
self.buf[msg['action']] = msg
# API methods
......
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