Skip to content

Commit

Permalink
added automatic retry to items download, when the request fails
Browse files Browse the repository at this point in the history
  • Loading branch information
pablohoffman committed Oct 24, 2012
1 parent 921f0ce commit f62cd26
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
6 changes: 6 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
python-scrapinghub release notes
================================

Version 1.1.1
-------------
(release date to be announced)

- added automatic retry to items download, when the request fails

Version 1.1
-----------
(release on Oct 19th, 2012)
Expand Down
23 changes: 21 additions & 2 deletions scrapinghub.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@

import os
import json
import logging
import time
import warnings


__all__ = ["APIError", "Connection"]
__version__ = '1.1'
__version__ = '1.1.1'

logger = logging.getLogger('scrapinghub')

class Connection(object):
"""Main class to access Scrapinghub API.
Expand Down Expand Up @@ -258,6 +261,10 @@ def _add_params(self, params):


class Job(RequestProxyMixin):

MAX_RETRIES = 180
RETRY_INTERVAL = 60

def __init__(self, project, id, info):
self.project = project
self._id = id
Expand All @@ -271,7 +278,19 @@ def __repr__(self):
return "Job({0.project!r}, {0.id})".format(self)

def items(self):
return self._get('items', 'jl')
offset = 0
for attempt in xrange(self.MAX_RETRIES):
try:
for item in self._get('items', 'jl', params={'offset': offset}):
yield item
time.sleep(1)
offset += 1
break
except Exception as exc:
msg = "Error reading items.jl (retrying in %ds): project=%s job=%s offset=%d attempt=%d/%d error=%s"
args = (self.RETRY_INTERVAL, self.project, self._id, offset, attempt, self.MAX_RETRIES, exc)
logger.error(msg, *args)
time.sleep(self.RETRY_INTERVAL)

def update(self, **modifiers):
# XXX: only allow add_tag/remove_tag
Expand Down

0 comments on commit f62cd26

Please sign in to comment.