-
Notifications
You must be signed in to change notification settings - Fork 1
AB#259554: Sync of Offices should trigger Programme sync #153
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
AB#259554: Sync of Offices should trigger Programme sync #153
Conversation
69c2c27
to
a55ac15
Compare
a1718f7
to
4805968
Compare
@sergey-misuk-valor pls lint, test, and conflicts |
3150d9e
to
de65c1e
Compare
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## develop #153 +/- ##
===========================================
+ Coverage 95.53% 95.61% +0.07%
===========================================
Files 181 181
Lines 5756 5790 +34
Branches 521 522 +1
===========================================
+ Hits 5499 5536 +37
+ Misses 156 152 -4
- Partials 101 102 +1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
de65c1e
to
1441c73
Compare
def db_connection_created(*_: Any, **__: Any) -> None: | ||
from .manager import cache_manager | ||
|
||
cache_manager.init() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sergey-misuk-valor If i am not mistaken this will be fired on every new db connection creation and do we need to initiate cache_manager on every time new connection is created?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://docs.djangoproject.com/en/5.2/ref/signals/#django.db.backends.signals.connection_created
Sent when the database wrapper makes the initial connection to the database. This is particularly useful if you’d like to send any post connection commands to the SQL backend.
it's only sent when initial connection created. But if it wasn't nothing complex or harmful happens in the init
. Here I'm solving the warning issued by Django, because init
implicitly imports db related code before db connection is performed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes but database wrapper initiates connection on every http request because CONN_MAX_AGE is set to 0 by default and Django closes connection at the end of the request. You can easily test it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems Sergey relocated cache initialization from CacheConfig.ready()
to a connection_created
signal handler to suppress the “Accessing the database during app initialization” runtime warning - early DB access can break migrations, etc. I suspect this warning is triggered by Constance reading its settings from the database in __init__().
Instead, we could leave cache_manager.init()
in ready()
and simply try to add:
CONSTANCE_DATABASE_CACHE_BACKEND = "default"
so that Constance’s reads go through Redis rather than the database - our default cache is Redis.
Or we can use the signal and set a non-zero CONN_MAX_AGE, but then init will depend on the connection pool, so it may never start when it needs to, or it may start in an unpredictable way - in this case additional tests will be required; with CONN_MAX_AGE = 0, it fires on every request, causing excessive re-initialization.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here's the init method:
def init(self) -> None:
from . import handlers # noqa: F401
try:
self.cache_timeout = config.CACHE_TIMEOUT
self.cache_by_version = config.CACHE_BY_VERSION
if self.cache_by_version:
self.cw_version = VERSION
except Exception as e:
logger.exception(e)
capture_exception(e)
It requires DB access to read cache config. Also we probably want to notice cache config change as it's handled using constance. So I think it's a good place to put this method call in a connection_created signal handler as we want to read cache config as soon as possible.
The CONN_MAX_AGE is a different story. It's our DB configuration issue. I'm not sure if it's done on purpose or not. But we can discuss it.
d1f751d
to
e6db1e7
Compare
e6db1e7
to
1fa6d92
Compare
1fa6d92
to
8eea531
Compare
AB#259554