Skip to content

Commit

Permalink
extract callback and patter waiting logic into separate methods
Browse files Browse the repository at this point in the history
  • Loading branch information
northernSage committed Mar 20, 2024
1 parent 9f8debc commit 96bb745
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions xprocess/xprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,18 +360,20 @@ class ProcessStarter(ABC):
terminate_on_interrupt = False

def __init__(self, control_dir, process):
self._max_time = None
self.control_dir = control_dir
self.process = process

@property
@abstractmethod
def args(self):
"The args to start the process."
"""The args to start the process."""
pass

@property
@abstractmethod
def pattern(self):
"The pattern to match when the process has started."
"""The pattern to match when the process has started."""
return None

def startup_check(self):
"""Used to assert process responsiveness after pattern match"""
Expand All @@ -397,10 +399,16 @@ def wait_callback(self):
def wait(self, log_file):
"""Wait until the pattern is mached and callback returns successful."""
self._max_time = datetime.now() + timedelta(seconds=self.timeout)
lines = map(self.log_line, self.filter_lines(self.get_lines(log_file)))
if self.pattern is not None:
self.wait_pattern(log_file)
return self.wait_callback()

def wait_pattern(self, log_file):
"""Wait until the pattern is mached and callback returns successful."""
raw_lines = self.get_lines(log_file)
lines = map(self.log_line, self.filter_lines(raw_lines))
has_match = any(re.search(self.pattern, line) for line in lines)
process_ready = self.wait_callback()
return has_match and process_ready
return has_match

def filter_lines(self, lines):
"""fetch first <max_read_lines>, ignoring blank lines."""
Expand Down

0 comments on commit 96bb745

Please sign in to comment.