pull/1291/head
Kaalleen 2021-07-29 18:16:06 +02:00 zatwierdzone przez GitHub
rodzic 1468b1e421
commit 2161379b55
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
3 zmienionych plików z 18 dodań i 26 usunięć

Wyświetl plik

@ -11,7 +11,8 @@ import time
from threading import Thread from threading import Thread
import requests import requests
from flask import Flask, g, request from flask import Flask, g
from werkzeug.serving import make_server
from ..utils.json import InkStitchJSONEncoder from ..utils.json import InkStitchJSONEncoder
from .install import install from .install import install
@ -24,13 +25,14 @@ class APIServer(Thread):
self.extension = args[0] self.extension = args[0]
Thread.__init__(self, *args[1:], **kwargs) Thread.__init__(self, *args[1:], **kwargs)
self.daemon = True self.daemon = True
self.shutting_down = False
self.app = None self.app = None
self.host = None self.host = None
self.port = None self.port = None
self.ready = False self.ready = False
self.__setup_app() self.__setup_app()
self.flask_server = None
self.server_thread = None
def __setup_app(self): # noqa: C901 def __setup_app(self): # noqa: C901
# Disable warning about using a development server in a production environment # Disable warning about using a development server in a production environment
@ -50,20 +52,13 @@ class APIServer(Thread):
# this request # this request
g.extension = self.extension g.extension = self.extension
@self.app.route('/shutdown', methods=['POST'])
def shutdown():
self.shutting_down = True
request.environ.get('werkzeug.server.shutdown')()
return "shutting down"
@self.app.route('/ping') @self.app.route('/ping')
def ping(): def ping():
return "pong" return "pong"
def stop(self): def stop(self):
# for whatever reason, shutting down only seems possible in self.flask_server.shutdown()
# the context of a flask request, so we'll just make one self.server_thread.join()
requests.post("http://%s:%s/shutdown" % (self.host, self.port))
def disable_logging(self): def disable_logging(self):
logging.getLogger('werkzeug').setLevel(logging.ERROR) logging.getLogger('werkzeug').setLevel(logging.ERROR)
@ -76,7 +71,9 @@ class APIServer(Thread):
while True: while True:
try: try:
self.app.run(self.host, self.port, threaded=True) self.flask_server = make_server(self.host, self.port, self.app)
self.server_thread = Thread(target=self.flask_server.serve_forever)
self.server_thread.start()
except socket.error as e: except socket.error as e:
if e.errno == errno.EADDRINUSE: if e.errno == errno.EADDRINUSE:
self.port += 1 self.port += 1

Wyświetl plik

@ -286,7 +286,6 @@ def add_group(document, node, command):
"transform": get_correction_transform(node) "transform": get_correction_transform(node)
}) })
parent.insert(parent.index(node) + 1, group) parent.insert(parent.index(node) + 1, group)
return group return group

Wyświetl plik

@ -15,10 +15,10 @@ from datetime import date
from threading import Thread from threading import Thread
import appdirs import appdirs
import requests
from flask import Flask, Response, jsonify, request, send_from_directory from flask import Flask, Response, jsonify, request, send_from_directory
from jinja2 import Environment, FileSystemLoader, select_autoescape from jinja2 import Environment, FileSystemLoader, select_autoescape
from lxml import etree from lxml import etree
from werkzeug.serving import make_server
from ..gui import open_url from ..gui import open_url
from ..i18n import get_languages from ..i18n import get_languages
@ -66,7 +66,8 @@ class PrintPreviewServer(Thread):
self.realistic_color_block_svgs = kwargs.pop('realistic_color_block_svgs') self.realistic_color_block_svgs = kwargs.pop('realistic_color_block_svgs')
Thread.__init__(self, *args, **kwargs) Thread.__init__(self, *args, **kwargs)
self.daemon = True self.daemon = True
self.shutting_down = False self.flask_server = None
self.server_thread = None
self.__setup_app() self.__setup_app()
@ -89,15 +90,9 @@ class PrintPreviewServer(Thread):
def index(): def index():
return self.html return self.html
@self.app.route('/shutdown', methods=['POST'])
def shutdown():
self.shutting_down = True
request.environ.get('werkzeug.server.shutdown')()
return "shutting down"
@self.app.route('/resources/<path:resource>', methods=['GET']) @self.app.route('/resources/<path:resource>', methods=['GET'])
def resources(resource): def resources(resource):
return send_from_directory(self.resources_path, resource, cache_timeout=1) return send_from_directory(self.resources_path, resource, max_age=1)
@self.app.route('/settings/<field_name>', methods=['POST']) @self.app.route('/settings/<field_name>', methods=['POST'])
def set_field(field_name): def set_field(field_name):
@ -158,9 +153,8 @@ class PrintPreviewServer(Thread):
return Response(self.realistic_overview_svg, mimetype='image/svg+xml') return Response(self.realistic_overview_svg, mimetype='image/svg+xml')
def stop(self): def stop(self):
# for whatever reason, shutting down only seems possible in self.flask_server.shutdown()
# the context of a flask request, so we'll just make one self.server_thread.join()
requests.post("http://%s:%s/shutdown" % (self.host, self.port))
def disable_logging(self): def disable_logging(self):
logging.getLogger('werkzeug').setLevel(logging.ERROR) logging.getLogger('werkzeug').setLevel(logging.ERROR)
@ -173,7 +167,9 @@ class PrintPreviewServer(Thread):
while True: while True:
try: try:
self.app.run(self.host, self.port, threaded=True) self.flask_server = make_server(self.host, self.port, self.app)
self.server_thread = Thread(target=self.flask_server.serve_forever)
self.server_thread.start()
except socket.error as e: except socket.error as e:
if e.errno == errno.EADDRINUSE: if e.errno == errno.EADDRINUSE:
self.port += 1 self.port += 1