Himanshu Poddar
Himanshu Poddar

Reputation: 7799

Celery does not discover tasks inside project

I have a project myproject and an app app.

Inside myproject I have tasks.py

from celery import shared_task

@shared_task
def add(x, y):
    return x + y

Inside my app I have the following tasks.py

from celery import shared_task
from django.core.mail import send_mail


@shared_task
def send_email_task(email):
    "background task to send an email asynchronously"
    subject = 'Hello from Celery'
    message = 'This is a test email sent asynchronously with Celery.'
    time.sleep(1)
    return send_mail(
        subject,
        message,
        '[email protected]',
        [email],
        fail_silently=False
    )

When running the celery workers I see only shared tasks from the app and not from myproject

(myprojectenv) root@ubuntu-s-1vcpu-1gb-blr1-02:/etc/myproject# celery -A myproject worker -l info
/etc/myprojectenv/lib/python3.8/site-packages/celery/platforms.py:840: SecurityWarning: You're running the worker with superuser privileges: this is
absolutely not recommended!

Please specify a different user using the --uid option.

User information: uid=0 euid=0 gid=0 egid=0

  warnings.warn(SecurityWarning(ROOT_DISCOURAGED.format(
                .> celery           exchange=celery(direct) key=celery


[tasks]
  . app.tasks.send_email_task

[2022-06-15 09:16:44,314: INFO/MainProcess] Connected to amqp://hpoddar:**@IPADDRESS:5672/vhostcheck
[2022-06-15 09:16:44,322: INFO/MainProcess] mingle: searching for neighbors

Here is my celery.py file

import os
from celery import Celery

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')
app = Celery('myproject')
app.autodiscover_tasks()

Upvotes: 1

Views: 1124

Answers (1)

Giovanni
Giovanni

Reputation: 1

You need add the import:

from django.conf import settings

Upvotes: 0

Related Questions