-
Notifications
You must be signed in to change notification settings - Fork 42
/
base.py
93 lines (70 loc) · 3 KB
/
base.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import logging
from PyQt5.QtCore import QObject, pyqtSignal
from sdclientapi import API, AuthError, RequestTimeoutError
from sqlalchemy.orm.session import Session
from typing import Any, Optional, TypeVar
logger = logging.getLogger(__name__)
DEFAULT_NUM_ATTEMPTS = 5
QueueJobType = TypeVar('QueueJobType', bound='QueueJob')
class ApiInaccessibleError(Exception):
def __init__(self, message: Optional[str] = None) -> None:
if not message:
message = ('API is inaccessible either because there is no client or because the '
'client is not properly authenticated.')
super().__init__(message)
class QueueJob(QObject):
def __init__(self) -> None:
super().__init__()
self.order_number = None # type: Optional[int]
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(QueueJob):
'''
Signal that is emitted after an job finishes successfully.
'''
success_signal = pyqtSignal('PyQt_PyObject')
'''
Signal that is emitted if there is a failure during the job.
'''
failure_signal = pyqtSignal(Exception)
def __init__(self, remaining_attempts: int = DEFAULT_NUM_ATTEMPTS) -> None:
super().__init__()
self.remaining_attempts = remaining_attempts
def _do_call_api(self, api_client: API, session: Session) -> None:
if not api_client:
raise ApiInaccessibleError()
while self.remaining_attempts >= 1:
try:
self.remaining_attempts -= 1
result = self.call_api(api_client, session)
except (AuthError, ApiInaccessibleError) as e:
raise ApiInaccessibleError() from e
except RequestTimeoutError as e:
if self.remaining_attempts == 0:
self.failure_signal.emit(e)
raise
except Exception as e:
self.failure_signal.emit(e)
raise
else:
self.success_signal.emit(result)
break
def call_api(self, api_client: API, session: Session) -> Any:
'''
Method for making the actual API call and handling the result.
This MUST resturn a value if the API call and other tasks were successful and MUST raise
an exception if and only if the tasks failed. Presence of a raise exception indicates a
failure.
'''
raise NotImplementedError