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

add log error for ping exception #61

Merged
merged 5 commits into from
Aug 6, 2021
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
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.8.4
0.8.5
34 changes: 27 additions & 7 deletions src/wechaty_puppet_service/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,20 @@
import socket
from logging import Logger
from typing import Optional, Tuple
from ping3 import ping # type: ignore
from ping3 import ping, errors # type: ignore
from wechaty_puppet.exceptions import WechatyPuppetConfigurationError # type: ignore


def extract_host_and_port(url: str) -> Tuple[str, Optional[int]]:
"""it shoule be <host>:<port> format, but it can be a service name.
If it's in docker cluster network, the port can be None
"""
It shoule be <host>:<port> format, but it can be a service name.
If it's in docker cluster network, the port can be None.

Args:
url (str): the service endpoint or names

Return:
host (str), port (Optional[int])
"""
if ':' in url:
host, port = url.split(':')
Expand All @@ -49,16 +53,32 @@ def extract_host_and_port(url: str) -> Tuple[str, Optional[int]]:

def test_endpoint(end_point: str, log: Logger) -> int:
"""
test_endpoint:
1.If there is port: telnet
2.If there is host/domain: ping or
Check end point is valid
Use different method:
1. If there is port: telnet
2. If there is host/domain: ping

Args:
end_point (str): host and port
log (Logger): for debug

Return:
return True if end point is valid, otherwise False

"""
tn = Telnet()
host, port = extract_host_and_port(end_point)

res = True
if port is None:
if ping(host) is False:
try:
if ping(host) is False:
res = False
except PermissionError as pe:
log.error(pe)
res = False
except errors.PingError as e:
log.error(f'got a ping error:\n{e}')
res = False
Comment on lines 73 to 82
Copy link
Collaborator

@wj-Mcat wj-Mcat Aug 5, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your improvement look so great to me. But I don't know in what situation the error will be raised. Can add some more detailed test case for PermissionError and PingError to help us understand the code ? If you can, it's great docs for developer to understand more about endpoint. How do you think about it ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for replay. :-)

There will be a PermissionError, when I set end_point to a service name and run script on a Linux machine without root. And without above codes, due to the outer try catch, I only got Wechaty - ERROR - The network is not good, the bot will try to restart after 60 seconds., which make people confusing. There need more message for figuring out the true problem, so I add try catch for ping call and print possible exception message.

By the way, I optimized the comment.

else:
try:
Expand Down