Hello
@task
def fancy_task():
fancy_task.update_state(state='CUSTOM_STATE1')
time.sleep(30)
when I call the above task, flower displays STARTED and then SUCCESS, CUSTOM_STATE1 is never shown while the task is waiting. Note that AsyncResult(task_id).state works as expected.
Flower gets state info from Celery events. update_state only changes results, it doesn't generate events.
Events and results are separate for a reason. The result backend is for programatic access, the events are for passive subscribers. The events are json only and does not contain any complex objects and task return values are truncated for efficiency and compatibility.
Results should not be used to monitor a cluster, that would be devastating for performance, and events must not be used programatically because they're ephemeral and may happen out of order.
If you want to send a monitoring event for your custom state then you must do so explicitly:
@app.task(bind=True)
def fancy_task(self):
self.update_state(state='CUSTOM_STATE')
with self.app.events.default_dispatcher() as dispatcher:
dispatcher.send('task-custom_state', field1='value1', field2='value2')
time.sleep(30)
So I see that this issue has been referenced by a commit, but can I get an example of what custom states looks like? So far, I have not been able to get custom states to show up in Flower or in the curses interface.
I can see send_event was added to the celery API, I just don't see any of these events appear in flower. Has this been implemented ?
Just an addendum, I did manage to use custom tasks events on flower, indeed I used send_event to communicate the state updates on my task. To be able to use the web-socket I had to add the event type to the EVENTS list at api/events.py and to the urls.py the respective path. I my case was created the task-running event so I could have progress bar updates for long running tasks. Worked fine. Thanks
@AndreLobato Maybe you should make it a pull request and convince whoever's in charge to accept it.
Having a progress bar for long running tasks will be great and pretty much what I'm trying to do as well.
I am sure lots of people would appreciate it.
Maybe we can find a way to make it more generic though. Maybe add events to listen to based on configuration instead of code ?
@daniel-cohen The progress state I did was inspired by the jobtastic module, I first implemented using jobstatic, but this module still requires celery 3.0 and depends on a django-celery result database scheme to store the progress data, which I tought was unecessary in my case since I was not using django. Also would require to use a database such as postgres and it's not so good for high availability. With the celery send-event task method is very trivial to add another state such as progress. Just as Ask mentioned above. A few more tweeks was needed on the templates so could parse the result when there is progress information or not. I may try to work in a clean way to be easy to implement the progress bar just for flower, that way any celery task that uses the send_event with the task-running or task-progress would reflect in a progress bar by flower on the result field while tasks run (not sure if would be accepted). I am also working on having the "Tasks" section of flower been updated by web sockets, so that could be used to real-time monitoring as well, with progress bar and sorteable columns as well, would be cool, but that would be another pull request.
Note that every Task instance has a send_event method which can be called instead of the dispatcher method as suggested above. This is much more elegant.
Most helpful comment
Note that every Task instance has a
send_eventmethod which can be called instead of the dispatcher method as suggested above. This is much more elegant.