Skip to content

Commit

Permalink
Add logging for whole chain
Browse files Browse the repository at this point in the history
  • Loading branch information
mjaehn committed Dec 20, 2023
1 parent 6dd552a commit 7ec7bc1
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
47 changes: 47 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ def __init__(self, casename):
# User-defined attributes from config file
self.load_config_file(casename)

# Set case root
self.case_root = self.work_root / self.casename

# Set workflow and async attributes and initiate job ids dict
self.set_workflow()

Expand Down Expand Up @@ -324,6 +327,50 @@ def create_vars_from_dicts(self, dct=None, key=None):
else:
setattr(self, subkey, v)

def log_job_status(self, job, status, launch_time, duration=None):
log_file = self.case_root / "chain_status.log"

# Check if the header exists, if not, create it
if not log_file.is_file():
header = "Name ID Status Time Duration\n"
with open(log_file, 'w') as f:
f.write(header)

# Log the job information
if job == 'chain':
if duration is not None:
duration = self.format_duration(duration)
job_id = ''
else:
job_id = self.job_id

launch_time = launch_time.strftime("%a %b %d %H:%M:%S %Z %Y")
if status == 'FINISH':
log_entry = f"{job:<15} {job_id:<21} {status:<6} {launch_time:<28} {duration}\n"
else:
log_entry = f"{job:<15} {job_id:<21} {status:<6} {launch_time:<28}\n"

with open(log_file, 'a') as f:
f.write(log_entry)

def format_duration(self, duration):
"""
Format a duration represented by a datetime.timedelta object into a human-readable string.
Parameters:
- duration (datetime.timedelta): The duration to be formatted.
Returns:
- str: A string representing the formatted duration in the "0d 0h 0m 0s" format.
"""
seconds = duration.total_seconds()
days, remainder = divmod(seconds, 86400)
hours, remainder = divmod(remainder, 3600)
minutes, seconds = divmod(remainder, 60)

formatted_duration = f"{int(days)}d {int(hours)}h {int(minutes)}m {int(seconds)}s"
return formatted_duration

def get_dep_ids(self, job_name, add_dep=None):
"""Get dependency job ids for `job_name`"""

Expand Down
7 changes: 7 additions & 0 deletions run_chain.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,10 @@ def main():
f"Starting chain for case {casename} and workflow {cfg.workflow_name}"
)

launch_time = datetime.now()
tools.create_dir(cfg.case_root, "case_root")
cfg.log_job_status('chain', 'START', launch_time)

# Check for restart compatibility and spinup
if 'restart' in cfg.workflow['features']:
if hasattr(cfg, 'spinup'):
Expand All @@ -448,6 +452,9 @@ def main():
cfg.enddate_sim = cfg.enddate
run_chunk(cfg=cfg, force=args.force, resume=args.resume)

end_time = datetime.now()
duration_seconds = (end_time - launch_time).total_seconds()
cfg.log_job_status('chain', 'START', launch_time, duration_seconds)
print('>>> Finished the processing chain successfully <<<')


Expand Down

0 comments on commit 7ec7bc1

Please sign in to comment.