diff --git a/HISTORY.rst b/HISTORY.rst index dfe12b1a..7cba3999 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -29,6 +29,7 @@ PlatformIO Core 4.0 * Extend project environment configuration in "platformio.ini" with other sections using a new `extends `__ option (`issue #2953 `_) * Generate ``.ccls`` LSP file for `Emacs `__ cross references, hierarchies, completion and semantic highlighting * Added ``--no-ansi`` flag for `PIO Core `__ to disable ANSI control characters +* Added ``--shutdown-timeout`` option to `PIO Home Server `__ * Fixed an issue with project generator for `CLion IDE `__ when 2 environments were used (`issue #2824 `_) * Fixed default PIO Unified Debugger configuration for `J-Link probe `__ * Fixed an issue when configuration file options partly ignored when using custom ``--project-conf`` (`issue #3034 `_) diff --git a/docs b/docs index 2c0c2222..22ac1a3e 160000 --- a/docs +++ b/docs @@ -1 +1 @@ -Subproject commit 2c0c2222a55284686f82022025370d856c6d8700 +Subproject commit 22ac1a3ea0f7d895822e391e8441a91bcd201d39 diff --git a/platformio/commands/home/command.py b/platformio/commands/home/command.py index a6340b5d..56298ea1 100644 --- a/platformio/commands/home/command.py +++ b/platformio/commands/home/command.py @@ -35,7 +35,16 @@ from platformio.managers.core import get_core_package_dir, inject_contrib_pysite ), ) @click.option("--no-open", is_flag=True) -def cli(port, host, no_open): +@click.option( + "--shutdown-timeout", + default=0, + type=int, + help=( + "Automatically shutdown server on timeout (in seconds) when no clients " + "are connected. Default is 0 which means never auto shutdown" + ), +) +def cli(port, host, no_open, shutdown_timeout): # pylint: disable=import-error, import-outside-toplevel # import contrib modules @@ -53,7 +62,7 @@ def cli(port, host, no_open): from platformio.commands.home.rpc.server import JSONRPCServerFactory from platformio.commands.home.web import WebRoot - factory = JSONRPCServerFactory() + factory = JSONRPCServerFactory(shutdown_timeout) factory.addHandler(AppRPC(), namespace="app") factory.addHandler(IDERPC(), namespace="ide") factory.addHandler(MiscRPC(), namespace="misc") diff --git a/platformio/commands/home/rpc/server.py b/platformio/commands/home/rpc/server.py index 4eb2aceb..1924754f 100644 --- a/platformio/commands/home/rpc/server.py +++ b/platformio/commands/home/rpc/server.py @@ -76,23 +76,25 @@ class JSONRPCServerProtocol(WebSocketServerProtocol): class JSONRPCServerFactory(WebSocketServerFactory): - SHUTDOWN_TIMEOUT = 3600 # in seconds - protocol = JSONRPCServerProtocol connection_nums = 0 shutdown_timer = 0 - def __init__(self): + def __init__(self, shutdown_timeout=0): super(JSONRPCServerFactory, self).__init__() + self.shutdown_timeout = shutdown_timeout self.dispatcher = jsonrpc.Dispatcher() def shutdownByTimeout(self): + if self.shutdown_timeout < 1: + return + def _auto_shutdown_server(): click.echo("Automatically shutdown server on timeout") reactor.stop() self.shutdown_timer = reactor.callLater( - self.SHUTDOWN_TIMEOUT, _auto_shutdown_server + self.shutdown_timeout, _auto_shutdown_server ) def addHandler(self, handler, namespace):