Search code examples
pythoncelery

celery worker has downin when beat is up


I can't get it already a cuople hourse, can you please give me advice why my celery worker is down when i starting the beat, here is my code:

from celery import Celery

app = Celery('my_task', broker='redis://localhost', include=["tasks"])

app.conf.broker_connection_retry_on_startup = True

app.conf.beat_schedule = {
    'add-every-3-seconds': {
        'task': 'my_beat_task',
        'schedule': 3.0,
    },
}


@app.task
def my_task():
    print('123')


@app.task
def my_beat_task():
    print('123')

My commands to start

celery -A tasks worker -l info
celery -A tasks beat -l info

Error traceback

[2023-07-24 00:25:32,678: ERROR/MainProcess] Received unregistered task of type 'my_beat_task'.
The message has been ignored and discarded.

Did you remember to import the module containing this task?
Or maybe you're using relative imports?

Please see
https://docs.celeryq.dev/en/latest/internals/protocol.html
for more information.

The full contents of the message body was:
b'[[], {}, {"callbacks": null, "errbacks": null, "chain": null, "chord": null}]' (77b)

The full contents of the message headers:
{'lang': 'py', 'task': 'my_beat_task', 'id': '082f2c56-c59b-44e7-8881-af76f642a5f4', 'shadow': None, 'eta': None, 'expires': None, 'group': None, 'group_index': None, 'retries': 0, 'timelimit': [None, None], 'root_id': '082f2c56-c59b-44e7-8881-af76f642a5f4', 'parent_id': None, 'argsrepr': '()', 'kwargsrepr': '{}', 'origin': 'gen65371@fil-tobefilledbyoem', 'ignore_result': False, 'stamped_headers': None, 'stamps': {}}

The delivery info for this task is:
{'exchange': '', 'routing_key': 'celery'}
Traceback (most recent call last):
  File "/home/fila/Pycharm_Projects/Learning/celery/venv/lib/python3.10/site-packages/celery/worker/consumer/consumer.py", line 642, in on_task_received
    strategy = strategies[type_]
KeyError: 'my_beat_task'

project is empty, just a file 'tasks.py' and venv directory

I tryied everything...


Solution

  • Celery identifies a task by it's unique name. Most probably, task_name in your case is tasks.my_beat_task. From the error thrown, it's clear that there is no task with task_name my_beat_task. For getting the exact task_name used by celery, you can look at the log where you will find the list of tasks actually registered with celery.

    Replace the 'task': 'my_beat_task' line with 'task': 'tasks.my_beat_task', and it might work.