def get_repository_query(repo_id): repository = Repository.get_by_id(repo_id) return { "id": repo_id, "name": repository.name, "mirror_url": repository.mirror_url, "mirror_zpool": repository.mirror_zpool, "mirror_location": repository.mirror_location, "mirror_type": repository.mirror_type, "mirror_args": repository.mirror_args, "user": repository.user.username, "mirror_init": repository.mirror_init, "schedule_status": repository.schedule_status, "schedule_run": repository.schedule_run, "schedule_number": repository.schedule_number, "schedule_minute": repository.schedule_minute, "schedule_hour": repository.schedule_hour, "schedule_day": repository.schedule_day, "schedule_month": repository.schedule_month, "schedule_year": repository.schedule_year, "created_at": repository.created_at, "updated_at": repository.updated_at, }
def __processing_task(self, task): try: self.logger.info(f'Thread: {self.tid}. Working on {task.id}') repo = Repository.get_by_id(task.id) # защита от двойного обновления (процесс обн. еще не закончился, а по расписанию наступил уже новый) already_in_queue = QueueTask.select().where( QueueTask.repository == repo) if already_in_queue: if already_in_queue.pid == os.getpid(): self.logger.warning( f'This repo is currently being updated') return self.logger.warning(f'Repo info is outdated. Refreshing...') already_in_queue.delete_instance() QueueTask.create(repository=repo, pid=os.getpid()) try: queries.task.write_task_status(repo=repo, msg="Updating repository") out = self.__subprocess_run( args=['update_repo.py', task.id], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, timeout=task.timeout, ) queries.task.write_task_status(repo=repo, msg=(out.returncode, out.stdout[-20000:])) self.logger.info( f'Thread: {self.tid}. Finished {task.id} ({out.returncode})' ) except subprocess.TimeoutExpired as e: queries.task.write_task_status(repo=repo, msg="Timeout exception") queries.task.write_task_status(repo=repo, msg=e.output[-20000:]) self.logger.warning( f'Thread: {self.tid}. Task {task.id} interrupted by timeout' ) finally: QueueTask.delete().where(QueueTask.repository == repo) except Exception as e: s = str(e) self.logger.warning( f'Thread: {self.tid}. Task {task.id} interrupted by exception: {s}' )
import sys from playhouse.db_url import connect import api_config from app.models import Repository from mirror.repository import LinuxRepoManager from queries.repository import init_mirror def run(repository: Repository): manager = LinuxRepoManager(repository) if not repository.mirror_init: if manager.filesystem_exist(): manager.delete() manager.create() init_mirror(repository) # штатный процесс обновления manager.update() if __name__ == '__main__': repo_id = sys.argv[1] # соединение с БД db = connect(api_config.DATABASE_URL, reuse_if_open=True) # привязываем модель ORM к БД db.bind([Repository]) repo = Repository.get_by_id(repo_id) run(repo) db.close()