Background task that periodically saves cookies.
()
| 144 | |
| 145 | |
| 146 | async def _periodic_refresh_loop(): |
| 147 | """ |
| 148 | Background task that periodically saves cookies. |
| 149 | """ |
| 150 | global _last_refresh_time |
| 151 | |
| 152 | logger.info( |
| 153 | f"🔄 Periodic cookie refresh started " |
| 154 | f"(interval: {COOKIE_REFRESH_INTERVAL_SECONDS}s)" |
| 155 | ) |
| 156 | |
| 157 | # Initial delay before first save |
| 158 | await asyncio.sleep(COOKIE_REFRESH_INTERVAL_SECONDS) |
| 159 | |
| 160 | while True: |
| 161 | try: |
| 162 | elapsed = time.time() - _last_refresh_time |
| 163 | if elapsed >= COOKIE_REFRESH_INTERVAL_SECONDS: |
| 164 | logger.debug("Periodic cookie refresh triggered") |
| 165 | await save_current_cookies_to_profile() |
| 166 | |
| 167 | # Sleep until next check |
| 168 | await asyncio.sleep(min(60, COOKIE_REFRESH_INTERVAL_SECONDS // 2)) |
| 169 | |
| 170 | except asyncio.CancelledError: |
| 171 | logger.info("🔄 Periodic cookie refresh task cancelled") |
| 172 | raise |
| 173 | except Exception as e: |
| 174 | logger.error(f"Error in periodic cookie refresh: {e}") |
| 175 | # Continue running despite errors |
| 176 | await asyncio.sleep(60) |
| 177 | |
| 178 | |
| 179 | def start_periodic_refresh() -> Optional[asyncio.Task]: |
no test coverage detected