From be652dfaddc1d2a7ed3992c92f01cde84a47f709 Mon Sep 17 00:00:00 2001 From: Snowykami Date: Thu, 17 Apr 2025 21:27:42 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=BB=E5=8A=A1=E5=A4=84?= =?UTF-8?q?=E7=90=86=E9=80=BB=E8=BE=91=EF=BC=8C=E4=BF=AE=E6=AD=A3=E4=BB=BB?= =?UTF-8?q?=E5=8A=A1=E5=87=BD=E6=95=B0=E4=BB=A5=E7=A1=AE=E4=BF=9D=E6=AD=A3?= =?UTF-8?q?=E7=A1=AE=E6=89=A7=E8=A1=8C=20docker=5Fpull=E3=80=81docker=5Fta?= =?UTF-8?q?g=20=E5=92=8C=20docker=5Fpush=20=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sync.py | 41 ++++++++++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/sync.py b/sync.py index a5baf34..09d8c89 100644 --- a/sync.py +++ b/sync.py @@ -62,25 +62,44 @@ async def main(): for image in config.images: if len(image.tags) > 0: for tag in image.tags: - async def task() -> int | None: - return (await docker_pull(f"{image.source}:{tag}")) \ - or (await docker_pull(f"{image.source}:{tag}")) \ - or (await docker_push(f"{image.target}:{tag}")) + # 定义任务函数 + async def task(): + if r := await docker_pull(f"{image.source}:{tag}"): + if r != 0: + return r + await docker_tag(f"{image.source}:{tag}", f"{image.target}:{tag}") + if r := await docker_push(f"{image.target}:{tag}"): + if r != 0: + return r + await docker_push(f"{image.target}:{tag}") + if r := await docker_push(image.target): + if r != 0: + return r + return 0 + + # 将协程对象添加到任务列表 tasks.append(limited_task(task)) else: - async def task() -> int | None: - return (await docker_pull(image.source)) \ - or (await docker_pull(image.source)) \ - or (await docker_push(image.target)) + async def task(): + if r := await docker_pull(image.source): + if r != 0: + return r + if r:= await docker_tag(image.source, image.target): + if r != 0: + return r + if r := await docker_push(image.target): + if r != 0: + return r + return 0 tasks.append(limited_task(task)) - + results = await asyncio.gather(*tasks) - print(tasks) failed_tasks = 0 for result in results: if result is not None and result != 0: failed_tasks += 1 - + if failed_tasks > 0: + raise Exception(f"{failed_tasks} tasks failed.") print(f"{len(results)} tasks completed. {len(results) - failed_tasks} succeed, {failed_tasks} failed.") if __name__ == "__main__":