summaryrefslogtreecommitdiff
path: root/modules
diff options
context:
space:
mode:
authorJon Bergli Heier <snakebite@jvnv.net>2011-12-15 21:16:05 +0100
committerJon Bergli Heier <snakebite@jvnv.net>2011-12-15 21:16:05 +0100
commitbdb00c28a02f844c6832d5438bc60328dd47aced (patch)
treed823482a467078615cbffd068c84f12bc33f1e54 /modules
parenta125494316f52e8371645d14c92e274ab007868b (diff)
tracking: Added timeout with error handling.
Diffstat (limited to 'modules')
-rw-r--r--modules/tracking.py29
1 files changed, 18 insertions, 11 deletions
diff --git a/modules/tracking.py b/modules/tracking.py
index 0f3ae22..548faa2 100644
--- a/modules/tracking.py
+++ b/modules/tracking.py
@@ -55,7 +55,8 @@ class Package(Base):
self.consignment_id = consignment_id
self.code = code
-class NoPackageFound(Exception): pass
+class PackageError(Exception): pass
+class NoPackageFound(PackageError): pass
class TrackingResult:
def __init__(self, code, date, desc, delivered = False):
@@ -80,9 +81,9 @@ class PostenModule(TrackingModule):
def get_xml(self, url):
try:
- u = urllib2.urlopen(url)
- except:
- return
+ u = urllib2.urlopen(url, timeout = config.getfloat('module/tracking', 'timeout'))
+ except Exception as e:
+ raise PackageError(str(e))
if u.headers['content-type'].startswith('application/xml'):
xml = etree.parse(u)
else:
@@ -215,12 +216,16 @@ class Module:
consignments = consignments.filter(Consignment.nick == nick)
results = []
for row in consignments:
- self.track_update(row.code)
- i = 0
if row.label:
label = ' (%s)' % row.label
else:
label = ''
+ try:
+ self.track_update(row.code, propagate_error = True)
+ except PackageError as e:
+ results.append('Failed to fetch data for \002%s\002%s.' % (row.code.decode('utf-8'), label))
+ continue
+ i = 0
for package in row.packages:
i += 1
@@ -251,7 +256,7 @@ class Module:
msg = 'Failed to fetch tracking data for \002%s\002.' % code.decode('utf8')
else:
msg = 'No tracking number given or registered.'
- except NoPackageFound as e:
+ except PackageError as e:
msg = str(e)
finally:
session.close()
@@ -343,16 +348,16 @@ class Module:
self.irc.msg(channel if not channel == self.irc.nickname else nick.split('!')[0], 'No data returned (this is a bug).')
# called by start and status
- def track_update(self, code, announce = False):
+ def track_update(self, code, announce = False, propagate_error = False):
try:
session = Session()
consignment = session.query(Consignment).filter(Consignment.code == code).one()
- self.update_consignment(session, consignment, announce)
+ self.update_consignment(session, consignment, announce, propagate_error)
session.commit()
finally:
session.close()
- def update_consignment(self, session, consignment, announce = True):
+ def update_consignment(self, session, consignment, announce = True, propagate_error = False):
now = datetime.datetime.utcnow()
td = datetime.timedelta(seconds = config.getint(cfg_section, 'cache_time'))
# return if consignment was cached within cache_time seconds
@@ -366,7 +371,9 @@ class Module:
tm = tracking_modules[consignment.type]()
try:
package_results = tm.track(consignment.code) or []
- except NoPackageFound as e:
+ except PackageError as e:
+ if propagate_error:
+ raise e
# ignore
return
for data in package_results: