Skip to content

Commit 8fc3fc3

Browse files
committed
add celery
1 parent 49f366f commit 8fc3fc3

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# This will make sure the app is always imported when
2+
# Django starts so that shared_task will use this app.
3+
from .celery import app as celery_app
4+
5+
__all__ = ('celery_app',)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import logfire
2+
from celery import Celery
3+
from celery.signals import worker_init, beat_init
4+
5+
app = Celery('proj')
6+
7+
8+
# Using a string here means the worker doesn't have to serialize
9+
# the configuration object to child processes.
10+
# - namespace='CELERY' means all celery-related configuration keys
11+
# should have a `CELERY_` prefix.
12+
app.config_from_object('django.conf:settings', namespace='CELERY')
13+
14+
# Load task modules from all registered Django apps.
15+
app.autodiscover_tasks()
16+
17+
18+
@worker_init.connect()
19+
def init_worker(*args, **kwargs):
20+
logfire.configure(service_name="celery-worker")
21+
logfire.instrument_celery()
22+
23+
@beat_init.connect()
24+
def init_beat(*args, **kwargs):
25+
logfire.configure(service_name="celery-beat")
26+
logfire.instrument_celery()
27+
28+
@app.task
29+
def add(x: int, y: int):
30+
return x + y
31+
32+
add.delay(42, 50)
33+
34+
35+
app.conf.beat_schedule = {
36+
"add-every-30-seconds": {
37+
"task": "tasks.add",
38+
"schedule": 30.0,
39+
"args": (16, 16),
40+
},
41+
}

pythonkr_backend/pythonkr_backend/settings/prod.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,14 @@
6666
logfire.configure(environment='prod', service_name="web", service_version=sha_service_version)
6767
logfire.instrument_django()
6868
logfire.instrument_system_metrics()
69-
#logfire.instrument_psycopg('psycopg')
69+
#logfire.instrument_psycopg('psycopg')
70+
71+
# celery
72+
CELERY_BROKER_PASSWORD = os.environ.get("CELERY_PASSWORD","FALSE")
73+
CELERY_BROKER_USERNAME = os.environ.get("CELERY_USERNAME","FALSE")
74+
CELERY_BROKER_VHOST = os.environ.get("CELERY_VHOST","FALSE")
75+
# Celery Configuration Options
76+
CELERY_TIMEZONE = "Asia/Seoul"
77+
CELERY_TASK_TRACK_STARTED = True
78+
CELERY_BROKER_URL = "amqp://userid:password@localhost:port/virtual_host"
79+
CELERY_TASK_TIME_LIMIT = 30 * 60

0 commit comments

Comments
 (0)