diff --git a/app/blueprints/api/tasks/handlers.py b/app/blueprints/api/tasks/handlers.py index d25215a..db823d5 100644 --- a/app/blueprints/api/tasks/handlers.py +++ b/app/blueprints/api/tasks/handlers.py @@ -6,16 +6,23 @@ def success_state(task_id): task.status = "SUCCESS" task.status_message = "задача выполнена успешно" + + db.session.commit() def failure_state(task_id, exception): task = Task.query.get(task_id) task.status = "FAILURE" task.status_message = f"произошла ошибка {str(exception)}" - + + db.session.commit() + def run_state(task_id, task_record_id): task = Task.query.get(task_record_id) task.task_id = task_id task.status = "RUNNING" - task.status_message = "задача запущена" \ No newline at end of file + task.status_message = "задача запущена" + + db.session.commit() + \ No newline at end of file diff --git a/app/blueprints/api/tasks/routes.py b/app/blueprints/api/tasks/routes.py index 8cfe384..df275fe 100644 --- a/app/blueprints/api/tasks/routes.py +++ b/app/blueprints/api/tasks/routes.py @@ -97,8 +97,10 @@ def start_task(task_id: int): add_to_group_task.delay(task_id=task_id) case 'parse': - parse_users_task.run(task_id=task_id) - delete_task(task_id) + try: + parse_users_task.run(task_id=task_id) + finally: + delete_task(task_id) return '', 204 diff --git a/app/blueprints/api/tasks/tasks.py b/app/blueprints/api/tasks/tasks.py index 27a2735..de533b1 100644 --- a/app/blueprints/api/tasks/tasks.py +++ b/app/blueprints/api/tasks/tasks.py @@ -44,9 +44,6 @@ def add_to_group_task(self, task_id: int): else: success_state(task_id) - - finally: - db.session.commit() @shared_task(bind=True) def parse_users_task(self, task_id: int): diff --git a/paper/parser.py b/paper/parser.py index 1eec34d..bc8b106 100644 --- a/paper/parser.py +++ b/paper/parser.py @@ -1,7 +1,7 @@ import asyncio from loguru import logger - +from app.extensions import db from paper.client import PaperClient from paper.errors import IgnoreException, NeedPasswordException, UserPrivacyException @@ -13,8 +13,6 @@ class PaperParser: self.client = PaperClient(session) - self.users_to_delete = [] - async def invite_users(self, users, group, task): await self.client.invite_self(group) @@ -34,6 +32,7 @@ class PaperParser: except (UserPrivacyException, IgnoreException): logger.warning(f"Exception occurred. Marking {user.username} for deletion...") user.delete = True + db.session.commit() finally: if not task.is_aborted(): @@ -56,8 +55,9 @@ class PaperParser: await self.client.send_message(user, message, file) except (UserPrivacyException, IgnoreException): - self.users_to_delete.append(user) - logger.warning("Exception occurred. Skipping user...") + logger.warning(f"Exception occurred. Marking {user.username} for deletion...") + user.delete = True + db.session.commit() finally: if not task.is_aborted():