Skip to content

Commit

Permalink
create QueueJob parent class
Browse files Browse the repository at this point in the history
  • Loading branch information
Allie Crevier committed Aug 12, 2019
1 parent a994d03 commit 1bf6083
Showing 1 changed file with 22 additions and 18 deletions.
40 changes: 22 additions & 18 deletions securedrop_client/api_jobs/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

DEFAULT_NUM_ATTEMPTS = 5

ApiJobType = TypeVar('ApiJobType', bound='ApiJob')
QueueJobType = TypeVar('QueueJobType', bound='QueueJob')


class ApiInaccessibleError(Exception):
Expand All @@ -22,13 +22,31 @@ def __init__(self, message: Optional[str] = None) -> None:
super().__init__(message)


class PauseQueueJob(QObject):
class QueueJob(QObject):
def __init__(self) -> None:
super().__init__()
self.order_number = None

def __lt__(self, other: QueueJobType) -> bool:
'''
Python's PriorityQueue requires that QueueJobs are sortable as it
retrieves the next job using sorted(list(entries))[0].
For QueueJobs that have equal priority, we need to use the order_number key
to break ties to ensure that objects are retrieved in FIFO order.
'''
if self.order_number is None or other.order_number is None:
raise ValueError('cannot compare jobs without order_number!')

return self.order_number < other.order_number


class PauseQueueJob(QueueJob):
def __init__(self) -> None:
super().__init__()

class ApiJob(QObject):

class ApiJob(QueueJob):

'''
Signal that is emitted after an job finishes successfully.
Expand All @@ -41,22 +59,8 @@ class ApiJob(QObject):
failure_signal = pyqtSignal(Exception)

def __init__(self, remaining_attempts: int = DEFAULT_NUM_ATTEMPTS) -> None:
super().__init__(None) # `None` because the QOjbect has no parent
super().__init__()
self.remaining_attempts = remaining_attempts
self.order_number = None # type: Optional[int]

def __lt__(self, other: ApiJobType) -> bool:
'''
Python's PriorityQueue requires that ApiJobs are sortable as it
retrieves the next job using sorted(list(entries))[0].
For ApiJobs that have equal priority, we need to use the order_number key
to break ties to ensure that objects are retrieved in FIFO order.
'''
if self.order_number is None or other.order_number is None:
raise ValueError('cannot compare jobs without order_number!')

return self.order_number < other.order_number

def _do_call_api(self, api_client: API, session: Session) -> None:
if not api_client:
Expand Down

0 comments on commit 1bf6083

Please sign in to comment.