Celery task execution when it is impossible to import this task

I have two servers: one works with a django application, and the other works both the rabbitmq queue and the celery worker. My .py tasks on the server running the queue / worker contain the task as follows:

@task(queue="reports") def test_task(): time.sleep(120) 

My goal is to accomplish this task from a django view. Since the task code is on a different server than the django view, which I would call a task, I am trying to use the following code to send a task from django to a working computer.

 send_task("tasks.test_task", task_id=task_id, args=[], kwargs={}, publisher=publisher, queue=queue) 

I found this method here , but so far testing has not worked.

I am testing with the -F tail on the celery worker work file on the celery worker server, then navigating to the view URL containing send_task in the browser. I am looking for a task to show how "received" in the tail output, but it is not.

The celery workerโ€™s log level is DEBUG, the log file shows that the task is registered with the appropriate name, and the django settings.py application contains the correct IP address and credentials for the rabbitmq server. When trying different approaches, I sometimes saw an error message in the celery log file when I changed the line passed to send_task to what was an invalid task (i.e. send_task ('asdf')). This caused an UnregisteredError in the log file. However, this only happens occasionally, and so far, while testing various combinations of settings and calls, I have not found a way to reliably replicate behavior.

Also, this is the corresponding settings.py section in the django project (with actual values โ€‹โ€‹removed):

 CELERY_RESULT_BACKEND = 'amqp' BROKER_HOST = 'the.correct.IP.address' BROKER_USER = 'the_correct_user' BROKER_PASSWORD = 'the_correct_pass' BROKER_VHOST = 'the_correct_vhost' BROKER_PORT = 5672 

I googled around and did not find much on send_task. Any ideas on what I can do wrong?

+6
source share
2 answers

Solved, it turns out that the publisher arg keyword that I passed send_task was invalid and threw an error. I did not see the error, because I was an AJAX request for the page, and not navigating to it directly. Everything else in this situation was correct. I also removed the unnecessary keyword arguments and the arguments passed to send_task.

 send_task("tasks.test_task", task_id=task_id, queue=queue) 
+7
source

What [I thought you were] trying to do is impossible. Celery workers require access to the task code that they must complete. Nothing like this.

REVISED:

But what you really want to do is to have code available to workers, but NOT to represent Django, which should only reference tasks.

+2
source

Source: https://habr.com/ru/post/904104/


All Articles