Search code examples
celeryflower

Why flower's HTTP API get task info not work?


I have a working celery flower project. Now I want some celery task details using flower http api,but when I make a request at flower http api on http://localhost:5555/api/task/info/task_id it return 500 error?

Everything is all right in flower page.

enter image description here

While using requests to get http api, something error happened!

enter image description here

All of the steps are followed Flower's documentation. The Flower logging as below: enter image description here


Solution

  • For flower 0.9.2, it tries to put each attribute of task._fields into the response:

    def get(self, taskid):
        ...
        response = {}
        for name in task._fields:
            if name not in ['uuid', 'worker']:
                response[name] = getattr(task, name, None)
        response['task-id'] = task.uuid
        if task.worker is not None:
            response['worker'] = task.worker.hostname
        self.write(response)
    

    Turns out there are Task object in task._fields like parent, root:

    class Task(object):
        ...
        _fields = (
            ...
            'clock', 'client', 'root', 'root_id', 'parent', 'parent_id', 'children',
        )
    

    In celery, these fields have specific serialize handlers:

    self._serializer_handlers = {
        'children': self._serializable_children,
        'root': self._serializable_root,
        'parent': self._serializable_parent,
    }
    

    While in flower, it just pass it to self.write and it doesn't know how to serialize those objects.

    I think it has been fixed in flower 1.0.0:

    def get(self, taskid):
        ...
        response = task.as_dict()
        if task.worker is not None:
            response['worker'] = task.worker.hostname
    
        self.write(response)
    

    In as_dict function, the serialization is delegated to celery if available or use default keys:

    def as_dict(task):
        # as_dict is new in Celery 3.1.7
        if hasattr(Task, 'as_dict'):
            return task.as_dict()
        # old version
        else:
            return task.info(fields=task._defaults.keys())
    

    Version 1.0.0 is still in development but you can install it from git repo by running pip install git+https://github.com/mher/flower.git.