Skip to content

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

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from

Conversation

sergey-misuk-valor
Copy link
Contributor

@sergey-misuk-valor sergey-misuk-valor commented Jun 26, 2025

@sergey-misuk-valor sergey-misuk-valor force-pushed the feature/259554-sync-of-offices-should-trigger-programme-sync branch 2 times, most recently from 69c2c27 to a55ac15 Compare June 30, 2025 13:15
@sergey-misuk-valor sergey-misuk-valor force-pushed the feature/259554-sync-of-offices-should-trigger-programme-sync branch 2 times, most recently from a1718f7 to 4805968 Compare July 14, 2025 09:43
@domdinicola
Copy link
Collaborator

@sergey-misuk-valor pls lint, test, and conflicts

@sergey-misuk-valor sergey-misuk-valor force-pushed the feature/259554-sync-of-offices-should-trigger-programme-sync branch 4 times, most recently from 3150d9e to de65c1e Compare July 29, 2025 09:36
Copy link

codecov bot commented Jul 29, 2025

Codecov Report

❌ Patch coverage is 96.92833% with 9 lines in your changes missing coverage. Please review.
✅ Project coverage is 95.61%. Comparing base (92f2121) to head (8eea531).

Files with missing lines Patch % Lines
src/country_workspace/contrib/hope/sync/base.py 92.47% 6 Missing and 1 partial ⚠️
...country_workspace/contrib/hope/sync/context_geo.py 95.91% 0 Missing and 2 partials ⚠️
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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@sergey-misuk-valor sergey-misuk-valor force-pushed the feature/259554-sync-of-offices-should-trigger-programme-sync branch from de65c1e to 1441c73 Compare July 29, 2025 12:28
@domdinicola domdinicola requested review from datamik and patsatsia July 29, 2025 13:58
def db_connection_created(*_: Any, **__: Any) -> None:
from .manager import cache_manager

cache_manager.init()
Copy link
Contributor

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?

Copy link
Contributor Author

@sergey-misuk-valor sergey-misuk-valor Jul 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@datamik

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

Copy link
Contributor

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.

Copy link
Contributor

@vitali-yanushchyk-valor vitali-yanushchyk-valor Jul 31, 2025

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@datamik

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.

@sergey-misuk-valor sergey-misuk-valor force-pushed the feature/259554-sync-of-offices-should-trigger-programme-sync branch from d1f751d to e6db1e7 Compare July 30, 2025 13:24
@sergey-misuk-valor sergey-misuk-valor force-pushed the feature/259554-sync-of-offices-should-trigger-programme-sync branch from e6db1e7 to 1fa6d92 Compare July 30, 2025 13:57
@sergey-misuk-valor sergey-misuk-valor force-pushed the feature/259554-sync-of-offices-should-trigger-programme-sync branch from 1fa6d92 to 8eea531 Compare July 30, 2025 17:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants