27
27
28
28
29
29
class ADSWorkRequest (OCIDataScienceMixin ):
30
+ """Class for monitoring OCI WorkRequest and representing on tqdm progress bar. This class inherits
31
+ `OCIDataScienceMixin` so as to call its `client` attribute to interact with OCI backend.
32
+ """
30
33
31
34
def __init__ (
32
35
self ,
@@ -37,6 +40,27 @@ def __init__(
37
40
client_kwargs : dict = None ,
38
41
** kwargs
39
42
) -> None :
43
+ """Initializes ADSWorkRequest object.
44
+
45
+ Parameters
46
+ ----------
47
+ id: str
48
+ Work Request OCID.
49
+ description: str
50
+ Progress bar initial step description (Defaults to `Processing`).
51
+ config : dict, optional
52
+ OCI API key config dictionary to initialize
53
+ oci.data_science.DataScienceClient (Defaults to None).
54
+ signer : oci.signer.Signer, optional
55
+ OCI authentication signer to initialize
56
+ oci.data_science.DataScienceClient (Defaults to None).
57
+ client_kwargs : dict, optional
58
+ Additional client keyword arguments to initialize
59
+ oci.data_science.DataScienceClient (Defaults to None).
60
+ kwargs:
61
+ Additional keyword arguments to initialize
62
+ oci.data_science.DataScienceClient.
63
+ """
40
64
self .id = id
41
65
self ._description = description
42
66
self ._percentage = 0
@@ -45,6 +69,7 @@ def __init__(
45
69
46
70
47
71
def _sync (self ):
72
+ """Fetches the latest work request information to ADSWorkRequest object."""
48
73
work_request = self .client .get_work_request (self .id ).data
49
74
work_request_logs = self .client .list_work_request_logs (
50
75
self .id
@@ -57,17 +82,35 @@ def _sync(self):
57
82
def watch (
58
83
self ,
59
84
progress_callback : Callable ,
60
- max_wait_time : int ,
61
- poll_interval : int ,
85
+ max_wait_time : int = DEFAULT_WAIT_TIME ,
86
+ poll_interval : int = DEFAULT_POLL_INTERVAL ,
62
87
):
88
+ """Updates the progress bar with realtime message and percentage until the process is completed.
89
+
90
+ Parameters
91
+ ----------
92
+ progress_callback: Callable
93
+ Progress bar callback function.
94
+ It must accept `(percent_change, description)` where `percent_change` is the
95
+ work request percent complete and `description` is the latest work request log message.
96
+ max_wait_time: int
97
+ Maximum amount of time to wait in seconds (Defaults to 1200).
98
+ Negative implies infinite wait time.
99
+ poll_interval: int
100
+ Poll interval in seconds (Defaults to 10).
101
+
102
+ Returns
103
+ -------
104
+ None
105
+ """
63
106
previous_percent_complete = 0
64
107
65
108
start_time = time .time ()
66
109
while self ._percentage < 100 :
67
110
68
111
seconds_since = time .time () - start_time
69
112
if max_wait_time > 0 and seconds_since >= max_wait_time :
70
- logger .error (f"Max wait time ( { max_wait_time } seconds) exceeded ." )
113
+ logger .error (f"Exceeded max wait time of { max_wait_time } seconds." )
71
114
return
72
115
73
116
time .sleep (poll_interval )
@@ -80,7 +123,10 @@ def watch(
80
123
81
124
percent_change = self ._percentage - previous_percent_complete
82
125
previous_percent_complete = self ._percentage
83
- progress_callback (percent_change , self ._description )
126
+ progress_callback (
127
+ progress = percent_change ,
128
+ description = self ._description
129
+ )
84
130
85
131
if self ._status in WORK_REQUEST_STOP_STATE :
86
132
if self ._status != oci .work_requests .models .WorkRequest .STATUS_SUCCEEDED :
@@ -90,19 +136,38 @@ def watch(
90
136
raise Exception (
91
137
"Error occurred in attempt to perform the operation. "
92
138
"Check the service logs to get more details. "
139
+ f"Work request id: { self .id } ."
93
140
)
94
141
else :
95
142
break
96
143
97
- progress_callback (0 , "Done" )
144
+ progress_callback (progress = 0 , description = "Done" )
98
145
99
146
100
147
def wait_work_request (
101
148
id : str ,
102
- progress_bar_description : str ,
149
+ progress_bar_description : str = "Processing" ,
103
150
max_wait_time : int = DEFAULT_WAIT_TIME ,
104
151
poll_interval : int = DEFAULT_POLL_INTERVAL
105
152
):
153
+ """Waits for the work request progress bar to be completed.
154
+
155
+ Parameters
156
+ ----------
157
+ id: str
158
+ Work Request OCID.
159
+ progress_bar_description: str
160
+ Progress bar initial step description (Defaults to `Processing`).
161
+ max_wait_time: int
162
+ Maximum amount of time to wait in seconds (Defaults to 1200).
163
+ Negative implies infinite wait time.
164
+ poll_interval: int
165
+ Poll interval in seconds (Defaults to 10).
166
+
167
+ Returns
168
+ -------
169
+ None
170
+ """
106
171
ads_work_request = ADSWorkRequest (id )
107
172
108
173
with tqdm (
0 commit comments