telegram_bots/feeds/tasks.py
2019-01-26 12:36:44 +03:00

37 lines
803 B
Python

from celery_once import QueueOnce
from django.utils import timezone
from telebot import TeleBot
from djconfig import config
from config.celery import app
from feeds.models import Feed
@app.task(base=QueueOnce)
def check_feeds():
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()
try:
feed = Feed.objects.get(pk=feed_pk)
if not feed.lock:
feed.lock = True
feed.save()
bot = TeleBot(config.feed_bot_token)
feed.last_id = feed.config.execute(bot, feed.chat_id, feed.last_id)
feed.last_check = timezone.now()
feed.save()
finally:
feed.lock = False
feed.save()