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

Timeout support #3

Closed
Tethik opened this issue Jan 13, 2020 · 4 comments
Closed

Timeout support #3

Tethik opened this issue Jan 13, 2020 · 4 comments

Comments

@Tethik
Copy link

Tethik commented Jan 13, 2020

Thanks for the library, works great. I'm using it for a script that will automatically spin down the VPS the minecraft server is running on if no players are detected within a certain time.

However one issue I'm finding is if the server is not responding then the library blocks indefinitely. I'm using the following code to fetch the stats.

from mcipc.query import Client

with Client(host, port) as client:
        basic_stats = client.basic_stats

It would be nice if some optional timeout could be implemented into the client so that e.g. server being offline could be handled better.

@Tethik
Copy link
Author

Tethik commented Jan 13, 2020

As a workaround I'm able to do the following:

from mcipc.query import Client
import signal

def check_players_online(host: str, port: int) -> bool:
    def _timeout_handler(signum, frame):
        raise Exception("Minecraft server did not reply in time")

    signal.signal(signal.SIGALRM, _timeout_handler)

    signal.alarm(1) # Should be instant since the server is at localhost
    with Client(host, port) as client:
        print(client.basic_stats)
        signal.alarm(0)
        return client.basic_stats.num_players > 0

However this won't necessarily work in multithreaded or non-UNIX environments.

@conqp
Copy link
Owner

conqp commented Jan 13, 2020

The Rcon an Query clients use python's socket library.
You should be able to set timeouts as described in the socket library's documentation:
https://docs.python.org/3/library/socket.html#socket-timeouts
PS: I added an additional optional timeout parameter to the Rcon and Query clients:
f7c2a3c

@Tethik
Copy link
Author

Tethik commented Jan 13, 2020

The new timeout paramater works perfectly. Thanks for the fix!

For future reference, my new usage looks as follows:

def check_players_online(host: str, port: int) -> bool:
    with Client(host, port, timeout=1) as client:
        print(client.basic_stats)
        print(f"{client.basic_stats.num_players} player(s) are online")
        return client.basic_stats.num_players > 0

@Tethik Tethik closed this as completed Jan 13, 2020
@conqp
Copy link
Owner

conqp commented Jan 14, 2020

Glad, I could help. I'm always interested in providing new features.
As for your use case, you might want to have a look at mcipc's own rconclt (which, however uses RCON, not Query), and especially at rconclt <server> in-use and rconclt <server> idle-shutdown.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants