Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update code in write_total_stat_by_retries to use pathlib, close the file after writing in it #2061

Open
kinow opened this issue Jan 10, 2025 · 1 comment
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@kinow
Copy link
Member

kinow commented Jan 10, 2025

Sibling to #2060

See the function write_total_stat_by_retries as it is not closing the file it opens to write/append. The code can also be simplified with pathlib, and with some type hints.

Probably some unit tests/doctests will be necessary too.

def write_total_stat_by_retries(self, total_stats, first_retrial = False):
"""
Writes all data to TOTAL_STATS file
:param total_stats: data gathered by the wrapper
:type total_stats: dict
:param first_retrial: True if this is the first retry, False otherwise
:type first_retrial: bool
"""
path = os.path.join(self._tmp_path, self.name + '_TOTAL_STATS')
f = open(path, 'a')
if first_retrial:
f.write(" " + date2str(datetime.datetime.fromtimestamp(total_stats[0]), 'S') + ' ' + date2str(datetime.datetime.fromtimestamp(total_stats[1]), 'S') + ' ' + total_stats[2])
else:
f.write('\n' + date2str(datetime.datetime.fromtimestamp(total_stats[0]), 'S') + ' ' + date2str(datetime.datetime.fromtimestamp(total_stats[0]), 'S') + ' ' + date2str(datetime.datetime.fromtimestamp(total_stats[1]), 'S') + ' ' + total_stats[2])
out, err = self.local_logs
path_out = os.path.join(self._tmp_path, 'LOG_' + str(self.expid), out)
# Launch first as simple non-threaded function
exp_history = ExperimentHistory(self.expid, jobdata_dir_path=BasicConfig.JOBDATA_DIR, historiclog_dir_path=BasicConfig.HISTORICAL_LOG_DIR)
exp_history.write_start_time(self.name, start=total_stats[0], status=Status.VALUE_TO_KEY.get(self.status, "UNKNOWN"), qos=self.queue, job_id=self.id, wrapper_queue=self._wrapper_queue, wrapper_code=get_job_package_code(self.expid, self.name),
children=self.children_names_str)
if not first_retrial:
exp_history = ExperimentHistory(self.expid, jobdata_dir_path=BasicConfig.JOBDATA_DIR, historiclog_dir_path=BasicConfig.HISTORICAL_LOG_DIR)
exp_history.write_submit_time(self.name, submit=total_stats[0], status=Status.VALUE_TO_KEY.get(self.status, "UNKNOWN"), ncpus=self.processors,
wallclock=self.wallclock, qos=self.queue, date=self.date, member=self.member, section=self.section, chunk=self.chunk,
platform=self.platform_name, job_id=self.id, wrapper_queue=self._wrapper_queue, wrapper_code=get_job_package_code(self.expid, self.name),
children=self.children_names_str)
exp_history = ExperimentHistory(self.expid, jobdata_dir_path=BasicConfig.JOBDATA_DIR, historiclog_dir_path=BasicConfig.HISTORICAL_LOG_DIR)
job_data_dc = exp_history.write_finish_time(self.name, finish=total_stats[1], status=total_stats[2], job_id=self.id, out_file=out, err_file=err)
# Launch second as threaded function only for slurm
if job_data_dc and type(self.platform) is not str and self.platform.type == "slurm":
thread_write_finish = Thread(target=ExperimentHistory(self.expid, jobdata_dir_path=BasicConfig.JOBDATA_DIR, historiclog_dir_path=BasicConfig.HISTORICAL_LOG_DIR).write_platform_data_after_finish, args=(job_data_dc, self.platform))
thread_write_finish.name = "JOB_data_{}".format(self.name)
thread_write_finish.start()

@kinow kinow added enhancement New feature or request good first issue Good for newcomers labels Jan 10, 2025
@kinow kinow added this to the Autosubmit 4.1.12 milestone Jan 10, 2025
@kinow
Copy link
Member Author

kinow commented Jan 10, 2025

Huh, hold on. A better task to whoever works on this: Check if that function is actually used anywhere. I couldn't find any usages with the IDE 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

1 participant