Denis Mokhov
Denis Mokhov

Reputation: 73

Celery's inspect unstable behaviour

I got celery project with RabbitMQ backend, that relies heavily on inspecting scheduled tasks. I found that the following code returns nothing for most of the time (of course, there are scheduled tasks) :

i = app.control.inspect()
scheduled = i.scheduled()
if (scheduled):
     # do something

This code also runs from one of tasks, but I think it doesn't matter, I got same result from interactive python command line (with some exceptions, see below).

At the same time, celery -A <proj> inspect scheduled command never fails. Also, I noticed, that when called from interactive python command line for the first time, this command also never fails. Most of the successive i.scheduled() calls return nothing.

i.scheduled() guarantees result only when called for the first time?

If so, why and how then can I inspect scheduled tasks from task? Run dedicated worker and restart it after every task? Seems like overkill for such trivial task.

Please explain, how to use this feature the right way.

Upvotes: 4

Views: 1688

Answers (1)

daniula
daniula

Reputation: 7028

This is caused by some weird issue inside Celery app. To repeat methods from Inspect object you have to create new Celery app instance object.

Here is small snippet, which can help you:

from celery import Celery

def inspect(method):
    app = Celery('app', broker='amqp://')
    return getattr(app.control.inspect(), method)()

print inspect('scheduled')
print inspect('active')

Upvotes: 3

Related Questions