Skip to content

Commit

Permalink
[aws][fix] Improve error messages on exception (#1160)
Browse files Browse the repository at this point in the history
  • Loading branch information
aquamatthias committed Sep 9, 2022
1 parent f2bf577 commit 877bfaa
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions plugins/aws/resoto_plugin_aws/resource/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,10 @@ class AwsEc2VolumeType(AwsResource, BaseVolumeType):
T = TypeVar("T")


class CancelOnFirstError(Exception):
pass


@define
class ExecutorQueue:
executor: Executor
Expand All @@ -272,20 +276,23 @@ class ExecutorQueue:

def submit_work(self, fn: Callable[..., T], *args: Any, **kwargs: Any) -> Future[T]:
def do_work() -> T:
# in case we should fail on first future, let's fail fast and do not execute the function
# in case of exception let's fail fast and do not execute the function
if self._exception is None:
try:
return fn(*args, **kwargs)
except Exception as e:
# only store the first exception if we should fail on first future
if self.fail_on_first_exception:
if self._exception is None:
self._exception = e
raise e
else:
# rethrow the exception raised in another thread
raise self._exception
raise CancelOnFirstError(
"Exception happened in another thread. Do not start work."
) from self._exception

future = self.executor.submit(do_work)
future = (
self.executor.submit(do_work) if self.fail_on_first_exception else self.executor.submit(fn, *args, **kwargs)
)

with self._lock:
self.futures.append(future)
Expand All @@ -299,6 +306,8 @@ def wait_for_submitted_work(self) -> None:
for future in concurrent.futures.as_completed(to_wait):
try:
future.result()
except CancelOnFirstError:
pass
except Exception as ex:
log.exception(f"Unhandled exception in account {self.name}: {ex}")
raise
Expand Down

0 comments on commit 877bfaa

Please sign in to comment.