2024-01-31 19:37:01 +10:00
|
|
|
import time
|
|
|
|
|
|
|
|
from celery import shared_task
|
|
|
|
import asyncio
|
|
|
|
|
|
|
|
from paper.parser import PaperParser
|
|
|
|
from app.models.task import Task
|
|
|
|
from app.extensions import db
|
|
|
|
from .handlers import run_state, failure_state, success_state
|
|
|
|
|
|
|
|
async def add_to_group(session, task, task_self):
|
|
|
|
async with PaperParser(session.name) as parser:
|
|
|
|
await parser.invite_users(session.users, task.url, task_self)
|
|
|
|
|
|
|
|
async def sending_message(session, task, task_self):
|
|
|
|
async with PaperParser(session.name) as parser:
|
|
|
|
await parser.send_messages(session.users, task.message, task.file, task_self)
|
|
|
|
|
|
|
|
@shared_task(bind=True)
|
|
|
|
def add_to_group_task(self, task_id):
|
|
|
|
try:
|
|
|
|
task: Task = Task.query.get(task_id)
|
|
|
|
run_state(self.request.id, task_id)
|
|
|
|
|
|
|
|
session = task.session
|
|
|
|
|
|
|
|
time.sleep(10)
|
|
|
|
|
|
|
|
asyncio.run(
|
|
|
|
add_to_group(session, task, self)
|
|
|
|
)
|
|
|
|
|
|
|
|
except Exception as e:
|
2024-02-08 18:00:37 +10:00
|
|
|
failure_state(task_id, e)
|
2024-01-31 19:37:01 +10:00
|
|
|
raise e
|
|
|
|
|
|
|
|
else:
|
2024-02-08 18:00:37 +10:00
|
|
|
success_state(task_id)
|
2024-01-31 19:37:01 +10:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@shared_task(bind=True)
|
|
|
|
def send_messages_task(self, task_id):
|
|
|
|
task: Task = Task.query.get(task_id)
|
|
|
|
|
|
|
|
session = task.session
|
|
|
|
users = session.users
|