From d5ed75860508652a704f4e102c7a34dd1b1aac8e Mon Sep 17 00:00:00 2001 From: bakatrouble Date: Thu, 14 Mar 2019 09:53:06 +0300 Subject: [PATCH] fix --- feeds/models.py | 5 +++-- feeds/tasks.py | 11 +++++++---- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/feeds/models.py b/feeds/models.py index c798edb..311ea24 100644 --- a/feeds/models.py +++ b/feeds/models.py @@ -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}' diff --git a/feeds/tasks.py b/feeds/tasks.py index 5f2f569..671dc28 100644 --- a/feeds/tasks.py +++ b/feeds/tasks.py @@ -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