@@ -111,14 +111,13 @@ def create_model(
111
111
metadata : dict = None
112
112
) -> Model :
113
113
"""
114
- Register an external model in the Hydrosphere platform
115
- and uploads training data.
114
+ Register an external model and send training data.
116
115
"""
117
- registration_response = self ._register_model (name , schema , metadata )
116
+ response = self ._register_model (name , schema , metadata )
118
117
model = Model (
119
- name = registration_response ["model" ]["name" ],
120
- version = registration_response ["modelVersion" ],
121
- model_version_id = registration_response ["id" ]
118
+ name = response ["model" ]["name" ],
119
+ version = response ["modelVersion" ],
120
+ model_version_id = response ["id" ]
122
121
)
123
122
self ._upload_training_data (model .model_version_id , training_file )
124
123
self ._wait_for_data_processing (model .model_version_id )
@@ -127,39 +126,41 @@ def create_model(
127
126
def _wait_for_data_processing (
128
127
self ,
129
128
model_version_id ,
130
- timeout : int = 120 ,
131
129
retry : int = 3
132
130
) -> requests .Response :
133
- """Wait till the data gets processed."""
131
+ """Wait till the data gets registered or finishes the processing."""
132
+
133
+ def tick (sleep : int = 5 ) -> bool :
134
+ nonlocal retry
135
+ if retry == 0 :
136
+ return False
137
+ retry -= 1
138
+ time .sleep (sleep )
139
+ return True
140
+
134
141
url = urllib .parse .urljoin (
135
142
self .endpoint , f"/monitoring/profiles/batch/{ model_version_id } /status" )
136
-
137
143
result = None
138
144
while True :
139
145
result = requests .get (url )
140
146
if result .status_code != 200 :
141
- if retry > 0 :
142
- retry -= 1
143
- time .sleep (5 )
147
+ if tick ():
144
148
continue
145
149
else :
146
150
raise errors .ApiNotAvailable (
147
151
"Could not fetch the status of the data processing task." )
148
152
149
153
body = result .json ()
150
154
status = DataProfileStatus [body ["kind" ]]
151
- if status == DataProfileStatus .Processing :
152
- if timeout > 0 :
153
- seconds = min (10 , timeout )
154
- timeout -= seconds
155
- time .sleep (seconds )
155
+
156
+ if status in (DataProfileStatus .Success , DataProfileStatus .Processing ):
157
+ # Don't wait until all data gets processed, release immediately
158
+ break
159
+ elif status == DataProfileStatus .NotRegistered :
160
+ # If profile hasn't been registered yet, wait for a little more
161
+ if tick ():
156
162
continue
157
- else :
158
- raise errors .TimeOut ("Data processing timed out." )
159
- elif status == DataProfileStatus .Success :
160
- break
161
- else :
162
- raise errors .DataUploadFailed (f"Failed to upload the data: { status } " )
163
+ raise errors .DataUploadFailed (f"Failed to upload the data: { status } " )
163
164
return result
164
165
165
166
def _upload_training_data (self , model_version_id : int , training_file : str ) -> requests .Response :
@@ -189,8 +190,7 @@ def _register_model(self, name: str, schema: SchemaDescription, metadata: dict)
189
190
response = result .json ()
190
191
self .logger .info ("Registered a new model: %s" , response ["model" ]["name" ])
191
192
else :
192
- raise errors .ModelRegistrationFailed (
193
- f"Could not register a model: { result .content } " )
193
+ raise errors .ModelRegistrationFailed (f"Could not register a model: { result .content } " )
194
194
return response
195
195
196
196
def _create_feature (self , column : ColumnDescription ) -> dict :
0 commit comments