Skip to content

Commit

Permalink
Save actor exit status
Browse files Browse the repository at this point in the history
Save the actor exit status as an audit event. Exit status 0 represents
successful execution or `StopActorExecutionError` (i.e. handled error)
and 1 represents unhandled expection.
  • Loading branch information
dkubek committed Jan 31, 2024
1 parent 132c0e3 commit 3e10406
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 23 deletions.
20 changes: 1 addition & 19 deletions leapp/actors/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,6 @@
from leapp.workflows.api import WorkflowAPI


class ActorExitStatus(object):
SUCCESS = 'success'
STOP_ACTOR_EXECUTION = 'stop_actor_execution'
STOP_ACTOR_EXECUTION_ERROR = 'stop_actor_execution_error'
REQUEST_STOP_AFTER_PHASE = 'request_stop_after_phase'

ALLOWED_VALUES = (SUCCESS, STOP_ACTOR_EXECUTION, STOP_ACTOR_EXECUTION_ERROR, REQUEST_STOP_AFTER_PHASE)

@classmethod
def validate(cls, value):
return value in cls.ALLOWED_VALUES


class Actor(object):
"""
The Actor class represents the smallest step in the workflow. It defines what kind
Expand Down Expand Up @@ -305,22 +292,17 @@ def run(self, *args):
""" Runs the actor calling the method :py:func:`process`. """
os.environ['LEAPP_CURRENT_ACTOR'] = self.name

exit_status = ActorExitStatus.SUCCESS
try:
self.process(*args)
except StopActorExecution:
exit_status = ActorExitStatus.STOP_ACTOR_EXECUTION
pass
except StopActorExecutionError as err:
self.report_error(err.message, err.severity, err.details)
exit_status = ActorExitStatus.STOP_ACTOR_EXECUTION_ERROR
except RequestStopAfterPhase:
self._messaging.request_stop_after_phase()
exit_status = ActorExitStatus.REQUEST_STOP_AFTER_PHASE
finally:
os.environ.pop('LEAPP_CURRENT_ACTOR', None)

return {'status': exit_status}

def process(self, *args, **kwargs):
""" Main processing method. In inherited actors, the function needs to be defined to be able to be processed."""
raise NotImplementedError()
Expand Down
10 changes: 6 additions & 4 deletions leapp/workflows/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,10 +363,8 @@ def run(self, context=None, until_phase=None, until_actor=None, skip_phases_unti
instance = actor(logger=current_logger, messaging=messaging,
config_model=config_model, skip_dialogs=skip_dialogs)

exit_status = None
try:
result = instance.run()
exit_status = result['status']
instance.run()
except BaseException as exc:
self._unhandled_exception = True
messaging.report_stacktrace(message=exc.message,
Expand All @@ -376,9 +374,13 @@ def run(self, context=None, until_phase=None, until_actor=None, skip_phases_unti
trace=exc.exception_info))
raise
finally:
# Set and unset the enviromental variable so that audit
# associates the entry with the correct data source
os.environ['LEAPP_CURRENT_ACTOR'] = actor.name
create_audit_entry(
event='actor-exit-status',
data={'exit_code': 1 if self._unhandled_exception else 0, 'exit_status': exit_status})
data={'exit_status': 1 if self._unhandled_exception else 0})
os.environ.pop('LEAPP_CURRENT_ACTOR')

self._stop_after_phase_requested = messaging.stop_after_phase or self._stop_after_phase_requested

Expand Down

0 comments on commit 3e10406

Please sign in to comment.