Skip to content

Commit

Permalink
Merge pull request #1649 from buildtesters/fix_raise_exception
Browse files Browse the repository at this point in the history
fix issues with exception
  • Loading branch information
shahzebsiddiqui authored Nov 1, 2023
2 parents f52fb6c + 808768e commit d08d446
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 38 deletions.
4 changes: 2 additions & 2 deletions buildtest/cli/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -1224,8 +1224,8 @@ def run_phase(self):
console.rule("[bold red]Running Tests")
try:
self.buildexecutor.run(self.builders)
except KeyboardInterrupt:
raise KeyboardInterrupt
except:
sys.exit()

builders = self.buildexecutor.get_validbuilders()
########## TEST SUMMARY ####################
Expand Down
58 changes: 29 additions & 29 deletions buildtest/executors/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import multiprocessing as mp
import os
import shutil
import sys
import time

from rich.table import Column, Table
Expand Down Expand Up @@ -124,12 +125,7 @@ def get(self, name):

def get_validbuilders(self):
"""Return a list of valid builders that were run"""
complete_builders = []
for builder in self.builders:
if builder.is_complete():
complete_builders.append(builder)

return complete_builders
return [builder for builder in self.builders if builder.is_complete()]

def _choose_executor(self, builder):
"""Choose executor is called at the onset of a run and poll stage. Given a builder
Expand Down Expand Up @@ -367,34 +363,18 @@ def run(self, builders):

if terminate:
break
except KeyboardInterrupt:
console.print("[red]Caught KeyboardInterrupt, terminating workers")

for builder in self.builders:
console.print(
f"[blue]{builder}[/blue]: [red]Removing test directory: {builder.test_root}"
)
try:
shutil.rmtree(builder.test_root)
except OSError as err:
console.print(
f"[blue]{builder}[/blue]: [red]Unable to delete test directory {builder.test_root} with error: {err.strerror}"
)
continue

if builder.is_batch_job():
console.print(
f"[blue]{builder}[/blue]: [red]Cancelling Job {builder.job.get()}"
)
builder.job.cancel()
except:
console.print("[red]Terminating workers due to exception")
self._cleanup_when_exception()

# close the worker pool by preventing any more tasks from being submitted
pool.close()

# terminate all worker processes
pool.join()

raise KeyboardInterrupt
sys.exit()

# close the worker pool by preventing any more tasks from being submitted
pool.close()

Expand Down Expand Up @@ -432,9 +412,9 @@ def poll(self, pending_jobs):
# need to remove builder from self._validbuilders when job is cancelled because these builders are ones
# self._validbuilders.remove(job)

self.print_job_details(jobs)
self._print_job_details(jobs)

def print_job_details(self, active_jobs):
def _print_job_details(self, active_jobs):
"""Print pending jobs in table format during each poll step
args:
Expand Down Expand Up @@ -504,3 +484,23 @@ def print_job_details(self, active_jobs):

if completed_jobs_table.row_count:
console.print(completed_jobs_table)

def _cleanup_when_exception(self):
"""This method is invoked by cleaning up any builders that are when exception is raised"""
for builder in self.builders:
console.print(
f"[blue]{builder}[/blue]: [red]Removing test directory: {builder.test_root}"
)
try:
shutil.rmtree(builder.test_root)
except OSError as err:
console.print(
f"[blue]{builder}[/blue]: [red]Unable to delete test directory {builder.test_root} with error: {err.strerror}"
)
continue

if builder.is_batch_job():
console.print(
f"[blue]{builder}[/blue]: [red]Cancelling Job {builder.job.get()}"
)
builder.job.cancel()
8 changes: 1 addition & 7 deletions buildtest/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,7 @@ def main():
max_jobs=args.max_jobs,
)
cmd.build()
except KeyboardInterrupt as err:
console.print(
"[red]Unable to complete buildtest build command, signal: KeyboardInterrupt detected"
)
console.print(err)
sys.exit(1)
except SystemExit as err:
except (KeyboardInterrupt, SystemExit) as err:
console.print("[red]buildtest build command failed")
console.print(err)
sys.exit(1)
Expand Down

0 comments on commit d08d446

Please sign in to comment.