Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Tried again to fix the update_feeds cron hanging, adding logging this…

… time.
  • Loading branch information...
commit b3e47209f26a2a61cfdff8c1fbab5d0597f77ea5 1 parent 10a0d12
@jacobian jacobian authored
Showing with 29 additions and 4 deletions.
  1. +29 −4 django_website/aggregator/management/commands/update_feeds.py
View
33 django_website/aggregator/management/commands/update_feeds.py
@@ -1,3 +1,4 @@
+import logging
import datetime
import feedparser
import optparse
@@ -27,6 +28,9 @@ class Command(BaseCommand):
)
def handle(self, *args, **kwargs):
+ log = self.setup_logging()
+ log.debug('Starting run.')
+
try:
lockfile = os.open(self.LOCKFILE, os.O_CREAT | os.O_EXCL)
except OSError:
@@ -39,13 +43,15 @@ def handle(self, *args, **kwargs):
verbose = True
try:
- socket.setdefaulttimeout(15)
self.update_feeds(verbose=verbose, num_threads=kwargs['threads'])
except:
- sys.exit(1)
+ log.exception('Uncaught exception updating feeds.')
finally:
+ log.debug('Cleaning up.')
os.close(lockfile)
os.unlink(self.LOCKFILE)
+
+ log.debug('Ending run.')
def update_feeds(self, verbose=False, num_threads=4):
feed_queue = Queue.Queue()
@@ -58,7 +64,17 @@ def update_feeds(self, verbose=False, num_threads=4):
[t.start() for t in threadpool]
[t.join() for t in threadpool]
-
+
+ def setup_logging(self):
+ log = logging.getLogger('django_website.update_feeds')
+ log.setLevel(logging.DEBUG)
+ handler = logging.FileHandler('/var/log/update_feeds.log')
+ handler.setLevel(logging.DEBUG)
+ formatter = logging.Formatter('%(asctime)s [%(levelname)s]: %(message)s')
+ handler.setFormatter(formatter)
+ log.addHandler(handler)
+ return log
+
class FeedUpdateWorker(threading.Thread):
def __init__(self, q, verbose, **kwargs):
@@ -66,6 +82,7 @@ def __init__(self, q, verbose, **kwargs):
self.daemon = True
self.verbose = verbose
self.q = q
+ self.log = logging.getLogger('django_website.update_feeds')
def run(self):
while 1:
@@ -77,10 +94,17 @@ def run(self):
self.q.task_done()
def update_feed(self, feed):
+ self.log.debug('Starting update: %s (%s)' % (feed, feed.feed_url))
if self.verbose:
print feed
- parsed_feed = feedparser.parse(feed.feed_url)
+ try:
+ socket.setdefaulttimeout(15)
+ parsed_feed = feedparser.parse(feed.feed_url)
+ except Exception:
+ self.log.exception('Error updating %s (%s)' % (feed, feed.feed_url))
+ return
+
for entry in parsed_feed.entries:
# Parse out the entry, handling all the fun stuff that feeds can do.
title = entry.title
@@ -118,3 +142,4 @@ def update_feed(self, feed):
summary = content,
date_modified = date_modified
)
+ self.log.debug('Done with %s (%s)' % (feed, feed.feed_url))
Please sign in to comment.
Something went wrong with that request. Please try again.