prijatelilab / PrijateliTree

A FastAPI application used to play lab games and collect survey data.
MIT License
1 stars 0 forks source link

Create database singleton #129

Closed michplunkett closed 10 months ago

michplunkett commented 10 months ago

Describe Your Changes

Use a singleton design pattern limit distinct connections to the database.

Checklist Before Requesting a Review

Verified limited connections (1 for the application and 1 for DataGrip): SQL statement explanation

Screenshot 2023-12-27 at 10 46 57 PM

Tested via session creation and login:

Screenshot 2023-12-27 at 10 41 57 PM
michplunkett commented 10 months ago

I checked the prod database and it is still showing 12 connections. I'm hoping that after the application is cycled sometime in the next day or so, we'll be down to 4 connections since there are only 4 workers.

anisfeld commented 10 months ago

This code doesn't work. I'll try to fix it, but this is a good sign we need tests! @FedericoDM

2024-01-03T17:42:24.022525+00:00 app[web.1]:     result = context.run(func, *args)

2024-01-03T17:42:24.022525+00:00 app[web.1]:              ^^^^^^^^^^^^^^^^^^^^^^^^

2024-01-03T17:42:24.022525+00:00 app[web.1]:   File "/app/prijateli_tree/app/routers/games.py", line 153, in view_round

2024-01-03T17:42:24.022526+00:00 app[web.1]:     template_text = languages[player.language.abbr]

2024-01-03T17:42:24.022526+00:00 app[web.1]:                               ^^^^^^^^^^^^^^^

2024-01-03T17:42:24.022526+00:00 app[web.1]:   File "/app/prijateli_tree/app/database.py", line 262, in language

2024-01-03T17:42:24.022526+00:00 app[web.1]:     user = db.query(User).filter_by(id=self.user_id).one()

2024-01-03T17:42:24.022527+00:00 app[web.1]:            ^^^^^^^^

2024-01-03T17:42:24.022527+00:00 app[web.1]: AttributeError: type object 'Database' has no attribute 'query'

2024-01-03T17:42:24.023266+00:00 heroku[router]: at=info method=GET path="/games/6/player/32/round" host=intense-tundra-19419-c4bc16058534.herokuapp.com request_id=896cf634-3184-487a-8062-e9e5945a6ca4 fwd="128.135.204.189" dyno=web.1 connect=0ms service=22ms status=500 bytes=193 protocol=https