(obj, with_result)
| 83 | |
| 84 | |
| 85 | def serialize_task(obj, with_result): |
| 86 | task_id = obj.id |
| 87 | status = obj.status |
| 88 | if with_result: |
| 89 | if status == TaskStatus.PENDING: |
| 90 | result = {"result": None} |
| 91 | elif status != TaskStatus.IN_PROGRESS or obj.is_all_task: |
| 92 | # set in cache |
| 93 | if not hasattr(obj, "result"): |
| 94 | result = {"result": TaskResults.get_all_task(task_id) if obj.is_all_task else TaskResults.get_task(task_id)} |
| 95 | else: |
| 96 | result = {"result": obj.result} |
| 97 | else: |
| 98 | result = {"result": None} |
| 99 | else: |
| 100 | result = {} |
| 101 | return { |
| 102 | "id": task_id, |
| 103 | "status": status, |
| 104 | "task_name": create_task_name(obj.task_name, task_id), |
| 105 | "scraper_name": obj.scraper_name, |
| 106 | "scraper_type": obj.scraper_type, |
| 107 | "is_all_task": obj.is_all_task, |
| 108 | "is_sync": obj.is_sync, |
| 109 | "parent_task_id": obj.parent_task_id, |
| 110 | "duration": calculate_duration(obj), |
| 111 | "started_at": isoformat(obj.started_at), |
| 112 | "finished_at": isoformat(obj.finished_at), |
| 113 | "data": obj.data, |
| 114 | "metadata": obj.meta_data, |
| 115 | **result, |
| 116 | "result_count": obj.result_count, |
| 117 | "created_at": isoformat(obj.created_at), |
| 118 | "updated_at": isoformat(obj.updated_at), |
| 119 | } |
| 120 | |
| 121 | |
| 122 | class Task(Base): |
no test coverage detected