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

@shared_task not trigger when Django-channels with Celery #9087

Open
PunkFleet opened this issue Jun 21, 2024 · 0 comments
Open

@shared_task not trigger when Django-channels with Celery #9087

PunkFleet opened this issue Jun 21, 2024 · 0 comments

Comments

@PunkFleet
Copy link

PunkFleet commented Jun 21, 2024

I found a bug(i think celery calling redis is not perfect when django-channels exists. Celery share_task will not trigger.)

...

class WebhookView(generics.CreateAPIView):
    permission_classes = [AllowAny]
    parser_classes = [BodyParser]
    
    def post(self, request):
        logger.info('Received webhook request')
        .........
        logger.info('process_event webhook request')
        process_event.delay_on_commit(event)
        return Response(status=200)

@shared_task
def process_event(event):
    logger.info(event.type)
    event_id = event['id']
    if ProcessedEvent.has_processed_event(event_id):
        logger.info(f'Event {event_id} already processed')
        return

celery.py and settings.py

import os
from celery import Celery
from celery.utils.log import get_task_logger
from celery.schedules import crontab

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'main.settings')

app = Celery('main')

app.config_from_object('django.conf:settings', namespace='CELERY')

app.autodiscover_tasks()
app.conf.broker_connection_retry_on_startup=True
app.conf.worker_cancel_long_running_tasks_on_connection_loss=True,
# Celery
CELERY_BROKER_URL = 'redis://localhost:6379/0'
CELERY_RESULT_BACKEND = 'redis://localhost:6379/1'
CELERY_TIMEZONE = TIME_ZONE
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
# ASGI and Channels
ASGI_APPLICATION = "main.asgi.application"
CHANNEL_LAYERS = {
    "default": {
        "BACKEND": "channels_redis.core.RedisChannelLayer",
        "CONFIG": {
            "hosts": [('redis://localhost:6379/2')],
        },
    },
}

the share_task will not process by celery but django-channels. Did anyone has the problems? It's celery calling redis bug right?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant