Skip to content

Commit

Permalink
log and auto-retry bg syncs
Browse files Browse the repository at this point in the history
  • Loading branch information
Allie Crevier committed Dec 3, 2019
1 parent 136163d commit b539b68
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
2 changes: 1 addition & 1 deletion securedrop_client/gui/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ def setup(self, controller):
self.controller.sync_events.connect(self._on_refresh_complete)

def _on_clicked(self):
self.controller.sync_api()
self.controller.sync_api(manual_retry=True)
# This is a temporary solution for showing the icon as active for the entire duration of a
# refresh, rather than for just the duration of a click. The icon image will be replaced
# when the controller tells us the refresh has finished. A cleaner solution would be to
Expand Down
29 changes: 23 additions & 6 deletions securedrop_client/logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,10 +305,12 @@ def completed_api_call(self, thread_id, user_callback):

def login(self, username, password, totp):
"""
Given a username, password and time based one-time-passcode (TOTP),
create a new instance representing the SecureDrop api and authenticate.
Given a username, password and time based one-time-passcode (TOTP), create a new instance
representing the SecureDrop api and authenticate.
Default to 60 seconds until we implement a better request timeout strategy.
Default to 60 seconds until we implement a better request timeout strategy. We lower the
default_request_timeout for Queue API requests in ApiJobQueue in order to display errors
faster.
"""
storage.mark_all_pending_drafts_as_failed(self.session)
self.api = sdclientapi.API(
Expand Down Expand Up @@ -377,7 +379,7 @@ def authenticated(self):
"""
return bool(self.api and self.api.token is not None)

def sync_api(self):
def sync_api(self, manual_retry: bool = False):
"""
Grab data from the remote SecureDrop API in a non-blocking manner.
"""
Expand All @@ -386,7 +388,13 @@ def sync_api(self):

if self.authenticated():
logger.debug("You are authenticated, going to make your call")
self.call_api(storage.get_remote_data,
if manual_retry:
self.call_api(storage.get_remote_data,
self.on_sync_success,
self.on_refresh_failure,
self.api)
else:
self.call_api(storage.get_remote_data,
self.on_sync_success,
self.on_sync_failure,
self.api)
Expand Down Expand Up @@ -443,8 +451,17 @@ def on_sync_success(self, result) -> None:

def on_sync_failure(self, result: Exception) -> None:
"""
Called when syncronisation of data via the API fails.
Called when syncronisation of data via the API fails. Just log and try again since this is
a background task.
"""
logger.debug('The SecureDrop server cannot be reached due to Error: {}'.format(result))
self.sync_api()

def on_refresh_failure(self, result: Exception) -> None:
"""
Called when syncronisation of data via the API fails after a user manual clicks refresh.
"""
logger.debug('The SecureDrop server cannot be reached due to Error: {}'.format(result))
self.gui.update_error_status(
_('The SecureDrop server cannot be reached.'),
duration=0,
Expand Down
10 changes: 10 additions & 0 deletions securedrop_client/queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,16 @@ def logout(self) -> None:

def login(self, api_client: API) -> None:
logger.debug('Passing API token to queues')

# Setting realistic (shorter) timeout for general requests so that user feedback is faster.
# General requests includes:
# FileDownloadJob
# MessageDownloadJob
# ReplyDownloadJo
# SendReplyJob
# UpdateStarJob
api_client.default_request_timeout = 5

self.main_queue.api_client = api_client
self.download_file_queue.api_client = api_client
self.start_queues()
Expand Down

0 comments on commit b539b68

Please sign in to comment.