Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(wait_http_200): add list #80

Merged
merged 1 commit into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions docs/features.md
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,18 @@ steps:
run: echo "Done"
```

Also, you can wait for a list of urls:
```yaml
steps:
wait-example-http:
wait_http_200:
- https://www.google.com
- https://www.elastic.co
- https://grafana.com
run: echo "Done"
```


## `--no-strict` mode

If you use lume with a command (step) that is not available on the `lume.yml`, this will fail and return an exit code 1.
Expand Down
6 changes: 6 additions & 0 deletions examples/lume-sample.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ steps:
wait-example-http:
wait_http_200: https://www.google.com
run: echo "Done"
wait-example-several-http:
wait_http_200:
- https://www.google.com
- https://www.elastic.co
- https://grafana.com
run: echo "Done"
wait-example-http-no-valid:
env:
LUME_WAIT_HTTP_200_NUM_MAX_ATTEMPTS: 3
Expand Down
7 changes: 5 additions & 2 deletions lume/config/step_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class StepConfig(BaseModel):
teardown: Optional[List[str]] = None
setup_detach: Optional[Dict] = None
wait_seconds: Optional[int] = None
wait_http_200: Optional[str] = None
wait_http_200: Optional[List[str]] = None
overwrote_envs: List[str] = list()

def add_shared_env(self, shared_envs: Dict[str, str]):
Expand All @@ -35,6 +35,9 @@ def from_dict(kdict):
setup_detach, "run", required=True, suffix=" (setup_detach)"
)
envs = get_envs(kdict)

wait_http_200 = check_list_or_str_item(kdict, key="wait_http_200")

return StepConfig(
run=run,
cwd=kdict.get("cwd"),
Expand All @@ -43,5 +46,5 @@ def from_dict(kdict):
teardown=teardown,
setup_detach=setup_detach,
wait_seconds=kdict.get("wait_seconds"),
wait_http_200=kdict.get("wait_http_200"),
wait_http_200=wait_http_200,
)
81 changes: 43 additions & 38 deletions lume/src/application/use_cases/lume_use_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,47 +199,52 @@ def _wait_if_necessary(self, action):
time.sleep(step.wait_seconds)

if step.wait_http_200:
self.logger.log(WAITING, f"Waiting for 200 -> {step.wait_http_200}")
wait_seconds_retry = float(
os.environ.get("LUME_WAIT_HTTP_200_WAIT_SECONDS_RETRY", 1)
)
num_max_attempts = int(
os.environ.get("LUME_WAIT_HTTP_200_NUM_MAX_ATTEMPTS", 20)
)

is_ok = False
num_attempts = 0
for i in range(num_max_attempts):
num_attempts += 1
try:
response = requests.get(step.wait_http_200)
status = response.status_code
status_message = f"{status} \033[F"
if status == 200:
is_ok = True
break
else:
status_message = f"Unexpected return code -> {status} | {response.text} \033[F"
except: # noqa E722
status_message = "Connection Error\033[F"

self.logger.log(
INFO, f" Attempt {num_attempts} -> {status_message}"
)
time.sleep(wait_seconds_retry)

time_elapsed = round((wait_seconds_retry * num_attempts), 2)
if is_ok:
self.logger.log(
INFO,
f" Received a 200 after {num_attempts} attempts in ~{time_elapsed} seconds",
for wait_http_200 in step.wait_http_200:
self.logger.log(WAITING, f"Waiting for 200 -> {wait_http_200}")
wait_seconds_retry = float(
os.environ.get("LUME_WAIT_HTTP_200_WAIT_SECONDS_RETRY", 1)
)
else:
self.logger.log(
WARNING,
f" Not received any 200 after {num_attempts} attempts in ~{time_elapsed} seconds",
num_max_attempts = int(
os.environ.get("LUME_WAIT_HTTP_200_NUM_MAX_ATTEMPTS", 20)
)

is_ok = False
num_attempts = 0
for i in range(num_max_attempts):
num_attempts += 1
try:
response = requests.get(wait_http_200)
status = response.status_code
status_message = f"{status} \033[F"
if status == 200:
is_ok = True
self.logger.log(
INFO,
f" Attempt {num_attempts} -> {status_message}",
)
break
else:
status_message = f"Unexpected return code -> {status} | {response.text} \033[F"
except: # noqa E722
status_message = "Connection Error\033[F"

self.logger.log(
INFO, f" Attempt {num_attempts} -> {status_message}"
)
time.sleep(wait_seconds_retry)

time_elapsed = round((wait_seconds_retry * num_attempts), 2)
if is_ok:
self.logger.log(
INFO,
f" Received a 200 after {num_attempts} attempts in ~{time_elapsed} seconds",
)
else:
self.logger.log(
WARNING,
f" Not received any 200 after {num_attempts} attempts in ~{time_elapsed} seconds",
)

return

@meiga
Expand Down