implement cancellation handling in CaptureService and Database, update job states accordingly

This commit is contained in:
kstyagi23
2026-09-05 06:54:10 +05:30
parent 9c1a301c1a
commit cab7a68b8b
7 changed files with 293 additions and 38 deletions
+62 -2
View File
@@ -97,13 +97,73 @@ def test_worker_cannot_cancel_without_a_user_cancellation_request(tmp_path) -> N
assert running is not None
assert running["state"] == "running"
database.request_cancel(job["id"])
assert database.mark_cancelled(job["id"], "worker-a", "Cancelled by user")
cancellation = database.request_cancel(job["id"])
assert cancellation is not None
assert cancellation["state"] == "cancelled"
assert not database.mark_cancelled(job["id"], "worker-a", "Cancelled by user")
cancelled = database.get_job(job["id"])
assert cancelled is not None
assert cancelled["state"] == "cancelled"
def test_worker_stop_check_detects_a_cancellation_request(tmp_path) -> None:
database = Database(tmp_path / "siteharbor.sqlite3")
database.initialize()
job = database.create_job(
"https://example.com",
"https://example.com/",
capture_config(),
)
assert database.claim_next("worker-a", lease_seconds=30)
assert not database.worker_should_stop(job["id"], "worker-a")
assert database.request_cancel(job["id"])
assert database.worker_should_stop(job["id"], "worker-a")
def test_cancel_terminalizes_an_existing_cancellation_request(tmp_path) -> None:
database = Database(tmp_path / "siteharbor.sqlite3")
database.initialize()
job = database.create_job(
"https://example.com",
"https://example.com/",
capture_config(),
)
assert database.claim_next("worker-a", lease_seconds=30)
with database.connection() as connection:
connection.execute(
"UPDATE jobs SET cancel_requested = 1 WHERE id = ?",
(job["id"],),
)
cancelled = database.request_cancel(job["id"])
assert cancelled is not None
assert cancelled["state"] == "cancelled"
def test_initialize_recovers_a_legacy_cancellation_request(tmp_path) -> None:
database = Database(tmp_path / "siteharbor.sqlite3")
database.initialize()
job = database.create_job(
"https://example.com",
"https://example.com/",
capture_config(),
)
assert database.claim_next("worker-a", lease_seconds=30)
with database.connection() as connection:
connection.execute(
"UPDATE jobs SET cancel_requested = 1 WHERE id = ?",
(job["id"],),
)
database.initialize()
recovered = database.get_job(job["id"])
assert recovered is not None
assert recovered["state"] == "cancelled"
def test_expired_cancel_requested_job_is_terminalized_before_reclaim(tmp_path) -> None:
database = Database(tmp_path / "siteharbor.sqlite3")
database.initialize()