statemachines.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. # __package__ = 'archivebox.filestore'
  2. # import time
  3. # import os
  4. # from datetime import timedelta
  5. # from typing import ClassVar
  6. # from django.utils import timezone
  7. # from rich import print
  8. # from statemachine import State, StateMachine
  9. # from workers.actor import ActorType
  10. # from .models import File
  11. # class FileMachine(StateMachine, strict_states=True):
  12. # """
  13. # State machine for managing File lifecycle.
  14. # https://github.com/ArchiveBox/ArchiveBox/wiki/ArchiveBox-Architecture-Diagrams
  15. # """
  16. # model: File
  17. # MAX_LOCK_TIME: ClassVar[int] = 600
  18. # # States
  19. # unlocked = State(value=File.StatusChoices.UNLOCKED, initial=True)
  20. # locked = State(value=File.StatusChoices.LOCKED)
  21. # # Transition Events
  22. # lock = unlocked.to(locked, cond='can_lock')
  23. # unlock = locked.to(unlocked)
  24. # def __init__(self, file, *args, **kwargs):
  25. # self.file = file
  26. # super().__init__(file, *args, **kwargs)
  27. # def __repr__(self) -> str:
  28. # return f'[grey53]File\\[{self.file.ABID}] 🏃‍♂️ Worker\\[pid={os.getpid()}].tick()[/grey53] [blue]{self.file.status.upper()}[/blue] ⚙️ [grey37]Machine[/grey37]'
  29. # def __str__(self) -> str:
  30. # return self.__repr__()
  31. # @locked.enter
  32. # def enter_locked(self):
  33. # print(f'{self}.on_locked() ↳ file.locked_at = now()')
  34. # self.file.lock_file(seconds=self.MAX_LOCK_TIME)
  35. # def can_lock(self) -> bool:
  36. # return self.file.status == File.StatusChoices.UNLOCKED
  37. # class FileWorker(ActorType[File]):
  38. # Model = File
  39. # StateMachineClass = FileMachine
  40. # ACTIVE_STATE: ClassVar[State] = FileMachine.locked
  41. # MAX_CONCURRENT_ACTORS: ClassVar[int] = 4
  42. # MAX_TICK_TIME: ClassVar[int] = 600
  43. # CLAIM_FROM_TOP_N: ClassVar[int] = MAX_CONCURRENT_ACTORS * 10