Example #1
0
    def get(self, request, project_id, task_id):
        """Returns the (possibly) new layer ID where the isochrone
        data has been added. If the task has not yet completed a status message is returned

        Note: `project_id` is only used for permissions checking!

        Returns 500 in case of exceptions
        Returns 404 in case of task not found
        Returns 200 ok for all other cases

        Response body:

        {
            "status": "complete",  // or "pending" or "error", full list at
                                   // https://huey.readthedocs.io/en/latest/signals.html#signals
            "exception": "Normally empty, error message in case of errors",
            "progress": [
                100,  // Progress %
            ],
            "task_result": {
                "qgis_layer_id": "4f2a88a1-ca93-4859-9de3-75d9728cde0e"
            }
        }

        TODO: move this into core, make it generic for task status and result reporting

        """

        try:

            # Try to retrieve the task result, may throw an exception
            try:
                result = HUEY.result(task_id)
                ret_status = status.HTTP_200_OK
            except TaskException:
                result = None
                ret_status = status.HTTP_500_INTERNAL_SERVER_ERROR

            task_model = TaskModel.objects.get(task_id=task_id)
            progress_info = task_model.progress_info

            try:
                progress_percentage = int(100 * progress_info[0] /
                                          task_model.total)
            except:
                progress_percentage = 0

            try:
                return Response(
                    {
                        'status': task_model.state.signal_name,
                        'exception': task_model.state.exception_line,
                        'progress': progress_percentage,
                        'task_result': result
                    },
                    status=ret_status)
            except:
                return Response(
                    {
                        'status': 'error',
                        'exception': 'Error retrieving task informations',
                        'progress': 0,
                        'task_result': result,
                    },
                    status=status.HTTP_500_INTERNAL_SERVER_ERROR)

        except TaskModel.DoesNotExist:

            # Handle pending
            pending_task_ids = [task.id for task in HUEY.pending()]

            if task_id in pending_task_ids:
                return Response({'result': True, 'status': 'pending'})

            return Response({
                'result': False,
                'error': _('Task not found!')
            },
                            status=status.HTTP_404_NOT_FOUND)