Skip to content

Commit 1ba090e

Browse files
author
Ilyas Gasanov
committed
[DOP-21799] Refactor celery initialization
1 parent 748a15a commit 1ba090e

File tree

5 files changed

+16
-13
lines changed

5 files changed

+16
-13
lines changed

syncmaster/backend/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ def application_factory(settings: Settings) -> FastAPI:
3232
redoc_url=None,
3333
)
3434
application.state.settings = settings
35-
application.state.celery = celery_factory(settings)
3635
application.include_router(api_router)
3736
application.exception_handler(RequestValidationError)(validation_exception_handler)
3837
application.exception_handler(ValidationError)(validation_exception_handler)
@@ -47,7 +46,6 @@ def application_factory(settings: Settings) -> FastAPI:
4746
{
4847
Settings: lambda: settings,
4948
UnitOfWork: get_uow(session_factory, settings=settings),
50-
Celery: lambda: application.state.celery,
5149
},
5250
)
5351

syncmaster/backend/api/v1/runs.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
from typing import Annotated
66

77
from asgi_correlation_id import correlation_id
8-
from celery import Celery
98
from fastapi import APIRouter, Depends, Query
109
from jinja2 import Template
1110
from kombu.exceptions import KombuError
1211

12+
from syncmaster.backend.celery import app as celery
1313
from syncmaster.backend.dependencies import Stub
1414
from syncmaster.backend.services import UnitOfWork, get_user
1515
from syncmaster.backend.settings import ServerAppSettings as Settings
@@ -84,7 +84,6 @@ async def read_run(
8484
async def start_run(
8585
create_run_data: CreateRunSchema,
8686
settings: Annotated[Settings, Depends(Stub(Settings))],
87-
celery: Annotated[Celery, Depends(Stub(Celery))],
8887
unit_of_work: UnitOfWork = Depends(UnitOfWork),
8988
current_user: User = Depends(get_user(is_active=True)),
9089
) -> ReadRunSchema:

syncmaster/backend/celery.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# SPDX-FileCopyrightText: 2023-2024 MTS PJSC
2+
# SPDX-License-Identifier: Apache-2.0
3+
from syncmaster.backend.settings import ServerAppSettings
4+
from syncmaster.worker import celery_factory
5+
6+
app = celery_factory(ServerAppSettings())

tests/test_unit/test_runs/test_create_run.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ async def test_developer_plus_can_create_run_of_transfer_his_group(
2020
) -> None:
2121
# Arrange
2222
user = group_transfer.owner_group.get_member_of_role(role_developer_plus)
23-
mock_send_task = mocker.patch("syncmaster.worker.celery.app.send_task")
23+
mock_send_task = mocker.patch("syncmaster.backend.celery.app.send_task")
2424
mock_to_thread = mocker.patch("asyncio.to_thread", new_callable=AsyncMock)
2525

2626
run = (
@@ -73,7 +73,7 @@ async def test_groupless_user_cannot_create_run(
7373
mocker,
7474
) -> None:
7575
# Arrange
76-
mocker.patch("syncmaster.worker.celery.app.send_task")
76+
mocker.patch("syncmaster.backend.celery.app.send_task")
7777
mocker.patch("asyncio.to_thread", new_callable=AsyncMock)
7878

7979
# Act
@@ -103,7 +103,7 @@ async def test_group_member_cannot_create_run_of_other_group_transfer(
103103
role_guest_plus: UserTestRoles,
104104
):
105105
# Arrange
106-
mocker.patch("syncmaster.worker.celery.app.send_task")
106+
mocker.patch("syncmaster.backend.celery.app.send_task")
107107
mocker.patch("asyncio.to_thread", new_callable=AsyncMock)
108108
user = group.get_member_of_role(role_guest_plus)
109109

@@ -139,7 +139,7 @@ async def test_superuser_can_create_run(
139139
mocker,
140140
) -> None:
141141
# Arrange
142-
mock_send_task = mocker.patch("syncmaster.worker.celery.app.send_task")
142+
mock_send_task = mocker.patch("syncmaster.backend.celery.app.send_task")
143143
mock_to_thread = mocker.patch("asyncio.to_thread", new_callable=AsyncMock)
144144

145145
# Act
@@ -183,7 +183,7 @@ async def test_unauthorized_user_cannot_create_run(
183183
mocker,
184184
) -> None:
185185
# Arrange
186-
mocker.patch("syncmaster.worker.celery.app.send_task")
186+
mocker.patch("syncmaster.backend.celery.app.send_task")
187187
mocker.patch("asyncio.to_thread", new_callable=AsyncMock)
188188

189189
# Act
@@ -212,7 +212,7 @@ async def test_group_member_cannot_create_run_of_unknown_transfer_error(
212212
) -> None:
213213
# Arrange
214214
user = group_transfer.owner_group.get_member_of_role(role_guest_plus)
215-
mocker.patch("syncmaster.worker.celery.app.send_task")
215+
mocker.patch("syncmaster.backend.celery.app.send_task")
216216
mocker.patch("asyncio.to_thread", new_callable=AsyncMock)
217217

218218
# Act
@@ -240,7 +240,7 @@ async def test_superuser_cannot_create_run_of_unknown_transfer_error(
240240
mocker,
241241
) -> None:
242242
# Arrange
243-
mocker.patch("syncmaster.worker.celery.app.send_task")
243+
mocker.patch("syncmaster.backend.celery.app.send_task")
244244
mocker.patch("asyncio.to_thread", new_callable=AsyncMock)
245245

246246
# Act

tests/test_unit/test_scheduler/test_transfer_job_manager.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ async def test_send_job_to_celery_with_success(
8080
group_transfer: MockTransfer,
8181
):
8282
# Arrange
83-
mock_send_task = mocker.patch("syncmaster.worker.celery.app.send_task")
83+
mock_send_task = mocker.patch("syncmaster.scheduler.celery.app.send_task")
8484
mock_to_thread = mocker.patch("asyncio.to_thread", new_callable=AsyncMock)
8585

8686
# Act
@@ -107,7 +107,7 @@ async def test_send_job_to_celery_with_failure(
107107
group_transfer: MockTransfer,
108108
):
109109
# Arrange
110-
mocker.patch("syncmaster.worker.celery.app.send_task")
110+
mocker.patch("syncmaster.scheduler.celery.app.send_task")
111111
mocker.patch("asyncio.to_thread", new_callable=AsyncMock, side_effect=KombuError)
112112

113113
# Act & Assert

0 commit comments

Comments
 (0)