def check_status(self): try: response = flower_get('metrics', timeout=2) if not response.ok: raise ServiceUnavailable('Flower Unavailable') except Exception as ex: raise ServiceUnavailable(ex.args)
def test_flower_get(self, http_get_mock): http_get_mock.return_value = 'foo-task-response' self.assertEqual(flower_get('some-url'), 'foo-task-response') http_get_mock.assert_called_once_with('http://flower:5555/some-url', auth=HTTPBasicAuth( settings.FLOWER_USER, settings.FLOWER_PASSWORD))
def get(self, request, import_queue=None): # pylint: disable=too-many-return-statements task_id = request.GET.get('task') result_format = request.GET.get('result') username = request.GET.get('username') user = self.request.user if task_id: parsed_task = parse_bulk_import_task_id(task_id) username = parsed_task['username'] if not user.is_staff and user.username != username: return Response(status=status.HTTP_403_FORBIDDEN) task = AsyncResult(task_id) if task.successful(): result = task.get() if result_format == 'json': response = Response(result.json, content_type="application/json") response['Content-Encoding'] = 'gzip' return response if result_format == 'report': return Response(result.report) return Response(result.detailed_summary) if task.failed(): return Response(dict(exception=str(task.result)), status=status.HTTP_400_BAD_REQUEST) if task.state == 'PENDING' and not task_exists(task_id): return Response(dict(exception='task ' + task_id + ' not found'), status=status.HTTP_404_NOT_FOUND) return Response(dict(task=task.id, state=task.state, username=username, queue=parsed_task['queue']), status=status.HTTP_202_ACCEPTED) flower_tasks = flower_get('api/tasks').json() tasks = [] for task_id, value in flower_tasks.items(): if not value['name'].startswith('tasks.bulk_import'): continue task = parse_bulk_import_task_id(task_id) if user.is_staff or user.username == task['username']: if (not import_queue or task['queue'] == import_queue) and \ (not username or task['username'] == username): tasks.append( dict(task=task_id, state=value['state'], queue=task['queue'], username=task['username'])) return Response(tasks)
def get(self, request, import_queue=None): # pylint: disable=too-many-return-statements,too-many-locals,too-many-branches task_id = request.GET.get('task') result_format = request.GET.get('result') username = request.GET.get('username') user = self.request.user if task_id: parsed_task = parse_bulk_import_task_id(task_id) username = parsed_task['username'] if not user.is_staff and user.username != username: return Response(status=status.HTTP_403_FORBIDDEN) task = AsyncResult(task_id) if task.successful(): result = task.get() if result and result_format == 'json': return Response(result.get('json', None), content_type="application/json") if result and result_format == 'report': return Response(result.get('report', None)) if result: return Response(result.get('detailed_summary', None)) if task.failed(): return Response(dict(exception=str(task.result)), status=status.HTTP_400_BAD_REQUEST) if task.state == 'STARTED': service = RedisService() if service.exists(task_id): return Response(dict( details=service.get_formatted(task_id), task=task.id, state=task.state, username=username, queue=parsed_task['queue']), status=status.HTTP_200_OK) if task.state == 'PENDING' and not task_exists(task_id): return Response(dict(exception='task ' + task_id + ' not found'), status=status.HTTP_404_NOT_FOUND) return Response(dict(task=task.id, state=task.state, username=username, queue=parsed_task['queue']), status=status.HTTP_202_ACCEPTED) try: response = flower_get('api/tasks') flower_tasks = response.json() except Exception as ex: return Response(dict( detail= 'Flower service returned unexpected result. Maybe check healthcheck.', exception=str(ex)), status=status.HTTP_422_UNPROCESSABLE_ENTITY) tasks = [] for task_id, value in flower_tasks.items(): if not value.get('name', None) or not value['name'].startswith( 'core.common.tasks.bulk_import'): continue task = parse_bulk_import_task_id(task_id) if user.is_staff or user.username == task['username']: if (not import_queue or task['queue'] == import_queue) and \ (not username or task['username'] == username): tasks.append( dict(task=task_id, state=value['state'], queue=task['queue'], username=task['username'])) return Response(tasks)