import sentry_sdk from celery_once import QueueOnce from django.utils import timezone from telebot import TeleBot from djconfig import config from config.celery import app @app.task(base=QueueOnce, once={'graceful': True}) def check_feeds(): from feeds.models import Feed feeds = Feed.objects.filter(lock=False) for feed in feeds: feed.run_check() @app.task() def execute_feed(feed_pk): from feeds.models import Feed config._reload_maybe() 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 bot = TeleBot(config.feed_bot_token, threaded=False) print(f'Last ID for "{feed}" = "{feed.last_id}"') for last_id in feed.config.execute(bot, feed.chat_id, feed.last_id): if last_id: feed.last_id = last_id feed.save() print(f'Saved last ID for "{feed}" = "{feed.last_id}"') bot.stop_bot() feed.last_check = timezone.now() feed.save() except Exception as e: sentry_sdk.capture_exception(e) finally: if feed: feed.lock = False feed.save()