Skip to content

Commit 796afae

Browse files
committed
add scheduler celery
1 parent ecc17e5 commit 796afae

3 files changed

Lines changed: 40 additions & 2 deletions

File tree

api_v1/users/tasks.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from config import celery_app
1+
from config import celery_app, settings
22
import asyncio
33

44

@@ -9,3 +9,12 @@ async def time_sleep_task():
99
"""
1010
await asyncio.sleep(2.0)
1111
return 'Task is done'
12+
13+
14+
celery_app.conf.beat_schedule = {
15+
'test-every-10-seconds': {
16+
'task': 'llm_analizer.tasks.test',
17+
'schedule': settings.celery.TEST_TIMEDELTA,
18+
'args': ('hello',)
19+
},
20+
}

config/celery/connection.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,5 @@ def wrapper(*args,
4141

4242
app = Celery(__name__)
4343
app.conf.broker_url = settings.rabbit.broker_url
44+
app.conf.timezone = settings.celery.TIMEZONE
4445
app.autodiscover_tasks(packages=['api_v1.users'])

config/config.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
from pathlib import Path
22
from pydantic_settings import BaseSettings, SettingsConfigDict
3-
from pydantic import BaseModel
3+
from pydantic import BaseModel, ConfigDict
44
from starlette.config import Config
5+
from celery.schedules import crontab
56

67

78
base_dir = Path(__file__).resolve().parent.parent
@@ -11,6 +12,14 @@
1112
config = Config('.env')
1213

1314

15+
class AlembicSettings(BaseModel):
16+
"""
17+
Настройки Alembic
18+
"""
19+
CONFIG_PATH: Path = Path('alembic.ini')
20+
MIGRATION_PATH: Path = Path('async_alembic/')
21+
22+
1423
class TestDBSettings(BaseModel):
1524
"""
1625
Настройки тестовой базы данных
@@ -35,6 +44,23 @@ class DBSettings(BaseModel):
3544
url: str = f'{_engine}://{_owner}:{_password}@{_name}/{_db_name}'
3645

3746

47+
class CelerySettings(BaseModel):
48+
"""
49+
Настройки Celery
50+
"""
51+
model_config = ConfigDict(
52+
arbitrary_types_allowed=True,
53+
)
54+
TIMEZONE: str = 'Europe/Moscow'
55+
TIMEDELTA_PER_DAY: crontab = crontab(minute=0,
56+
hour=2,
57+
day_of_week='*/1',
58+
day_of_month='*/1',
59+
month_of_year='*/1',
60+
)
61+
TEST_TIMEDELTA: crontab = crontab(minute='*/1')
62+
63+
3864
class RabbitSettings(BaseModel):
3965
"""
4066
Настройки RabbitMQ
@@ -62,7 +88,9 @@ class Settings(BaseSettings):
6288
)
6389
db: DBSettings = DBSettings()
6490
test_db: TestDBSettings = TestDBSettings()
91+
celery: CelerySettings = CelerySettings()
6592
rabbit: RabbitSettings = RabbitSettings()
93+
alembic: AlembicSettings = AlembicSettings()
6694
debug: bool = bool(int(config('DEBUG')))
6795
API_PREFIX: str = '/api/v1'
6896
BASE_DIR: Path = base_dir

0 commit comments

Comments
 (0)