diff --git a/app/blueprints/api/tasks/handlers.py b/app/blueprints/api/tasks/handlers.py index 4243fa9..d25215a 100644 --- a/app/blueprints/api/tasks/handlers.py +++ b/app/blueprints/api/tasks/handlers.py @@ -7,21 +7,15 @@ 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 = "задача запущена" - - db.session.commit() \ No newline at end of file + task.status_message = "задача запущена" \ No newline at end of file diff --git a/app/blueprints/api/tasks/tasks.py b/app/blueprints/api/tasks/tasks.py index 4940298..27a2735 100644 --- a/app/blueprints/api/tasks/tasks.py +++ b/app/blueprints/api/tasks/tasks.py @@ -3,6 +3,7 @@ import time from celery import shared_task import asyncio +from loguru import logger from telethon.tl.types import UserStatusOnline, UserStatusOffline, UserStatusRecently from app.models.user import User @@ -38,11 +39,14 @@ def add_to_group_task(self, task_id: int): ) except Exception as e: + logger.exception(e) failure_state(task_id, e) - raise e else: success_state(task_id) + + finally: + db.session.commit() @shared_task(bind=True) def parse_users_task(self, task_id: int): diff --git a/app/models/user.py b/app/models/user.py index ea9356b..85acfb9 100644 --- a/app/models/user.py +++ b/app/models/user.py @@ -1,4 +1,4 @@ -from sqlalchemy import Integer +from sqlalchemy import Integer, null from sqlalchemy.schema import ( Column, ForeignKey, @@ -15,6 +15,8 @@ class User(db.Model): phone: Mapped[str] = mapped_column(nullable=True) username: Mapped[str] = mapped_column(nullable=True) + delete: Mapped[bool] = mapped_column(default="false") + collection_id = Column(Integer, ForeignKey("collection.id")) collection: Mapped["Collection"] = relationship("Collection", back_populates="users") diff --git a/migrations/versions/65ccd5b1039b_delete_flag_for_users.py b/migrations/versions/65ccd5b1039b_delete_flag_for_users.py new file mode 100644 index 0000000..bc1ce96 --- /dev/null +++ b/migrations/versions/65ccd5b1039b_delete_flag_for_users.py @@ -0,0 +1,32 @@ +"""Delete flag for users + +Revision ID: 65ccd5b1039b +Revises: 11ed77b4dbe8 +Create Date: 2024-03-13 16:48:13.991684 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '65ccd5b1039b' +down_revision = '11ed77b4dbe8' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + with op.batch_alter_table('user', schema=None) as batch_op: + batch_op.add_column(sa.Column('delete', sa.Boolean(), nullable=False, server_default="false")) + + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + with op.batch_alter_table('user', schema=None) as batch_op: + batch_op.drop_column('delete') + + # ### end Alembic commands ### diff --git a/paper/parser.py b/paper/parser.py index 5f3589f..1eec34d 100644 --- a/paper/parser.py +++ b/paper/parser.py @@ -22,7 +22,7 @@ class PaperParser: if group_participants: participants_usernames = [participant.username for participant in group_participants if participant.username] - users = filter(lambda user: user.username not in participants_usernames, users) + users = filter(lambda user: user.username not in participants_usernames and not user.delete, users) for user in users: try: @@ -32,8 +32,8 @@ class PaperParser: await self.client.invite_user(user, group) 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 finally: if not task.is_aborted():