Skip to content

Fix stale-requests cron by deferring auth setup to full app mode - #125

Closed
guillermodotn wants to merge 2 commits into
release-engineering:mainfrom
guillermodotn:fix/app-factory
Closed

guillermodotn wants to merge 2 commits into
release-engineering:mainfrom
guillermodotn:fix/app-factory

Conversation

@guillermodotn

Copy link
Copy Markdown
Contributor

The stale-requests CronJob was failing at startup because LDAP bind validation ran for every
cts-manager invocation, even though that job only needs database access.

This introduces a Flask application factory with two modes:

  • minimal config, DB, and models only (batch CLI, migrations, stale-requests cron)
  • full auth, views, telemetry (WSGI, dev server, openapispec)

LDAP configuration is now validated only when LDAP is actually queried, not at process startup.

@codecov-commenter

codecov-commenter commented Sep 7, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 86.66667% with 14 lines in your changes missing coverage. Please review.
✅ Project coverage is 84.19%. Comparing base (b50cfe3) to head (254b630).

Files with missing lines Patch % Lines
cts/__init__.py 91.76% 7 Missing ⚠️
cts/manage.py 0.00% 6 Missing ⚠️
cts/messaging.py 83.33% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #125      +/-   ##
==========================================
+ Coverage   83.84%   84.19%   +0.35%     
==========================================
  Files          13       13              
  Lines        1374     1405      +31     
==========================================
+ Hits         1152     1183      +31     
  Misses        222      222              
Flag Coverage Δ
unit-tests 84.19% <86.66%> (+0.35%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 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.

@lubomir lubomir left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The main problem with the original code base is that it relies on global variables that are initialized at module import time. This makes it difficult to test and customize things where needed. The PR changes adds functions for the initializations, but keeps the global state. It needs to add complexity to ensure the state is read correctly. Architecturally, this doesn't seem better than prior state.

I'm wondering it would be easier to do a phased approach here. First convert the codebase to follow Flask best practices (current_app and app.config), and then fix the problem if still present.

Comment thread cts/__init__.py
if "cts.views" in sys.modules:
import cts.views

cts.views.conf = conf

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This seems rather fragile. If another module starts importing the config, it needs to be added here or confusing failures happen.

Comment thread cts/manage.py
from cts import create_app, db, models

# Bootstrap configuration for CLI option defaults without web auth/views.
create_app(mode="minimal")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

With this import at top level, some of the handlers could create the application twice. Would it be cleaner to only create the app in the handlers?

Comment thread tests/test_auth.py
if "cts.views" in sys.modules:
import cts.views

cts.views.conf = self._saved_conf

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is effectively the same thing as _sync_conf_binding.

@guillermodotn

Copy link
Copy Markdown
Contributor Author

Close in favor of deeper refactor

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.

3 participants