time_guard.py 1.0 KB

1234567891011121314151617181920212223242526272829303132333435
  1. # Copyright Materialize, Inc. and contributors. All rights reserved.
  2. #
  3. # Use of this software is governed by the Business Source License
  4. # included in the LICENSE file at the root of this repository.
  5. #
  6. # As of the Change Date specified in that file, in accordance with
  7. # the Business Source License, use of this software will be governed
  8. # by the Apache License, Version 2.0.
  9. from datetime import datetime, timedelta
  10. class TimeGuard:
  11. def __init__(
  12. self,
  13. max_runtime_in_sec: int,
  14. ):
  15. self.max_runtime_in_sec = max_runtime_in_sec
  16. self.start_time = datetime.now()
  17. self.end_time: datetime | None = (
  18. self.start_time + timedelta(seconds=max_runtime_in_sec)
  19. if max_runtime_in_sec > 0
  20. else None
  21. )
  22. self.replied_abort_yes = False
  23. def shall_abort(self) -> bool:
  24. if self.end_time is None:
  25. return False
  26. if datetime.now() >= self.end_time:
  27. self.replied_abort_yes = True
  28. return True
  29. return False