Skip to content
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

fix: add user_plan to apply_async options #631

Merged
merged 1 commit into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions celery_task_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ def route_task(name, args, kwargs, options, task=None, **kw):
"""Function to dynamically route tasks to the proper queue.
Docs: https://docs.celeryq.dev/en/stable/userguide/routing.html#routers
"""
db_session = get_db_session()
user_plan = _get_user_plan_from_task(db_session, name, kwargs)

user_plan = options.get("user_plan")
if user_plan is None:
db_session = get_db_session()
user_plan = _get_user_plan_from_task(db_session, name, kwargs)
return route_tasks_based_on_user_plan(name, user_plan)
1 change: 1 addition & 0 deletions tasks/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ def apply_async(self, args=None, kwargs=None, **options):
celery_compatible_config = {
"time_limit": extra_config.get("hard_timelimit", None),
"soft_time_limit": extra_config.get("soft_timelimit", None),
"user_plan": user_plan,
}
options = {**options, **celery_compatible_config}

Expand Down
4 changes: 4 additions & 0 deletions tasks/tests/unit/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,7 @@ def test_apply_async_override(self, mocker):
headers=dict(created_timestamp="2023-06-13T10:01:01.000123"),
time_limit=400,
soft_time_limit=200,
user_plan=mock_celery_task_router(),
)

@pytest.mark.freeze_time("2023-06-13T10:01:01.000123")
Expand Down Expand Up @@ -610,6 +611,7 @@ def test_real_example_no_override(
soft_time_limit=None,
headers=dict(created_timestamp="2023-06-13T10:01:01.000123"),
time_limit=None,
user_plan="users-pr-inappm",
)

@pytest.mark.freeze_time("2023-06-13T10:01:01.000123")
Expand Down Expand Up @@ -654,6 +656,7 @@ def test_real_example_override_from_celery(
soft_time_limit=500,
headers=dict(created_timestamp="2023-06-13T10:01:01.000123"),
time_limit=600,
user_plan="users-enterprisey",
)

@pytest.mark.freeze_time("2023-06-13T10:01:01.000123")
Expand Down Expand Up @@ -698,4 +701,5 @@ def test_real_example_override_from_upload(
soft_time_limit=400,
headers=dict(created_timestamp="2023-06-13T10:01:01.000123"),
time_limit=450,
user_plan="users-enterprisey",
)
Loading