From 839d513bb80cb601d7309cb831a009b812824845 Mon Sep 17 00:00:00 2001 From: creslinux Date: Sat, 23 Jun 2018 11:53:09 +0000 Subject: [PATCH] Added api server shutdown function, and exposed on HTTP as /stop_api url This will stop the running app gracefully - processing current api calls then shutting the werkzueg (run) listening server. Have also called this from the cleanup placeholder. I'm not sure this is what is intended by cleanup def. By which I mean there may be a thread left running with no app within - not sure how to check this just yet. tidied excessive logging. --- freqtrade/rpc/api_server.py | 49 +++++++++++++++++++++++++++++++------ 1 file changed, 41 insertions(+), 8 deletions(-) diff --git a/freqtrade/rpc/api_server.py b/freqtrade/rpc/api_server.py index 3a8391b98..d89bee5f9 100644 --- a/freqtrade/rpc/api_server.py +++ b/freqtrade/rpc/api_server.py @@ -41,6 +41,7 @@ class ApiServerSuperWrap(RPC): """ app.register_error_handler(404, self.page_not_found) app.add_url_rule('/', 'hello', view_func=self.hello, methods=['GET']) + app.add_url_rule('/stop_api', 'stop_api', view_func=self.stop_api, methods=['GET']) def register_rest_rpc_urls(self): """ @@ -77,10 +78,6 @@ class ApiServerSuperWrap(RPC): except Exception: logger.exception("Api server failed to start, exception message is:") - def cleanup(self) -> None: - # TODO: implement me - raise NotImplementedError - @property def name(self) -> str: # TODO: implement me @@ -90,11 +87,43 @@ class ApiServerSuperWrap(RPC): # TODO: implement me raise NotImplementedError + def shutdown_api_server(self): + """ + Stop the running flask application + + Records the shutdown in logger.info + :return: + """ + func = request.environ.get('werkzeug.server.shutdown') + if func is None: + raise RuntimeError('Not running the Flask Werkzeug Server') + if func is not None: + logger.info('Stopping the Local Rest Server') + func() + return + + def cleanup(self) -> None: + """ + Stops the running application server + + Does not stop the thread,this may not be the desired outcome of cleanup. TBC + :return: + """ + self.shutdown_api_server() + # def cleanup(self) -> None: + # # TODO: implement me + # raise NotImplementedError + """ Define the application methods here, called by app.add_url_rule each Telegram command should have a like local substitute """ + def stop_api(self): + """ For calling shutdown_api_server over via api server HTTP""" + self.shutdown_api_server() + return 'Api Server shutting down... ' + def page_not_found(self, error): # Return "404 not found", 404. return jsonify({'status': 'error', @@ -110,11 +139,16 @@ class ApiServerSuperWrap(RPC): :return: index.html """ rest_cmds = 'Commands implemented:
' \ - '/daily?timescale=7' \ + 'Show 7 days of stats' \ '
' \ - '/stop' \ + 'Stop the Trade thread' \ '
' \ - '/start' + 'Start the Traded thread' \ + '
' \ + ' 404 page does not exist' \ + '
' \ + '
' \ + 'Shut down the api server - be sure' return rest_cmds def daily(self): @@ -125,7 +159,6 @@ class ApiServerSuperWrap(RPC): """ try: timescale = request.args.get('timescale') - logger.info("LocalRPC - Daily Command Called") timescale = int(timescale) stats = self._rpc_daily_profit(timescale,