Skip to content

Commit

Permalink
Add functionality for capturing error for later use
Browse files Browse the repository at this point in the history
  • Loading branch information
akashdhruv committed Oct 2, 2024
1 parent 3d6f61a commit 76d1194
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
26 changes: 23 additions & 3 deletions jobrunner/api/_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ def setup(dirlist, verbose=False, exit_on_failure=False):
# set variable to determine console separator
separator = False

# create an empty list for output
output = []

# loop over dirlist
for workdir in dirlist:

Expand Down Expand Up @@ -51,14 +54,21 @@ def setup(dirlist, verbose=False, exit_on_failure=False):
print(f'{" "*4}- {value.replace(basedir,"<ROOT>")}')

# run a bash process
lib.BashProcess(basedir, workdir, "job.setup", verbose, exit_on_failure)
process = lib.BashProcess(
basedir, workdir, "job.setup", verbose, exit_on_failure
)

# append process to output
output.append(process)

# set separator value
separator = True

# Return to base directory
os.chdir(basedir)

return output


def submit(dirlist, verbose=False, exit_on_failure=False):
"""
Expand All @@ -70,6 +80,9 @@ def submit(dirlist, verbose=False, exit_on_failure=False):
# set variable to determine console separator
separator = False

# create an empty list for output
output = []

# loop over dirlist
for workdir in dirlist:

Expand Down Expand Up @@ -116,19 +129,26 @@ def submit(dirlist, verbose=False, exit_on_failure=False):

# Submit job
if config.schedular.command == "bash":
lib.BashProcess(basedir, workdir, "job.submit", verbose, exit_on_failure)
process = lib.BashProcess(
basedir, workdir, "job.submit", verbose, exit_on_failure
)

else:
lib.SchedularProcess(
process = lib.SchedularProcess(
basedir, workdir, config.schedular.command, "job.submit"
)

# append process to output
output.append(process)

# set separator value
separator = True

# Return to base directory
os.chdir(basedir)

return output


def clean(dirlist):
"""
Expand Down
12 changes: 8 additions & 4 deletions jobrunner/lib/_console.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ def SchedularProcess(basedir, workdir, command, script):
check=True,
)

return process


def BashProcess(basedir, workdir, script, verbose=False, exit_on_failure=False):
"""
Expand Down Expand Up @@ -73,11 +75,11 @@ def BashProcess(basedir, workdir, script, verbose=False, exit_on_failure=False):
# output.write(line)

else:
with open("job.output", "w") as output:
with open("job.output", "w") as output, open("job.error", "w") as error:
process = subprocess.Popen(
f"bash {script}".split(),
stdout=output,
stderr=subprocess.STDOUT,
stderr=error,
text=True,
)

Expand All @@ -87,8 +89,8 @@ def BashProcess(basedir, workdir, script, verbose=False, exit_on_failure=False):

if process.returncode != 0:
if not verbose:
with open("job.output", "r") as output:
print("".join(output.readlines()[-8:]))
with open("job.error", "r") as error:
print("".join(error.readlines()[-8:]))

if exit_on_failure:
raise ValueError(f"{lib.Color.red}FAILURE {lib.Color.end}")
Expand All @@ -101,3 +103,5 @@ def BashProcess(basedir, workdir, script, verbose=False, exit_on_failure=False):
print(
f'\n{lib.Color.purple}OUTPUT:{lib.Color.end} {workdir.replace(basedir,"<ROOT>")}/job.output'
)

return process
1 change: 1 addition & 0 deletions jobrunner/lib/_filetools.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ def RemoveNodeFiles(config, nodedir):
nodedir + os.sep + "job.submit",
nodedir + os.sep + "job.target",
nodedir + os.sep + "job.output",
nodedir + os.sep + "job.error",
]

# loop over list of files in nodedir and append to
Expand Down

0 comments on commit 76d1194

Please sign in to comment.