Making some refactors to make it easier to cancel the build at any time.

This commit is contained in:
Charlton Austin 2016-10-21 10:05:17 -04:00
parent 42ed8522fd
commit 1cde22e76c
3 changed files with 86 additions and 16 deletions

View file

@ -143,24 +143,47 @@ def get_pull_robot_name(trigger):
return trigger.pull_robot.username
def cancel_repository_build(build, work_queue):
def update_phase(build_uuid, phase):
""" A function to change the phase of a build """
with db_transaction():
try:
build = get_repository_build(build_uuid)
build.phase = phase
build.save()
return True
except InvalidRepositoryBuildException:
return False
def create_cancel_build_in_queue(build, build_queue):
""" A function to cancel a build before it leaves the queue """
def cancel_build():
if build.phase != BUILD_PHASE.WAITING or not build.queue_id:
return False
return build_queue.cancel(build.queue_id)
return cancel_build
def cancel_repository_build(build, build_queue):
""" This tries to cancel the build returns true if request is successful false if it can't be cancelled """
with db_transaction():
# Reload the build for update.
# We are loading the build for update so checks should be as quick as possible.
try:
build = db_for_update(RepositoryBuild.select().where(RepositoryBuild.id == build.id)).get()
except RepositoryBuild.DoesNotExist:
return False
if build.phase != BUILD_PHASE.WAITING or not build.queue_id:
return False
cancel_builds = [create_cancel_build_in_queue(build, build_queue), ]
for cancelled in cancel_builds:
if cancelled():
# Delete the build row.
build.delete_instance()
return True
# Try to cancel the queue item.
if not work_queue.cancel(build.queue_id):
return False
# Delete the build row.
build.delete_instance()
return True
return False
def get_archivable_build():