chore(api): migrate schedule tasks to use Session(db.engine)#35232
Open
jerryzai wants to merge 6 commits intolanggenius:mainfrom
Open
chore(api): migrate schedule tasks to use Session(db.engine)#35232jerryzai wants to merge 6 commits intolanggenius:mainfrom
jerryzai wants to merge 6 commits intolanggenius:mainfrom
Conversation
Contributor
Pyrefly Diffbase → PR--- /tmp/pyrefly_base.txt 2026-04-15 02:19:39.081559899 +0000
+++ /tmp/pyrefly_pr.txt 2026-04-15 02:19:29.802351074 +0000
@@ -579,7 +579,7 @@
ERROR Argument `SimpleNamespace` is not assignable to parameter `dataset` with type `Dataset` in function `dify_vdb_weaviate.weaviate_vector.WeaviateVectorFactory.init_vector` [bad-argument-type]
--> providers/vdb/vdb-weaviate/tests/unit_tests/test_weaviate_vector.py:912:42
ERROR No matching overload found for function `redis.client.Redis.__init__` called with arguments: (host=int | str | Unknown, port=int | str | Unknown, password=int | str | Unknown | None, db=int, ssl=bool, ssl_ca_certs=str | None, ssl_cert_reqs=Any | None, ssl_certfile=Any | None, ssl_keyfile=Any | None, socket_timeout=Literal[5], socket_connect_timeout=Literal[5], health_check_interval=Literal[30]) [no-matching-overload]
- --> schedule/queue_monitor_task.py:14:21
+ --> schedule/queue_monitor_task.py:13:21
ERROR Object of class `Tenant` has no attribute `role` [missing-attribute]
--> services/account_service.py:1154:13
ERROR No matching overload found for function `flask.helpers.stream_with_context` called with arguments: (Generator[bytes]) [no-matching-overload]
|
Contributor
There was a problem hiding this comment.
Pull request overview
Refactors Celery scheduled tasks under api/schedule/ to avoid using the Flask-SQLAlchemy scoped db.session by introducing explicit SQLAlchemy Session(db.engine) lifetimes, as part of the broader effort to remove flask_sqlalchemy usage in non-request contexts.
Changes:
- Replaced
db.sessionusage with context-managedSession(db.engine)blocks in several scheduled tasks. - Removed a stale
finally: db.session.close()guard fromqueue_monitor_task.py. - Tightened session ownership so tasks control session lifetime explicitly.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| api/schedule/clean_embedding_cache_task.py | Uses Session(db.engine) for batched embedding cleanup commits. |
| api/schedule/check_upgradable_plugin_task.py | Loads auto-upgrade strategies via Session(db.engine) (but session scope is now too broad). |
| api/schedule/queue_monitor_task.py | Removes db.session import/close guard; task is Redis/email only. |
| api/schedule/update_tidb_serverless_status_task.py | Loads TiDB bindings via Session(db.engine) before calling cluster status updater. |
| api/schedule/create_tidb_serverless_task.py | Uses Session(db.engine) for counting idle clusters and persisting newly created bindings. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
auto-merge was automatically disabled
April 15, 2026 02:33
Head branch was pushed to by a user without write access
Replace Session(db.engine) with session_factory.create_session() for consistency with other background tasks (expire_on_commit=False).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
db.sessionwithwith Session(db.engine) as session:in all Celery schedule task files underapi/schedule/finally: db.session.close()guard inqueue_monitor_task.py(no longer needed with context-managed sessions)Files changed
api/schedule/clean_embedding_cache_task.pyapi/schedule/check_upgradable_plugin_task.pyapi/schedule/queue_monitor_task.pyapi/schedule/update_tidb_serverless_status_task.pyapi/schedule/create_tidb_serverless_task.pyMotivation
Part of the ongoing effort to remove the
flask_sqlalchemydependency (#24138 → #23647).Schedule tasks run outside a Flask request context, so using the scoped
db.sessionhas always been fragile; explicitSession(db.engine)blocks make the session lifetime obvious and safe.Test plan
uv run --project api pytest)