Skip to content

Commit

Permalink
add crh_wait_to_finish.py
Browse files Browse the repository at this point in the history
  • Loading branch information
m-weigand committed May 17, 2022
1 parent 7ff18ed commit 86faa61
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 10 deletions.
7 changes: 5 additions & 2 deletions src/crh_add.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,7 @@ def _register_tomodir_for_processing(
engine.dispose()


def main():

def crh_add():
global_settings = get_config()
cmd_options = handle_cmd_options()

Expand All @@ -301,5 +300,9 @@ def main():
directory, 'inv', global_settings, cmd_options)


def main():
crh_add


if __name__ == '__main__':
main()
22 changes: 19 additions & 3 deletions src/crh_retrieve.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ def _is_finished(sim_id, conn):
Ignore any rows already locked by other processes (i.e., concurrent runs
of crh_retrieve)
Returns None if the given inversion is not finished or unavailable
"""
result = conn.execute(
' '.join((
Expand All @@ -59,7 +61,12 @@ def _is_finished(sim_id, conn):
def _check_and_retrieve(filename):
"""For a given .crh file, check if the inversion results are ready to be
downloaded and extract the results
Returns
-------
"""
status = False
logger.info('Checking: {}'.format(filename))
sim_settings = json.load(open(filename, 'r'))
# ignore any simulation not successfully uploade
Expand Down Expand Up @@ -112,11 +119,13 @@ def _check_and_retrieve(filename):
tar.extractall('.')
os.chdir(pwd)
mark_sim_as_downloaded(sim_settings['sim_id'], conn)
status = True
os.unlink(filename)
# IPython.embed()
transaction.commit()
conn.close()
engine.dispose()
return status


def mark_sim_as_downloaded(sim_id, conn):
Expand Down Expand Up @@ -146,14 +155,21 @@ def mark_sim_as_downloaded(sim_id, conn):
result.close()


def main():
def retrieve_all_finished_mods_and_invs():
unfinished = False
for root, dirs, files in os.walk('.'):
dirs.sort()
files.sort()
for filename in files:
if filename.endswith('.crh'):
_check_and_retrieve(root + os.sep + filename)
print(engine.pool.status())
status = _check_and_retrieve(root + os.sep + filename)
if not status:
unfinished = True
return unfinished


def main():
retrieve_all_finished_mods_and_invs()


if __name__ == '__main__':
Expand Down
29 changes: 29 additions & 0 deletions src/crh_wait_to_finish.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env python
import time

import logging

from crh_retrieve import retrieve_all_finished_mods_and_invs

logging.basicConfig(
level=logging.INFO,
format='{asctime} - {name} - %{levelname} - {message}',
style='{',
)
logger = logging.getLogger(__name__)


def crh_wait_for_all_sims_to_finish():
unfinished = True
while unfinished:
unfinished = retrieve_all_finished_mods_and_invs()
if unfinished:
time.sleep(10)


def main():
crh_wait_for_all_sims_to_finish()


if __name__ == '__main__':
main()
10 changes: 5 additions & 5 deletions tests/0_setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ cp -r "unfinished_inversions/20_157.142860" "${outdir}/"
# cp -r "unfinished_inversions/20_157.142860" "${outdir}/01"
# overwrite to make the inversion fail with an error.dat file
# echo 0 > "${outdir}/01/grid/elem.dat"
# cp -r "unfinished_inversions/20_157.142860" "${outdir}/02"
# cp -r "unfinished_inversions/20_157.142860" "${outdir}/03"
# cp -r "unfinished_inversions/20_157.142860" "${outdir}/04"
# cp -r "unfinished_inversions/20_157.142860" "${outdir}/05"
# cp -r "unfinished_inversions/20_157.142860" "${outdir}/06"
cp -r "unfinished_inversions/20_157.142860" "${outdir}/02"
cp -r "unfinished_inversions/20_157.142860" "${outdir}/03"
cp -r "unfinished_inversions/20_157.142860" "${outdir}/04"
cp -r "unfinished_inversions/20_157.142860" "${outdir}/05"
cp -r "unfinished_inversions/20_157.142860" "${outdir}/06"
# mkdir "${outdir}"/subdir1
# cp -r "unfinished_inversions/20_157.142860" "${outdir}/subdir1/07"
# cp -r "unfinished_inversions/20_157.142860" "${outdir}/08"
Expand Down
10 changes: 10 additions & 0 deletions tests/2_test_python.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env python
import os

from crh_add import crh_add
from crh_wait_to_finish import crh_wait_for_all_sims_to_finish

os.chdir('test00')
crh_add()
crh_wait_for_all_sims_to_finish()
print('Finished the inversions')

0 comments on commit 86faa61

Please sign in to comment.