Skip to content

Commit

Permalink
Catch TimeoutExpired error for subprocesses #298
Browse files Browse the repository at this point in the history
  • Loading branch information
rafelafrance committed Feb 19, 2021
1 parent f250616 commit 6c2f3fd
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 5 deletions.
6 changes: 3 additions & 3 deletions lib/assemblers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import datetime
from os.path import abspath, basename, exists, getsize, join, splitext
from subprocess import CalledProcessError
from subprocess import CalledProcessError, TimeoutExpired

from .. import bio, db_atram, util

Expand Down Expand Up @@ -72,11 +72,11 @@ def run(self):
self.log.info('Assembling shards with {}: iteration {}'.format(
self.args['assembler'], self.state['iteration']))
self.assemble()
except TimeoutError:
except (TimeoutExpired, TimeoutError):
msg = 'Time ran out for the assembler after {} (HH:MM:SS)'.format(
datetime.timedelta(seconds=self.args['timeout']))
self.log.error(msg)
raise TimeoutError(msg)
raise TimeoutExpired(msg, timeout=self.args['timeout'])
except CalledProcessError as cpe:
msg = 'The assembler failed with error: ' + str(cpe)
self.log.error(msg)
Expand Down
3 changes: 2 additions & 1 deletion lib/core_atram.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import re
from multiprocessing import Pool
from os.path import basename, join, split, splitext
from subprocess import TimeoutExpired

from Bio import SeqIO

Expand Down Expand Up @@ -34,7 +35,7 @@ def assemble(args):

try:
assembly_loop(args, log, assembler, blast_db, query)
except (TimeoutError, RuntimeError):
except (TimeoutExpired, TimeoutError, RuntimeError):
pass
except Exception as err: # pylint: disable=broad-except
log.error('Exception: {}'.format(err))
Expand Down
2 changes: 1 addition & 1 deletion lib/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import sys
from os.path import basename, exists, join

ATRAM_VERSION = 'v2.3.4'
ATRAM_VERSION = 'v2.3.5'

# DB_VERSION != ATRAM_VERSION
# We don't force DB changes until required.
Expand Down
5 changes: 5 additions & 0 deletions lib/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ def subcommand(self, cmd, temp_dir, timeout=None):
"""
self.debug(cmd)

error = None

with tempfile.NamedTemporaryFile(mode='w', dir=temp_dir) as log_output:
try:
subprocess.check_call(
Expand All @@ -59,12 +61,15 @@ def subcommand(self, cmd, temp_dir, timeout=None):
stderr=log_output)
except Exception as err: # pylint: disable=broad-except
self.error('Exception: {}'.format(err))
error = err
finally:
with open(log_output.name) as log_input:
for line in log_input:
line = line.strip()
if line:
self.debug(line)
if error:
raise error

def _output(self, msg, level):
timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
Expand Down

0 comments on commit 6c2f3fd

Please sign in to comment.