Skip to content

Commit 7dd6165

Browse files
authored
Merge pull request scrapy#174 from scrapinghub/fix-import-crash
fixing crash on settings import when kafka-python isn't installed
2 parents 62d9e49 + c674254 commit 7dd6165

File tree

4 files changed

+20
-5
lines changed

4 files changed

+20
-5
lines changed

docs/source/topics/frontera-settings.rst

+1-2
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,7 @@ KAFKA_CODEC_LEGACY
158158

159159
Default: ``KAFKA_CODEC_LEGACY``
160160

161-
Kafka-python 0.x version codec, could be one of ``CODEC_NONE``, ``CODEC_SNAPPY`` or ``CODEC_GZIP``,
162-
imported from ``kafka.protocol``.
161+
Kafka-python 0.x version compression codec to use, is a string and could be one of ``none``, ``snappy`` or ``gzip``.
163162

164163

165164
.. setting:: LOGGING_CONFIG

frontera/contrib/backends/remote/kafka.py

+4
Original file line numberDiff line numberDiff line change
@@ -224,3 +224,7 @@ def __init__(self, manager):
224224

225225
def get_next_requests(self, max_n_requests, **kwargs):
226226
return self._buffer.get_next_requests(max_n_requests, **kwargs)
227+
228+
229+
raise DeprecationWarning("KafkaBackend and KafkaOverusedBackend is deprecated, and will be removed soon please use "
230+
"MessageBusBackend instead")

frontera/contrib/messagebus/kafkabus.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,20 @@ def __init__(self, settings):
211211
self.spider_partition_id = settings.get('SPIDER_PARTITION_ID')
212212
self.max_next_requests = settings.MAX_NEXT_REQUESTS
213213
self.hostname_partitioning = settings.get('QUEUE_HOSTNAME_PARTITIONING')
214-
self.codec = settings.get('KAFKA_CODEC_LEGACY')
214+
215+
self.codec = None
216+
codec = settings.get('KAFKA_CODEC_LEGACY')
217+
if codec == 'none':
218+
from kafka.protocol import CODEC_NONE
219+
self.codec = CODEC_NONE
220+
if codec == 'snappy':
221+
from kafka.protocol import CODEC_SNAPPY
222+
self.codec = CODEC_SNAPPY
223+
if codec == 'gzip':
224+
from kafka.protocol import CODEC_GZIP
225+
self.codec = CODEC_GZIP
226+
if not self.codec:
227+
raise NameError("Non-existent Kafka compression codec.")
215228

216229
self.conn = KafkaClient(server)
217230

frontera/settings/default_settings.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from datetime import timedelta
2-
from kafka.protocol import CODEC_NONE
32

43

54
AUTO_START = True
@@ -19,7 +18,7 @@
1918
HBASE_STATE_CACHE_SIZE_LIMIT = 3000000
2019
HBASE_QUEUE_TABLE = 'queue'
2120
KAFKA_GET_TIMEOUT = 5.0
22-
KAFKA_CODEC_LEGACY = CODEC_NONE
21+
KAFKA_CODEC_LEGACY = "none"
2322
MAX_NEXT_REQUESTS = 64
2423
MAX_REQUESTS = 0
2524
MESSAGE_BUS = 'frontera.contrib.messagebus.zeromq.MessageBus'

0 commit comments

Comments
 (0)