feed thread loop.

This commit is contained in:
Jason Schwarzenberger 2020-11-10 10:09:48 +13:00
parent c1b7877f4b
commit 4ec50e20cb

View File

@ -28,9 +28,6 @@ from flask_cors import CORS
database.init() database.init()
search.init() search.init()
news_length = 0
news_index = 0
def new_id(): def new_id():
nid = gen_rand_id() nid = gen_rand_id()
while database.get_story(nid): while database.get_story(nid):
@ -145,13 +142,7 @@ def static_story(sid):
http_server = WSGIServer(('', 33842), flask_app) http_server = WSGIServer(('', 33842), flask_app)
def feed_thread(): def _add_new_refs():
global news_index, news_length
try:
while True:
# onboard new stories
if news_index == 0:
for ref, source in feed.get_list(): for ref, source in feed.get_list():
if database.get_story_by_ref(ref): if database.get_story_by_ref(ref):
continue continue
@ -162,19 +153,13 @@ def feed_thread():
except database.IntegrityError: except database.IntegrityError:
continue continue
ref_list = database.get_reflist() def _update_current_story(item):
news_length = len(ref_list)
# update current stories
if news_index < len(ref_list):
item = ref_list[news_index]
try: try:
story = database.get_story(item['sid']).data story = database.get_story(item['sid']).data
except AttributeError: except AttributeError:
story = dict(id=item['sid'], ref=item['ref'], source=item['source']) story = dict(id=item['sid'], ref=item['ref'], source=item['source'])
logging.info('Updating story: ' + str(story['ref']) + ', index: ' + str(news_index)) logging.info('Updating story: {}'.format(str(story['ref'])))
valid = feed.update_story(story) valid = feed.update_story(story)
if valid: if valid:
@ -183,14 +168,23 @@ def feed_thread():
else: else:
database.del_ref(item['ref']) database.del_ref(item['ref'])
logging.info('Removed ref {}'.format(item['ref'])) logging.info('Removed ref {}'.format(item['ref']))
else:
logging.info('Skipping index: ' + str(news_index)) def feed_thread():
ref_list = []
try:
while True:
# onboard new stories
if not len(ref_list):
_add_new_refs()
ref_list = database.get_reflist()
# update current stories
if len(ref_list):
item = ref_list.pop(0)
_update_current_story(item)
gevent.sleep(6) gevent.sleep(6)
news_index += 1
if news_index >= news_length: news_index = 0
except KeyboardInterrupt: except KeyboardInterrupt:
logging.info('Ending feed thread...') logging.info('Ending feed thread...')
except ValueError as e: except ValueError as e: