Skip to content

Commit

Permalink
[bugfix] exception thrown if --print-logs is used (#19)
Browse files Browse the repository at this point in the history
First introduced in 0.1.4 when the --delay option was introduced
  • Loading branch information
nh13 authored Jul 16, 2020
1 parent d8a3adc commit b022eea
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
1 change: 1 addition & 0 deletions pyfgaws/batch/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,7 @@ def wait_on(

minimum_delay = MINIMUM_DELAY if minimum_delay is None else minimum_delay
delay_width = DEFAULT_JITTER_WIDTH if delay_width is None else delay_width
delay = 0 if delay is None else delay
delay = add_jitter(delay=delay, width=delay_width, minima=minimum_delay)
if self._logger is not None:
self._logger.debug("Changing delay from {delay} to {actual_delay}")
Expand Down
17 changes: 12 additions & 5 deletions pyfgaws/batch/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,23 @@
from pyfgaws.logs import Log


def _log_it(region_name: str, job: BatchJob, logger: logging.Logger) -> None:
def _log_it(
region_name: str, job: BatchJob, logger: logging.Logger, delay: Optional[int] = None
) -> None:
"""Creates a background thread to print out CloudWatch logs.
Args:
region_name: the AWS region
job: the AWS batch job
logger: the logger to which logs should be printed
delay: the number of seconds to wait after polling for status. Only used when
`--watch-until` is `true`.
"""
if job.stream is None:
return None
# Create a background thread
logs_thread = threading.Thread(
target=_watch_logs, args=(region_name, job, logger), daemon=True
target=_watch_logs, args=(region_name, job, logger, delay), daemon=True
)
logs_thread.start()

Expand Down Expand Up @@ -72,7 +76,7 @@ def watch_job(

logger.info(f"Watching job with name '{job.name}' and id '{job.job_id}'")
if print_logs:
_log_it(region_name=region_name, job=job, logger=logger)
_log_it(region_name=region_name, job=job, logger=logger, delay=delay)

job.wait_on_complete(delay=delay)
end_status = job.get_status()
Expand Down Expand Up @@ -154,7 +158,7 @@ def run_job(
# - https://github.com/anntzer/defopt/issues/83
if len(watch_until) > 0:
if print_logs:
_log_it(region_name=region_name, job=job, logger=logger)
_log_it(region_name=region_name, job=job, logger=logger, delay=delay)

# Wait for the job to reach on of the statuses
job.wait_on(
Expand All @@ -176,6 +180,7 @@ def _watch_logs(
region_name: str,
job: BatchJob,
logger: logging.Logger,
delay: Optional[int] = None,
polling_interval: int = DEFAULT_LOGS_POLLING_INTERVAL,
indefinitely: bool = True,
) -> None:
Expand All @@ -185,12 +190,14 @@ def _watch_logs(
region_name: the AWS region
job: the AWS batch job
logger: the logger to which logs should be printed
delay: the number of seconds to wait after polling for status. Only used when
`--watch-until` is `true`.
polling_interval: the default time to wait for new CloudWatch logs after no more logs are
returned
indefinitely: true to watch indefinitely, false to print only the available logs
"""
# wait until it's running to get the CloudWatch logs
job.wait_on_running()
job.wait_on_running(delay=delay)

client: Optional[logs.Client] = boto3.client(
service_name="logs", region_name=region_name # type: ignore
Expand Down

0 comments on commit b022eea

Please sign in to comment.