Skip to content

Commit 39dd2cd

Browse files
fixes async issues
1 parent d0d1fe3 commit 39dd2cd

File tree

3 files changed

+35
-43
lines changed

3 files changed

+35
-43
lines changed

lib/controllers/environment.py

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,10 @@ async def create_env(self) -> "Union[EnvCreated, HTTPException]":
6969
Returns:
7070
views.EnvCreated
7171
"""
72-
env_repo = EnvRepository(environment=self.env)
7372
try:
74-
await env_repo.create_env()
73+
created_env = await EnvRepository(
74+
environment=self.env
75+
).create_env()
7576
except Exception as e:
7677
exc_str = parse_error(e)
7778
logging.error(
@@ -82,7 +83,7 @@ async def create_env(self) -> "Union[EnvCreated, HTTPException]":
8283
detail=f"Failed to create environment: {e}",
8384
) from e
8485
else:
85-
return EnvCreated(env_id=env_repo.env_id)
86+
return EnvCreated(env_id=created_env.env_id)
8687
finally:
8788
logging.info(
8889
f"[{datetime.now()}] Call to controllers.environment.create_env completed; params: Env {hash(self.env)}"
@@ -102,9 +103,8 @@ async def get_env_by_id(env_id: int) -> "Union[Env, HTTPException]":
102103
Raises:
103104
HTTP 404 Not Found: If the env is not found in the database.
104105
"""
105-
env_repo = EnvRepository(env_id=env_id)
106106
try:
107-
read_env = await env_repo.get_env()
107+
read_env = await EnvRepository(env_id=env_id).get_env()
108108
except Exception as e:
109109
exc_str = parse_error(e)
110110
logging.error(
@@ -143,9 +143,10 @@ async def get_rocketpy_env_as_jsonpickle(
143143
Raises:
144144
HTTP 404 Not Found: If the env is not found in the database.
145145
"""
146-
env_repo = EnvRepository(env_id=env_id)
147146
try:
148-
read_env = await env_repo.get_env()
147+
read_env = await cls.get_env_by_id(env_id)
148+
except HTTPException as e:
149+
raise e from e
149150
except Exception as e:
150151
exc_str = parse_error(e)
151152
logging.error(
@@ -156,14 +157,9 @@ async def get_rocketpy_env_as_jsonpickle(
156157
detail=f"Failed to read environment: {e}",
157158
) from e
158159
else:
159-
if read_env:
160-
rocketpy_env = cls.get_rocketpy_env(read_env)
161-
return EnvPickle(
162-
jsonpickle_rocketpy_env=jsonpickle.encode(rocketpy_env)
163-
)
164-
raise HTTPException(
165-
status_code=status.HTTP_404_NOT_FOUND,
166-
detail="Environment not found",
160+
rocketpy_env = await cls.get_rocketpy_env(read_env)
161+
return EnvPickle(
162+
jsonpickle_rocketpy_env=jsonpickle.encode(rocketpy_env)
167163
)
168164
finally:
169165
logging.info(
@@ -185,16 +181,13 @@ async def update_env(
185181
Raises:
186182
HTTP 404 Not Found: If the env is not found in the database.
187183
"""
188-
env_repo = EnvRepository(environment=self.env, env_id=env_id)
189184
try:
190-
read_env = await env_repo.get_env()
191-
if read_env:
192-
await env_repo.update_env()
193-
else:
194-
raise HTTPException(
195-
status_code=status.HTTP_404_NOT_FOUND,
196-
detail="Environment not found",
197-
)
185+
await EnvController.get_env_by_id(env_id)
186+
updated_env = await EnvRepository(
187+
environment=self.env, env_id=env_id
188+
).update_env()
189+
except HTTPException as e:
190+
raise e from e
198191
except Exception as e:
199192
exc_str = parse_error(e)
200193
logging.error(
@@ -205,7 +198,7 @@ async def update_env(
205198
detail=f"Failed to update environment: {e}",
206199
) from e
207200
else:
208-
return EnvUpdated(new_env_id=env_repo.env_id)
201+
return EnvUpdated(new_env_id=updated_env.env_id)
209202
finally:
210203
logging.info(
211204
f"[{datetime.now()}] Call to controllers.environment.update_env completed; params: EnvID {env_id}, Env {hash(self.env)}"
@@ -225,9 +218,8 @@ async def delete_env(env_id: str) -> "Union[EnvDeleted, HTTPException]":
225218
Raises:
226219
HTTP 404 Not Found: If the env is not found in the database.
227220
"""
228-
env_repo = EnvRepository(env_id=env_id)
229221
try:
230-
await env_repo.delete_env()
222+
await EnvRepository(env_id=env_id).delete_env()
231223
except Exception as e:
232224
exc_str = parse_error(e)
233225
logging.error(
@@ -258,9 +250,9 @@ async def simulate(cls, env_id: int) -> "Union[EnvSummary, HTTPException]":
258250
Raises:
259251
HTTP 404 Not Found: If the env does not exist in the database.
260252
"""
261-
read_env = await cls.get_env_by_id(env_id)
262253
try:
263-
rocketpy_env = cls.get_rocketpy_env(read_env)
254+
read_env = await cls.get_env_by_id(env_id)
255+
rocketpy_env = await cls.get_rocketpy_env(read_env)
264256
env_simulation_numbers = EnvData.parse_obj(
265257
rocketpy_env.all_info_returned()
266258
)
@@ -270,6 +262,8 @@ async def simulate(cls, env_id: int) -> "Union[EnvSummary, HTTPException]":
270262
env_summary = EnvSummary(
271263
env_data=env_simulation_numbers, env_plots=env_simulation_plots
272264
)
265+
except HTTPException as e:
266+
raise e from e
273267
except Exception as e:
274268
exc_str = parse_error(e)
275269
logging.error(

lib/repositories/environment.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def __init__(self, environment: Env = None, env_id: str = None):
2222
if env_id:
2323
self._env_id = env_id
2424
else:
25-
self._env_id = hash(self._env)
25+
self._env_id = str(hash(self._env))
2626

2727
@property
2828
def env(self) -> "Env":
@@ -45,12 +45,8 @@ async def create_env(self):
4545
Creates a non-existing models.Env in the database
4646
4747
Returns:
48-
None
48+
self
4949
"""
50-
env_exists = await self.get_env()
51-
if env_exists:
52-
return
53-
5450
try:
5551
environment_to_dict = self.env.dict()
5652
environment_to_dict["env_id"] = self.env_id
@@ -61,22 +57,24 @@ async def create_env(self):
6157
f"[{datetime.now()}] repositories.environment.create_env: {exc_str}"
6258
)
6359
raise Exception(f"Error creating environment: {str(e)}") from e
60+
else:
61+
return self
6462
finally:
6563
logging.info(
6664
f"[{datetime.now()}] Call to repositories.environment.create_env completed; states: Env {hash(self.env)}, EnvID {self.env_id}"
6765
)
68-
self.close_connection()
66+
await self.close_connection()
6967

7068
async def update_env(self):
7169
"""
7270
Updates a models.Env in the database
7371
7472
Returns:
75-
None
73+
self
7674
"""
7775
try:
7876
environment_to_dict = self.env.dict()
79-
environment_to_dict["env_id"] = hash(self.env)
77+
environment_to_dict["env_id"] = str(hash(self.env))
8078
await self.collection.update_one(
8179
{"env_id": self.env_id}, {"$set": environment_to_dict}
8280
)
@@ -88,12 +86,12 @@ async def update_env(self):
8886
)
8987
raise Exception(f"Error updating environment: {str(e)}") from e
9088
else:
91-
return
89+
return self
9290
finally:
9391
logging.info(
9492
f"[{datetime.now()}] Call to repositories.environment.update_env completed; states: Env {hash(self.env)}, EnvID {self.env_id}"
9593
)
96-
self.close_connection()
94+
await self.close_connection()
9795

9896
async def get_env(self) -> "Union[Env, None]":
9997
"""
@@ -115,7 +113,7 @@ async def get_env(self) -> "Union[Env, None]":
115113
logging.info(
116114
f"[{datetime.now()}] Call to repositories.environment.get_env completed; states: Env {hash(self.env)}, EnvID {self.env_id}"
117115
)
118-
self.close_connection()
116+
await self.close_connection()
119117

120118
async def delete_env(self):
121119
"""
@@ -135,4 +133,4 @@ async def delete_env(self):
135133
logging.info(
136134
f"[{datetime.now()}] Call to repositories.environment.delete_env completed; states: Env {hash(self.env)}, EnvID {self.env_id}"
137135
)
138-
self.close_connection()
136+
await self.close_connection()

lib/repositories/repo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@ def __init__(self, collection: str):
2727
self.db = self.client.rocketpy
2828
self.collection = self.db[collection]
2929

30-
def close_connection(self) -> None:
30+
async def close_connection(self) -> None:
3131
logging.info(f"[{datetime.now()}] Closing connection to database")
3232
self.client.close()

0 commit comments

Comments
 (0)