mirror of
https://github.com/ArchiveBox/ArchiveBox.git
synced 2025-05-13 14:44:29 -04:00

Some checks failed
Build Docker image / buildx (push) Has been cancelled
Build Homebrew package / build (push) Has been cancelled
Run linters / lint (push) Has been cancelled
Build Pip package / build (push) Has been cancelled
Run tests / python_tests (ubuntu-22.04, 3.11) (push) Has been cancelled
Run tests / docker_tests (push) Has been cancelled
Build Debian package / build (push) Has been cancelled
48 lines
1.3 KiB
Python
48 lines
1.3 KiB
Python
__package__ = 'archivebox.crawls'
|
|
|
|
from statemachine import State, StateMachine
|
|
|
|
from crawls.models import Crawl
|
|
|
|
# State Machine Definitions
|
|
#################################################
|
|
|
|
|
|
class CrawlMachine(StateMachine, strict_states=True):
|
|
"""State machine for managing Crawl lifecycle."""
|
|
|
|
model: Crawl
|
|
|
|
# States
|
|
queued = State(value=Crawl.CrawlStatus.QUEUED, initial=True)
|
|
started = State(value=Crawl.CrawlStatus.STARTED)
|
|
sealed = State(value=Crawl.CrawlStatus.SEALED, final=True)
|
|
|
|
# Tick Event
|
|
tick = (
|
|
queued.to.itself(unless='can_start', internal=True) |
|
|
queued.to(started, cond='can_start') |
|
|
started.to.itself(unless='is_finished', internal=True) |
|
|
started.to(sealed, cond='is_finished')
|
|
)
|
|
|
|
def __init__(self, crawl, *args, **kwargs):
|
|
self.crawl = crawl
|
|
super().__init__(crawl, *args, **kwargs)
|
|
|
|
def can_start(self) -> bool:
|
|
return self.crawl.seed and self.crawl.seed.uri
|
|
|
|
def is_finished(self) -> bool:
|
|
return not self.crawl.has_pending_archiveresults()
|
|
|
|
|
|
|
|
def on_started(self):
|
|
self.crawl.create_root_snapshot()
|
|
self.crawl.bump_retry_at(seconds=10)
|
|
self.crawl.save()
|
|
|
|
def on_sealed(self):
|
|
self.crawl.retry_at = None
|
|
self.crawl.save()
|