Scrapy, a fast high-level web crawling & scraping framework for Python.
Support SQLite-backed queues (#7877)
Adrian40c5ad9389ca
diff --git a/docs/topics/security.rst b/docs/topics/security.rstindex bb89aef31..444800bec 100644--- a/docs/topics/security.rst+++ b/docs/topics/security.rst@@ -309,3 +309,31 @@ and a crawl that spans multiple domains can leak them to unintended hosts:default :setting:`REFERRER_POLICY` already avoids sending the referrer fromHTTPS to HTTP, but you can tighten it further (for example, to``same-origin`` or ``no-referrer``) if needed.++.. _security-job-state:++Job state integrity+===================++When you use :setting:`JOBDIR` to :ref:`pause and resume crawls+<topics-jobs>`, the default scheduler disk queues+(:setting:`SCHEDULER_DISK_QUEUE`, :setting:`SCHEDULER_START_DISK_QUEUE`) write+requests to a compact binary format that is only guaranteed to be complete+after a clean shutdown. If the process is killed, runs out of disk space, or+shares its job directory with a second process, those files can be left+truncated, and resuming the crawl then either fails with an unrecoverable+error or silently drops the pending requests.++To store the scheduler queues in an SQLite database instead, where each+request is written within its own transaction:++.. code-block:: python++ SCHEDULER_DISK_QUEUE = "scrapy.squeues.PickleLifoSQLiteQueue"+ SCHEDULER_START_DISK_QUEUE = "scrapy.squeues.PickleFifoSQLiteQueue"++* **Pro:** an interrupted write cannot corrupt the queues or discard pending+ requests unnoticed.++* **Con:** scheduling requests becomes slower, which can be noticeable in+ crawls with a high request throughput.diff --git a/docs/topics/settings.rst b/docs/topics/settings.rstindex aa4208607..862548153 100644--- a/docs/topics/settings.rst+++ b/docs/topics/settings.rst@@ -1956,10 +1956,21 @@ SCHEDULER_DISK_QUEUEDefault: ``'scrapy.squeues.PickleLifoDiskQueue'``+.. versionadded:: VERSION+ The ``SQLite`` queue types.+Type of disk queue that will be used by the scheduler. Other available typesare ``scrapy.squeues.PickleFifoDiskQueue``,``scrapy.squeues.MarshalFifoDiskQueue``,-``scrapy.squeues.MarshalLifoDiskQueue``.+``scrapy.squeues.MarshalLifoDiskQueue``,+``scrapy.squeues.PickleFifoSQLiteQueue``,+``scrapy.squeues.PickleLifoSQLiteQueue``,+``scrapy.squeues.MarshalFifoSQLiteQueue`` and+``scrapy.squeues.MarshalLifoSQLiteQueue``.++The ``SQLite`` types store requests in an SQLite database, which makes writes+slower but keeps the queue usable after an unclean shutdown. See+:ref:`security-job-state`... setting:: SCHEDULER_MEMORY_QUEUEdiff --git a/pyproject.toml b/pyproject.tomlindex e55149ecb..1380c07f5 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -298,21 +298,30 @@ filterwarnings = [# Twisted leaves the listening socket open after a failed bind# (CannotListenError): https://github.com/twisted/twisted/issues/6108# The asyncio reactor likewise leaves client sockets and transports open on- # teardown. These surface as unraisable exceptions during finalization; the- # message wording varies across Python implementations and versions- # (PyPy leaves the prefix empty), so match on the object repr.+ # teardown. These surface as unraisable exceptions during finalization when+ # collected outside any warnings-catching block, or as a plain+ # ResourceWarning, re-emitted by pytest.warns() with the outer filters+ # applied, when GC instead runs while a pytest.warns() block for an+ # unrelated warning is active, failing that unrelated test. The message+ # wording varies across Python implementations and versions (PyPy leaves+ # the prefix empty), so match on the object repr."ignore:.*<socket\\.socket fd=:pytest.PytestUnraisableExceptionWarning",+ "ignore:unclosed <socket\\.socket fd=:ResourceWarning","ignore:.*<function (_SelectorTransport|_ProactorBasePipeTransport|_ProactorSocketTransport|BaseEventLoop)\\.__del__:pytest.PytestUnraisableExceptionWarning",+ "ignore:unclosed transport <:ResourceWarning",# PyDispatcher iterates its connection registry while a weakref callback# may mutate it, which raises RuntimeError on PyPy, where several weakrefs# can die within a single collection."ignore:.*<function connect\\.<locals>\\.remove:pytest.PytestUnraisableExceptionWarning",# queuelib's test helpers (reused by tests/test_squeues.py) leave queue- # files open; the resulting warning surfaces at an arbitrary GC point, as an- # unraisable exception or as a plain ResourceWarning, which pytest.warns()- # re-emits when it does not match, failing an unrelated test.+ # files and SQLite connections open; see the comment above about the two+ # forms this can surface as. sqlite3's warning message has no path to+ # match on, so it cannot be scoped to queuelib's temporary directories the+ # way the file one is."ignore:.*<_io\\.\\w+ name=.*queuelib-tests-:pytest.PytestUnraisableExceptionWarning","ignore:unclosed file <_io\\.\\w+ name=.*queuelib-tests-:ResourceWarning",+ "ignore:.*<sqlite3\\.Connection object at:pytest.PytestUnraisableExceptionWarning",+ "ignore:unclosed database in <sqlite3\\.Connection object at:ResourceWarning",# itemadapter imports pydantic.v1, which warns on Python 3.14 and higher."ignore:Core Pydantic V1 functionality isn't compatible:UserWarning",# pyftpdlib imports asynchat, removed in Python 3.12, on lower versions.diff --git a/scrapy/squeues.py b/scrapy/squeues.pyindex e06e38e23..ed3287d1f 100644--- a/scrapy/squeues.py+++ b/scrapy/squeues.py@@ -166,11 +166,39 @@ _MarshalLifoSerializationDiskQueue = _serializable_queue(marshal.dumps,marshal.loads,)+_PickleFifoSerializationSQLiteQueue = _serializable_queue(+ _with_mkdir(queue.FifoSQLiteQueue), # type: ignore[arg-type]+ _pickle_serialize,+ pickle.loads,+)+_PickleLifoSerializationSQLiteQueue = _serializable_queue(+ _with_mkdir(queue.LifoSQLiteQueue), # type: ignore[arg-type]+ _pickle_serialize,+ pickle.loads,+)+_MarshalFifoSerializationSQLiteQueue = _serializable_queue(+ _with_mkdir(queue.FifoSQLiteQueue), # type: ignore[arg-type]+ marshal.dumps,+ marshal.loads,+)+_MarshalLifoSerializationSQLiteQueue = _serializable_queue(+ _with_mkdir(queue.LifoSQLiteQueue), # type: ignore[arg-type]+ marshal.dumps,+ marshal.loads,+)# public queue classesPickleFifoDiskQueue = _scrapy_serialization_queue(_PickleFifoSerializationDiskQueue)PickleLifoDiskQueue = _scrapy_serialization_queue(_PickleLifoSerializationDiskQueue)MarshalFifoDiskQueue = _scrapy_serialization_queue(_MarshalFifoSerializationDiskQueue)MarshalLifoDiskQueue = _scrapy_serialization_queue(_MarshalLifoSerializationDiskQueue)+PickleFifoSQLiteQueue = _scrapy_serialization_queue(_PickleFifoSerializationSQLiteQueue)+PickleLifoSQLiteQueue = _scrapy_serialization_queue(_PickleLifoSerializationSQLiteQueue)+MarshalFifoSQLiteQueue = _scrapy_serialization_queue(+ _MarshalFifoSerializationSQLiteQueue+)+MarshalLifoSQLiteQueue = _scrapy_serialization_queue(+ _MarshalLifoSerializationSQLiteQueue+)FifoMemoryQueue = _scrapy_non_serialization_queue(queue.FifoMemoryQueue) # type: ignore[arg-type]LifoMemoryQueue = _scrapy_non_serialization_queue(queue.LifoMemoryQueue) # type: ignore[arg-type]diff --git a/tests/test_squeues.py b/tests/test_squeues.pyindex 067343b67..c25321ff3 100644--- a/tests/test_squeues.py+++ b/tests/test_squeues.py@@ -11,18 +11,26 @@ from scrapy.loader import ItemLoaderfrom scrapy.selector import Selectorfrom scrapy.squeues import (_MarshalFifoSerializationDiskQueue,+ _MarshalFifoSerializationSQLiteQueue,_MarshalLifoSerializationDiskQueue,+ _MarshalLifoSerializationSQLiteQueue,_PickleFifoSerializationDiskQueue,+ _PickleFifoSerializationSQLiteQueue,_PickleLifoSerializationDiskQueue,+ _PickleLifoSerializationSQLiteQueue,_scrapy_non_serialization_queue,_serializable_queue,)-# The disk queue classes are built at run time, so they are untyped.+# The disk and SQLite queue classes are built at run time, so they are untyped._MarshalFifoQueue: Any = _MarshalFifoSerializationDiskQueue_MarshalLifoQueue: Any = _MarshalLifoSerializationDiskQueue_PickleFifoQueue: Any = _PickleFifoSerializationDiskQueue_PickleLifoQueue: Any = _PickleLifoSerializationDiskQueue+_MarshalFifoSQLiteQueue: Any = _MarshalFifoSerializationSQLiteQueue+_MarshalLifoSQLiteQueue: Any = _MarshalLifoSerializationSQLiteQueue+_PickleFifoSQLiteQueue: Any = _PickleFifoSerializationSQLiteQueue+_PickleLifoSQLiteQueue: Any = _PickleLifoSerializationSQLiteQueueclass MyItem(Item):@@ -76,7 +84,7 @@ def nonserializable_object_test(self):q.close()-class FifoDiskQueueTestMixin:+class FifoQueueTestMixin:def queue(self) -> Any:raise NotImplementedError@@ -93,7 +101,7 @@ class FifoDiskQueueTestMixin:test_nonserializable_object = nonserializable_object_test-class MarshalFifoDiskQueueTest(t.FifoDiskQueueTest, FifoDiskQueueTestMixin):+class MarshalFifoDiskQueueTest(t.FifoDiskQueueTest, FifoQueueTestMixin):chunksize = 100000def queue(self) -> Any:@@ -116,7 +124,7 @@ class ChunkSize4MarshalFifoDiskQueueTest(MarshalFifoDiskQueueTest):chunksize = 4-class PickleFifoDiskQueueTest(t.FifoDiskQueueTest, FifoDiskQueueTestMixin):+class PickleFifoDiskQueueTest(t.FifoDiskQueueTest, FifoQueueTestMixin):chunksize = 100000def queue(self) -> Any:@@ -188,7 +196,7 @@ class ChunkSize4PickleFifoDiskQueueTest(PickleFifoDiskQueueTest):chunksize = 4-class LifoDiskQueueTestMixin:+class LifoQueueTestMixin:def queue(self) -> Any:raise NotImplementedError@@ -205,12 +213,12 @@ class LifoDiskQueueTestMixin:test_nonserializable_object = nonserializable_object_test-class MarshalLifoDiskQueueTest(t.LifoDiskQueueTest, LifoDiskQueueTestMixin):+class MarshalLifoDiskQueueTest(t.LifoDiskQueueTest, LifoQueueTestMixin):def queue(self) -> Any:return _MarshalLifoQueue(self.qpath)-class PickleLifoDiskQueueTest(t.LifoDiskQueueTest, LifoDiskQueueTestMixin):+class PickleLifoDiskQueueTest(t.LifoDiskQueueTest, LifoQueueTestMixin):def queue(self) -> Any:return _PickleLifoQueue(self.qpath)@@ -243,3 +251,23 @@ class PickleLifoDiskQueueTest(t.LifoDiskQueueTest, LifoDiskQueueTestMixin):assert r.url == r2.urlassert r2.meta["request"] is r2q.close()+++class MarshalFifoSQLiteQueueTest(t.FifoSQLiteQueueTest, FifoQueueTestMixin):+ def queue(self) -> Any:+ return _MarshalFifoSQLiteQueue(self.qpath)+++class PickleFifoSQLiteQueueTest(t.FifoSQLiteQueueTest, FifoQueueTestMixin):+ def queue(self) -> Any:+ return _PickleFifoSQLiteQueue(self.qpath)+++class MarshalLifoSQLiteQueueTest(t.LifoSQLiteQueueTest, LifoQueueTestMixin):+ def queue(self) -> Any:+ return _MarshalLifoSQLiteQueue(self.qpath)+++class PickleLifoSQLiteQueueTest(t.LifoSQLiteQueueTest, LifoQueueTestMixin):+ def queue(self) -> Any:+ return _PickleLifoSQLiteQueue(self.qpath)diff --git a/tests/test_squeues_request.py b/tests/test_squeues_request.pyindex 105f3acef..d944ef097 100644--- a/tests/test_squeues_request.py+++ b/tests/test_squeues_request.py@@ -5,7 +5,7 @@ Queues that handle requestsfrom __future__ import annotationsfrom abc import ABC, abstractmethod-from typing import TYPE_CHECKING+from typing import TYPE_CHECKING, Anyimport pytest@@ -15,14 +15,20 @@ from scrapy.squeues import (FifoMemoryQueue,LifoMemoryQueue,MarshalFifoDiskQueue,+ MarshalFifoSQLiteQueue,MarshalLifoDiskQueue,+ MarshalLifoSQLiteQueue,PickleFifoDiskQueue,+ PickleFifoSQLiteQueue,PickleLifoDiskQueue,+ PickleLifoSQLiteQueue,)from scrapy.utils.misc import build_from_crawlerfrom scrapy.utils.test import get_crawlerif TYPE_CHECKING:+ from pathlib import Path+import queuelibfrom scrapy.crawler import Crawler@@ -136,6 +142,92 @@ class TestMarshalLifoDiskQueueRequest(TestRequestQueueBase):queue.close()+class TestPickleFifoSQLiteQueueRequest(TestRequestQueueBase):+ is_fifo = True++ @pytest.fixture+ def q(self, crawler, tmp_path):+ queue = build_from_crawler(+ PickleFifoSQLiteQueue, crawler, key=str(tmp_path / "pickle" / "fifo")+ )+ try:+ yield queue+ finally:+ queue.close()+++class TestPickleLifoSQLiteQueueRequest(TestRequestQueueBase):+ is_fifo = False++ @pytest.fixture+ def q(self, crawler, tmp_path):+ queue = build_from_crawler(+ PickleLifoSQLiteQueue, crawler, key=str(tmp_path / "pickle" / "lifo")+ )+ try:+ yield queue+ finally:+ queue.close()+++class TestMarshalFifoSQLiteQueueRequest(TestRequestQueueBase):+ is_fifo = True++ @pytest.fixture+ def q(self, crawler, tmp_path):+ queue = build_from_crawler(+ MarshalFifoSQLiteQueue, crawler, key=str(tmp_path / "marshal" / "fifo")+ )+ try:+ yield queue+ finally:+ queue.close()+++class TestMarshalLifoSQLiteQueueRequest(TestRequestQueueBase):+ is_fifo = False++ @pytest.fixture+ def q(self, crawler, tmp_path):+ queue = build_from_crawler(+ MarshalLifoSQLiteQueue, crawler, key=str(tmp_path / "marshal" / "lifo")+ )+ try:+ yield queue+ finally:+ queue.close()+++ "queue_cls",+ [+ PickleFifoSQLiteQueue,+ PickleLifoSQLiteQueue,+ MarshalFifoSQLiteQueue,+ MarshalLifoSQLiteQueue,+ ],+)+def test_sqlite_queue_survives_unclean_shutdown(+ crawler: Crawler, tmp_path: Path, queue_cls: Any+) -> None:+ key = str(tmp_path / "queue")+ queue = queue_cls.from_crawler(crawler=crawler, key=key)+ queue.push(Request("https://toscrape.com"))+ # No close(), as if the process had been killed. The connection is closed+ # directly instead, since on Windows the file cannot be removed later while+ # it is still open.+ queue._db.close()++ queue = queue_cls.from_crawler(crawler=crawler, key=key)+ try:+ assert len(queue) == 1+ request = queue.pop()+ assert request is not None+ assert request.url == "https://toscrape.com"+ finally:+ queue.close()++class TestFifoMemoryQueueRequest(TestRequestQueueBase):is_fifo = True