Commit 8c4f3b88 authored by Mustafa Gezen's avatar Mustafa Gezen
Browse files

The whole loop should NOT be atomic, only single items

parent 110d5386
......@@ -21,6 +21,7 @@
import asyncio
import datetime
import xmlrpc
from typing import List
import koji
from tortoise.transactions import atomic
......@@ -35,78 +36,74 @@ from distrobuild_scheduler.sigul import sign_koji_package
@atomic()
async def atomic_sign_unsigned_builds():
builds = await Build.filter(signed=False, status=BuildStatus.SUCCEEDED).prefetch_related("package").all()
for build in builds:
if build.koji_id:
koji_session.packageListAdd(tags.compose(), build.package.name, "distrobuild")
should_tag = True
build_tasks = koji_session.listBuilds(taskID=build.koji_id)
for build_task in build_tasks:
build_history = koji_session.queryHistory(build=build_task["build_id"])
if "tag_listing" in build_history:
for tag in build_history["tag_listing"]:
if tag["tag.name"] == tags.compose():
should_tag = False
if should_tag:
koji_session.tagBuild(tags.compose(), build_task["nvr"])
build_rpms = koji_session.listBuildRPMs(build_task["build_id"])
for rpm in build_rpms:
rpm_sigs = koji_session.queryRPMSigs(rpm["id"])
for rpm_sig in rpm_sigs:
if rpm_sig["sigkey"] == settings.sigul_key_id:
continue
nvr_arch = "%s.%s" % (rpm["nvr"], rpm["arch"])
await sign_koji_package(nvr_arch)
koji_session.writeSignedRPM(nvr_arch, settings.sigul_key_id)
build.signed = True
await build.save()
async def atomic_sign_unsigned_builds(build: Build):
if build.koji_id:
koji_session.packageListAdd(tags.compose(), build.package.name, "distrobuild")
should_tag = True
build_tasks = koji_session.listBuilds(taskID=build.koji_id)
for build_task in build_tasks:
build_history = koji_session.queryHistory(build=build_task["build_id"])
if "tag_listing" in build_history:
for tag in build_history["tag_listing"]:
if tag["tag.name"] == tags.compose():
should_tag = False
if should_tag:
koji_session.tagBuild(tags.compose(), build_task["nvr"])
build_rpms = koji_session.listBuildRPMs(build_task["build_id"])
for rpm in build_rpms:
rpm_sigs = koji_session.queryRPMSigs(rpm["id"])
for rpm_sig in rpm_sigs:
if rpm_sig["sigkey"] == settings.sigul_key_id:
continue
nvr_arch = "%s.%s" % (rpm["nvr"], rpm["arch"])
await sign_koji_package(nvr_arch)
koji_session.writeSignedRPM(nvr_arch, settings.sigul_key_id)
build.signed = True
await build.save()
@atomic()
async def atomic_check_build_status():
builds = await Build.filter(status=BuildStatus.BUILDING).all()
for build in builds:
if build.koji_id:
task_info = koji_session.getTaskInfo(build.koji_id, request=True)
if task_info["state"] == koji.TASK_STATES["CLOSED"]:
build.status = BuildStatus.SUCCEEDED
await build.save()
async def atomic_check_build_status(build: Build):
if build.koji_id:
task_info = koji_session.getTaskInfo(build.koji_id, request=True)
if task_info["state"] == koji.TASK_STATES["CLOSED"]:
build.status = BuildStatus.SUCCEEDED
await build.save()
package = await Package.filter(id=build.package_id).get()
package.last_build = datetime.datetime.now()
await package.save()
elif task_info["state"] == koji.TASK_STATES["CANCELED"]:
package = await Package.filter(id=build.package_id).get()
package.last_build = datetime.datetime.now()
await package.save()
elif task_info["state"] == koji.TASK_STATES["CANCELED"]:
build.status = BuildStatus.CANCELLED
await build.save()
elif task_info["state"] == koji.TASK_STATES["FAILED"]:
try:
task_result = koji_session.getTaskResult(build.koji_id)
logger.debug(task_result)
except (koji.BuildError, xmlrpc.client.Fault):
build.status = BuildStatus.FAILED
except koji.GenericError:
build.status = BuildStatus.CANCELLED
finally:
await build.save()
elif task_info["state"] == koji.TASK_STATES["FAILED"]:
try:
task_result = koji_session.getTaskResult(build.koji_id)
logger.debug(task_result)
except (koji.BuildError, xmlrpc.client.Fault):
build.status = BuildStatus.FAILED
except koji.GenericError:
build.status = BuildStatus.CANCELLED
finally:
await build.save()
elif build.mbs_id:
build_info = await mbs_client.get_build(build.mbs_id)
state = build_info["state_name"]
if state == "ready":
build.status = BuildStatus.SUCCEEDED
await build.save()
elif build.mbs_id:
build_info = await mbs_client.get_build(build.mbs_id)
state = build_info["state_name"]
if state == "ready":
build.status = BuildStatus.SUCCEEDED
await build.save()
package = await Package.filter(id=build.package_id).get()
package.last_build = datetime.datetime.now()
await package.save()
elif state == "failed":
build.status = BuildStatus.FAILED
await build.save()
package = await Package.filter(id=build.package_id).get()
package.last_build = datetime.datetime.now()
await package.save()
elif state == "failed":
build.status = BuildStatus.FAILED
await build.save()
async def check_build_status():
......@@ -114,7 +111,9 @@ async def check_build_status():
logger.debug("[*] Running periodic task: check_build_status")
try:
await atomic_check_build_status()
builds = await Build.filter(status=BuildStatus.BUILDING).all()
for build in builds:
await atomic_check_build_status(build)
except Exception as e:
logger.error(e)
......@@ -128,7 +127,9 @@ async def sign_unsigned_builds():
logger.debug("[*] Running periodic task: sign_unsigned_builds")
try:
await atomic_sign_unsigned_builds()
builds = await Build.filter(signed=False, status=BuildStatus.SUCCEEDED).prefetch_related("package").all()
for build in builds:
await atomic_sign_unsigned_builds(build)
except Exception as e:
logger.error(e)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment