mirror of
https://github.com/samuelclay/NewsBlur.git
synced 2025-09-18 21:50:56 +00:00
Refactoring timelimit to add feed address re-check to a limited timeout.
This commit is contained in:
parent
c7d1410bb1
commit
7dee2103a9
3 changed files with 35 additions and 30 deletions
|
@ -18,6 +18,7 @@ from mongoengine.queryset import OperationError
|
|||
from utils import json
|
||||
from utils import feedfinder
|
||||
from utils.feed_functions import levenshtein_distance
|
||||
from utils.feed_functions import timelimit
|
||||
from utils.story_functions import format_story_link_date__short
|
||||
from utils.story_functions import format_story_link_date__long
|
||||
from utils.story_functions import pre_process_story
|
||||
|
@ -78,6 +79,7 @@ class Feed(models.Model):
|
|||
self.save_popular_authors(lock=lock)
|
||||
self.save_popular_tags(lock=lock)
|
||||
|
||||
@timelimit(20)
|
||||
def check_feed_address_for_feed_link(self):
|
||||
feed_address = None
|
||||
|
||||
|
|
|
@ -9,6 +9,7 @@ from utils import feedparser
|
|||
from django.db import IntegrityError
|
||||
from utils.story_functions import pre_process_story
|
||||
from utils import log as logging
|
||||
from utils.feed_functions import timelimit
|
||||
import sys
|
||||
import time
|
||||
import datetime
|
||||
|
@ -36,36 +37,6 @@ def mtime(ttime):
|
|||
"""
|
||||
return datetime.datetime.fromtimestamp(time.mktime(ttime))
|
||||
|
||||
import threading
|
||||
class TimeoutError(Exception): pass
|
||||
def timelimit(timeout):
|
||||
"""borrowed from web.py"""
|
||||
def _1(function):
|
||||
def _2(*args, **kw):
|
||||
class Dispatch(threading.Thread):
|
||||
def __init__(self):
|
||||
threading.Thread.__init__(self)
|
||||
self.result = None
|
||||
self.error = None
|
||||
|
||||
self.setDaemon(True)
|
||||
self.start()
|
||||
|
||||
def run(self):
|
||||
try:
|
||||
self.result = function(*args, **kw)
|
||||
except:
|
||||
self.error = sys.exc_info()
|
||||
|
||||
c = Dispatch()
|
||||
c.join(timeout)
|
||||
if c.isAlive():
|
||||
raise TimeoutError, 'took too long'
|
||||
if c.error:
|
||||
raise c.error[0], c.error[1]
|
||||
return c.result
|
||||
return _2
|
||||
return _1
|
||||
|
||||
class FetchFeed:
|
||||
def __init__(self, feed, options):
|
||||
|
|
|
@ -1,7 +1,39 @@
|
|||
import datetime
|
||||
import threading
|
||||
import sys
|
||||
from django.utils.translation import ungettext
|
||||
from utils import feedfinder
|
||||
|
||||
class TimeoutError(Exception): pass
|
||||
def timelimit(timeout):
|
||||
"""borrowed from web.py"""
|
||||
def _1(function):
|
||||
def _2(*args, **kw):
|
||||
class Dispatch(threading.Thread):
|
||||
def __init__(self):
|
||||
threading.Thread.__init__(self)
|
||||
self.result = None
|
||||
self.error = None
|
||||
|
||||
self.setDaemon(True)
|
||||
self.start()
|
||||
|
||||
def run(self):
|
||||
try:
|
||||
self.result = function(*args, **kw)
|
||||
except:
|
||||
self.error = sys.exc_info()
|
||||
|
||||
c = Dispatch()
|
||||
c.join(timeout)
|
||||
if c.isAlive():
|
||||
raise TimeoutError, 'took too long'
|
||||
if c.error:
|
||||
raise c.error[0], c.error[1]
|
||||
return c.result
|
||||
return _2
|
||||
return _1
|
||||
|
||||
def encode(tstr):
|
||||
""" Encodes a unicode string in utf-8
|
||||
"""
|
||||
|
|
Loading…
Add table
Reference in a new issue