Skip to content

Commit

Permalink
Added app lifecycle hooks accessible via decorators.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Lee committed Nov 29, 2019
1 parent 2c0d406 commit 6ebd546
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 12 deletions.
10 changes: 5 additions & 5 deletions pyrunner/core/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,17 @@
from pathlib import Path
def main():
pr = PyRunner()
app = PyRunner()
# Assign default config and .lst file
pr.source_config_file(Path('{}/app_profile'.format(pr.config['config_dir'])))
pr.load_from_file(Path('{}/{}.lst'.format(pr.config['config_dir'], pr.config['app_name'])))
app.source_config_file(Path('{}/app_profile'.format(app.config['config_dir'])))
app.load_from_file(Path('{}/{}.lst'.format(app.config['config_dir'], app.config['app_name'])))
# Parse command line args
pr.parse_args()
app.parse_args()
# Initiate job
exit_status = pr.execute()
exit_status = app.execute()
# Ensure job exit status is passed to the caller
sys.exit(exit_status)
Expand Down
50 changes: 44 additions & 6 deletions pyrunner/core/pyrunner.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ def __init__(self, **kwargs):
'exec_to_id' : None
}

self._on_restart_func = None
self._on_success_func = None
self._on_fail_func = None
self._on_exit_func = None

# Config wiring
self.source_config_file = self.config.source_config_file

Expand Down Expand Up @@ -117,6 +122,15 @@ def plugin_notification(self, obj):
if not isinstance(obj, notification.Notification): raise Exception('Notification plugin must implement the Notification interface')
self.notification = obj

def on_restart(self, func):
self._on_restart_func = func
def on_success(self, func):
self._on_success_func = func
def on_fail(self, func):
self._on_fail_func = func
def on_exit(self, func):
self._on_exit_func = func

def execute(self):
return self.run()
def run(self):
Expand All @@ -126,6 +140,7 @@ def run(self):
# Initialize NodeRegister
if self._init_params['restart']:
if not self.load_last_failed():
self._init_params['restart'] = False
self.load_proc_list_file(self._init_params['proc_file'])
else:
self.load_proc_list_file(self._init_params['proc_file'])
Expand All @@ -142,24 +157,48 @@ def run(self):
if self._init_params['exec_to_id'] is not None:
self.exec_to(self._init_params['exec_to_id'])

# App lifecycle - RESTART
if self._init_params['restart'] and self._on_restart_func:
self._on_restart_func()

self.config['app_start_time'] = datetime.now().strftime('%Y-%m-%d %H:%M:%S')

# Prepare engine
self.engine.config = self.config
self.engine.register = self.register
self.engine.save_state_func = self.save_state

# Short circuit for a dryrun
if self.config['dryrun']:
self.print_documentation()
return 0

# Fire up engine
print('Executing PyRunner App: {}'.format(self.config['app_name']))
retcode = self.engine.initiate()

if retcode == 0 and not self.config['email_on_success']:
print('Skipping Email Notification: Property "email_on_success" is set to FALSE.')
elif retcode != 0 and not self.config['email_on_fail']:
print('Skipping Email Notification: Property "email_on_fail" is set to FALSE.')
emit_notification = True

# # App lifecycle - SUCCESS
if retcode == 0:
if self._on_success_func:
self._on_success_func()
if not self.config['email_on_succss']:
print('Skipping Email Notification: Property "email_on_success" is set to FALSE.')
emit_notification = False
# # App lifecycle - FAIL
else:
if self._on_fail_func:
self._on_fail_func()
if not self.config['email_on_fail']:
print('Skipping Email Notification: Property "email_on_fail" is set to FALSE.')
emit_notification = False

# App lifecycle - EXIT
if self._on_exit_func:
self._on_exit_func()

if emit_notification:
self.notification.emit_notification(self.config, self.register)

if not self.config['nozip']:
Expand Down Expand Up @@ -325,8 +364,7 @@ def parse_args(self):
elif opt in ['-n', '--max-procs']:
self.config['max_procs'] = int(arg)
elif opt in ['-r', '--restart']:
if os.path.isfile(self.config.ctllog_file):
self._init_params['restart'] = True
self._init_params['restart'] = True
elif opt in ['-x', '--exec-only']:
self._init_params['exec_only_list'] = [ int(id) for id in arg.split(',') ]
elif opt in ['-N', '--norun']:
Expand Down
2 changes: 1 addition & 1 deletion pyrunner/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '4.0.3'
__version__ = '4.1.0'

0 comments on commit 6ebd546

Please sign in to comment.