posts devel

This commit is contained in:
2025-08-08 12:21:25 +09:00
parent 927da228c8
commit f1153a0bab
11 changed files with 309 additions and 0 deletions

27
posts/tasks.py Normal file
View File

@@ -0,0 +1,27 @@
# posts/tasks.py
from celery import shared_task
from django.utils import timezone
from .models import PostJob, Post
from .utils import render_post_text, build_keyboard, send_post_to_chat
@shared_task(bind=True, rate_limit="25/m") # мягкий лимит
def run_post_jobs(self):
jobs = (PostJob.objects
.select_related("post","chat")
.filter(status=Post.QUEUED, run_at__lte=timezone.now())[:50])
for job in jobs:
try:
text = render_post_text(job.post)
kb = build_keyboard(job.post.buttons)
send_post_to_chat(job.chat.chat_id, job.post, text, kb) # обёртка над PTB Bot
job.status = Post.SENT
job.save(update_fields=["status"])
except Exception as e:
job.attempts += 1
job.last_error = str(e)[:2000]
if job.attempts >= job.max_attempts:
job.status = Post.FAILED
else:
# экспоненциальный backoff
job.run_at = timezone.now() + timezone.timedelta(minutes=2 ** min(job.attempts,4))
job.save()