-
Notifications
You must be signed in to change notification settings - Fork 116
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
Added state tracking and conditional event triggering for URL status … #212
Open
tarunchy
wants to merge
2
commits into
ansible:main
Choose a base branch
from
tarunchy:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
"""url_check.py. | ||
""" | ||
url_check.py. | ||
|
||
An ansible-rulebook event source plugin that polls a set of URLs and sends | ||
events with their status. | ||
|
@@ -26,30 +27,46 @@ | |
|
||
OK = 200 | ||
|
||
|
||
async def main(queue: asyncio.Queue, args: dict[str, Any]) -> None: | ||
"""Poll a set of URLs and send events with status.""" | ||
urls = args.get("urls", []) | ||
delay = int(args.get("delay", 1)) | ||
verify_ssl = args.get("verify_ssl", True) | ||
|
||
# Added initialization for client_error to handle cases where an exception occurs | ||
client_error = "" | ||
|
||
if not urls: | ||
return | ||
|
||
# Added url_states dictionary to track the last known status of each URL | ||
url_states = {url: None for url in urls} | ||
|
||
while True: | ||
try: | ||
async with aiohttp.ClientSession() as session: | ||
for url in urls: | ||
async with session.get(url, verify_ssl=verify_ssl) as resp: | ||
await queue.put( | ||
{ | ||
"url_check": { | ||
"url": url, | ||
"status": "up" if resp.status == OK else "down", | ||
"status_code": resp.status, | ||
}, | ||
try: | ||
async with session.get(url, ssl=verify_ssl) as resp: | ||
status = "up" if resp.status == OK else "down" | ||
status_code = resp.status | ||
except aiohttp.ClientError as exc: | ||
status = "down" | ||
status_code = None | ||
client_error = str(exc) | ||
# Only trigger event if the status has changed | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure if that logic should be handled by the plugin and it would change the existing behavior so it could break existing rulebooks. |
||
if url_states[url] != status: | ||
url_states[url] = status | ||
event = { | ||
"url_check": { | ||
"url": url, | ||
"status": status, | ||
"status_code": status_code, | ||
}, | ||
) | ||
} | ||
if status == "down" and client_error: | ||
event["url_check"]["error_msg"] = client_error | ||
await queue.put(event) | ||
|
||
except aiohttp.ClientError as exc: | ||
client_error = str(exc) | ||
|
@@ -65,7 +82,6 @@ async def main(queue: asyncio.Queue, args: dict[str, Any]) -> None: | |
|
||
await asyncio.sleep(delay) | ||
|
||
|
||
if __name__ == "__main__": | ||
"""MockQueue if running directly.""" | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think to set the status to "down" is not correct, since the ClientError can be raised by a variety of reasons that doesn't necessarily mean the url is down, like for example networking issues in the client side. This would be something that the plugin is already doing wrong in the upper try/except block.
Instead, I suggest a new option with "false" as default named for example "send_error_events" and send the ClientError as event if its set.
thoughts? @bzwei