(agent, target_time, is_duration_wait, log, get_heading_callback)
| 40 | |
| 41 | |
| 42 | async def managed_wait(agent, target_time, is_duration_wait, log, get_heading_callback): |
| 43 | |
| 44 | while Localization.get().now() < target_time: |
| 45 | before_intervention = Localization.get().now() |
| 46 | await agent.handle_intervention() |
| 47 | after_intervention = Localization.get().now() |
| 48 | |
| 49 | if is_duration_wait: |
| 50 | pause_duration = after_intervention - before_intervention |
| 51 | if pause_duration.total_seconds() > 1.5: # Adjust for pauses longer than the sleep cycle |
| 52 | target_time += pause_duration |
| 53 | PrintStyle.info( |
| 54 | f"Wait extended by {pause_duration.total_seconds():.1f}s to {Localization.get().serialize_datetime(target_time)}...", |
| 55 | ) |
| 56 | |
| 57 | current_time = Localization.get().now() |
| 58 | if current_time >= target_time: |
| 59 | break |
| 60 | |
| 61 | remaining_seconds = (target_time - current_time).total_seconds() |
| 62 | if log: |
| 63 | log.update(heading=get_heading_callback(format_remaining_time(remaining_seconds))) |
| 64 | sleep_duration = min(1.0, remaining_seconds) |
| 65 | |
| 66 | await asyncio.sleep(sleep_duration) |
| 67 | |
| 68 | return target_time |
no test coverage detected