This commit is contained in:
bakatrouble 2019-03-14 09:53:06 +03:00
parent add2c9597a
commit d5ed758605
2 changed files with 10 additions and 6 deletions

View File

@ -24,13 +24,14 @@ class Feed(models.Model):
def run_check(self):
if self.lock:
return
return False
if self.last_check and timezone.now() < self.last_check + self.check_interval:
return
return False
self.lock = True
self.save()
execute_feed.apply_async(args=(self.pk,), shadow=str(self))
return True
def __str__(self):
return f'#{self.pk} {self.title}'

View File

@ -1,4 +1,5 @@
import sentry_sdk
from celery.exceptions import Reject
from celery_once import QueueOnce
from django.utils import timezone
from telebot import TeleBot
@ -12,8 +13,11 @@ def check_feeds():
from feeds.models import Feed
feeds = Feed.objects.filter(lock=False)
enqueued = []
for feed in feeds:
feed.run_check()
if feed.run_check():
enqueued.append(str(feed))
return f'Following tasks were enqueued: {enqueued}'
@app.task()
@ -25,14 +29,12 @@ def execute_feed(feed_pk):
feed = None
try:
feed = Feed.objects.get(pk=feed_pk)
print(f'Processing feed "{feed}"')
if not feed.lock:
feed.lock = True
feed.save()
else:
print(f'Lock detected, aborting "{feed}"...')
return
raise Reject('Lock', requeue=False)
bot = TeleBot(config.feed_bot_token, threaded=False)
print(f'Last ID for "{feed}" = "{feed.last_id}"')
@ -46,6 +48,7 @@ def execute_feed(feed_pk):
feed.save()
except Exception as e:
sentry_sdk.capture_exception(e)
finally:
if feed:
feed.lock = False