(task_id: str)
| 2102 | push = None |
| 2103 | if isinstance(result, dict) and result.get("push_id"): |
| 2104 | push = db_ops.get_push_papers(result["push_id"]) |
| 2105 | if push is None: |
| 2106 | push = db_ops.get_latest_push(user_id) |
| 2107 | push_payload = _push_payload(push) |
| 2108 | if push_payload is not None and isinstance(result, dict): |
| 2109 | metadata = dict(push_payload.get("metadata") or {}) |
| 2110 | for key in ( |
| 2111 | "paper_count", |
| 2112 | "total_fetched", |
| 2113 | "reason", |
| 2114 | "fallback_used", |
| 2115 | "fallback_days", |
| 2116 | "fallback_total_fetched", |
| 2117 | "fallback_filtered_already_handled", |
| 2118 | "fallback_kept_candidates", |
| 2119 | "fallback_relaxed", |
| 2120 | "fallback_source_scope", |
| 2121 | ): |
| 2122 | if key in result and key not in metadata: |
| 2123 | metadata[key] = result[key] |
| 2124 | push_payload["metadata"] = metadata |
| 2125 | return {"result": result, "push": push_payload} |
| 2126 | |
| 2127 | |
| 2128 | def _task_timestamp() -> str: |
| 2129 | return datetime.now().replace(microsecond=0).isoformat() |
| 2130 | |
| 2131 | |
| 2132 | # Finished tasks are kept around so the UI can still fetch the result after a |
| 2133 | # reload, but they were never removed: _DAILY_TASKS, _DAILY_TASK_BY_USER and |
| 2134 | # _READING_TASKS only ever grew. A long-lived GUI process accumulated every |
| 2135 | # task it had ever run, each holding a full push payload and preview items. |
| 2136 | _TASK_RETENTION_SECONDS = max(60, _env_int("PAPERFLOW_TASK_RETENTION_SECONDS", 3600, max_value=86400)) |
| 2137 | _TASK_RETENTION_MAX = max(8, _env_int("PAPERFLOW_TASK_RETENTION_MAX", 64, max_value=4096)) |
| 2138 | _TERMINAL_TASK_STATUSES = {"completed", "failed"} |
| 2139 | |
| 2140 | |
| 2141 | def _task_age_seconds(task: Dict[str, Any], now: datetime) -> float: |
| 2142 | """Seconds since a task reached a terminal state (0.0 if unknown).""" |
| 2143 | stamp = str(task.get("completed_at") or task.get("updated_at") or "") |
| 2144 | if not stamp: |
| 2145 | return 0.0 |
| 2146 | try: |
| 2147 | return max(0.0, (now - datetime.fromisoformat(stamp)).total_seconds()) |
| 2148 | except ValueError: |
| 2149 | return 0.0 |
| 2150 | |
| 2151 |
nothing calls this directly
no test coverage detected