Update public source
This commit is contained in:
+21
-12
@@ -291,17 +291,21 @@ scanner_output = deque(
|
||||
|
||||
scanner_lock = threading.Lock()
|
||||
|
||||
scanner_output_thread = None
|
||||
|
||||
vendor_previews = {}
|
||||
|
||||
vendor_preview_lock = threading.Lock()
|
||||
|
||||
vendor_update_job = None
|
||||
vendor_update_thread = None
|
||||
|
||||
vendor_update_lock = threading.Lock()
|
||||
|
||||
vendor_operation_lock = threading.Lock()
|
||||
|
||||
vendor_recalc_job = None
|
||||
vendor_recalc_thread = None
|
||||
|
||||
vendor_recalc_lock = threading.Lock()
|
||||
|
||||
@@ -309,6 +313,9 @@ gps_test_service = None
|
||||
|
||||
gps_test_lock = threading.Lock()
|
||||
|
||||
sudo_keepalive_stop_event = threading.Event()
|
||||
sudo_keepalive_thread = None
|
||||
|
||||
wifi_connection_service = WiFiConnectionService()
|
||||
|
||||
def check_sudo():
|
||||
@@ -335,13 +342,11 @@ def check_sudo():
|
||||
return result.returncode == 0
|
||||
|
||||
|
||||
def sudo_keepalive():
|
||||
def sudo_keepalive(stop_event):
|
||||
|
||||
checker = SudoChecker()
|
||||
|
||||
while True:
|
||||
|
||||
time.sleep(60)
|
||||
while not stop_event.wait(60):
|
||||
|
||||
checker.refresh()
|
||||
|
||||
@@ -1582,6 +1587,7 @@ def _run_vendor_update(
|
||||
def reports_vendors_update():
|
||||
|
||||
global vendor_update_job
|
||||
global vendor_update_thread
|
||||
|
||||
config = read_config()
|
||||
|
||||
@@ -1647,7 +1653,7 @@ def reports_vendors_update():
|
||||
|
||||
try:
|
||||
|
||||
thread = threading.Thread(
|
||||
vendor_update_thread = threading.Thread(
|
||||
target=_run_vendor_update,
|
||||
args=(
|
||||
job_id,
|
||||
@@ -1656,7 +1662,7 @@ def reports_vendors_update():
|
||||
daemon=True
|
||||
)
|
||||
|
||||
thread.start()
|
||||
vendor_update_thread.start()
|
||||
|
||||
except Exception as error:
|
||||
|
||||
@@ -1791,6 +1797,7 @@ def _run_vendor_recalculation(
|
||||
def reports_vendors_recalculate():
|
||||
|
||||
global vendor_recalc_job
|
||||
global vendor_recalc_thread
|
||||
|
||||
config = read_config()
|
||||
|
||||
@@ -1846,7 +1853,7 @@ def reports_vendors_recalculate():
|
||||
|
||||
try:
|
||||
|
||||
thread = threading.Thread(
|
||||
vendor_recalc_thread = threading.Thread(
|
||||
target=_run_vendor_recalculation,
|
||||
args=(
|
||||
job_id,
|
||||
@@ -1855,7 +1862,7 @@ def reports_vendors_recalculate():
|
||||
daemon=True
|
||||
)
|
||||
|
||||
thread.start()
|
||||
vendor_recalc_thread.start()
|
||||
|
||||
except Exception as error:
|
||||
|
||||
@@ -3386,6 +3393,7 @@ def config_save():
|
||||
def scanner_start():
|
||||
|
||||
global scanner_process
|
||||
global scanner_output_thread
|
||||
global scanner_health
|
||||
global scanner_stopped_at
|
||||
global scanner_starting
|
||||
@@ -3536,13 +3544,13 @@ def scanner_start():
|
||||
)
|
||||
|
||||
|
||||
thread = threading.Thread(
|
||||
scanner_output_thread = threading.Thread(
|
||||
target=read_scanner_output,
|
||||
args=(process,),
|
||||
daemon=True
|
||||
)
|
||||
|
||||
thread.start()
|
||||
scanner_output_thread.start()
|
||||
|
||||
|
||||
return redirect(
|
||||
@@ -3619,12 +3627,13 @@ if __name__ == "__main__":
|
||||
)
|
||||
|
||||
|
||||
thread = threading.Thread(
|
||||
sudo_keepalive_thread = threading.Thread(
|
||||
target=sudo_keepalive,
|
||||
args=(sudo_keepalive_stop_event,),
|
||||
daemon=True
|
||||
)
|
||||
|
||||
thread.start()
|
||||
sudo_keepalive_thread.start()
|
||||
|
||||
|
||||
logging.getLogger("werkzeug").disabled = True
|
||||
|
||||
@@ -0,0 +1,192 @@
|
||||
import threading
|
||||
import time
|
||||
|
||||
|
||||
class RequestLifecycleMiddleware:
|
||||
|
||||
ALLOWED_DURING_SHUTDOWN = {
|
||||
("GET", "/"),
|
||||
("HEAD", "/"),
|
||||
("GET", "/api/status"),
|
||||
("GET", "/api/gps-test"),
|
||||
("POST", "/scanner/stop"),
|
||||
("POST", "/gps-test/stop"),
|
||||
}
|
||||
|
||||
STATIC_PREFIXES = (
|
||||
"/static/",
|
||||
"/reports/static/",
|
||||
)
|
||||
|
||||
def __init__(self, application):
|
||||
|
||||
self.application = application
|
||||
|
||||
self._condition = threading.Condition()
|
||||
self._shutdown_requested = False
|
||||
self._active_requests = 0
|
||||
|
||||
@property
|
||||
def shutdown_requested(self):
|
||||
|
||||
with self._condition:
|
||||
|
||||
return self._shutdown_requested
|
||||
|
||||
@property
|
||||
def active_requests(self):
|
||||
|
||||
with self._condition:
|
||||
|
||||
return self._active_requests
|
||||
|
||||
def begin_shutdown(self):
|
||||
|
||||
with self._condition:
|
||||
|
||||
self._shutdown_requested = True
|
||||
|
||||
self._condition.notify_all()
|
||||
|
||||
def wait_for_drain(self, timeout=None):
|
||||
|
||||
deadline = (
|
||||
None
|
||||
if timeout is None
|
||||
else time.monotonic() + timeout
|
||||
)
|
||||
|
||||
with self._condition:
|
||||
|
||||
while self._active_requests:
|
||||
|
||||
remaining = (
|
||||
None
|
||||
if deadline is None
|
||||
else deadline - time.monotonic()
|
||||
)
|
||||
|
||||
if remaining is not None and remaining <= 0:
|
||||
return False
|
||||
|
||||
self._condition.wait(remaining)
|
||||
|
||||
return True
|
||||
|
||||
def _is_allowed_during_shutdown(self, method, path):
|
||||
|
||||
if (method, path) in self.ALLOWED_DURING_SHUTDOWN:
|
||||
return True
|
||||
|
||||
if method not in ("GET", "HEAD"):
|
||||
return False
|
||||
|
||||
return any(
|
||||
path.startswith(prefix)
|
||||
for prefix in self.STATIC_PREFIXES
|
||||
)
|
||||
|
||||
def _release_request(self):
|
||||
|
||||
with self._condition:
|
||||
|
||||
self._active_requests -= 1
|
||||
|
||||
self._condition.notify_all()
|
||||
|
||||
def __call__(self, environ, start_response):
|
||||
|
||||
method = environ.get("REQUEST_METHOD", "GET").upper()
|
||||
path = environ.get("PATH_INFO", "/")
|
||||
|
||||
with self._condition:
|
||||
|
||||
if (
|
||||
self._shutdown_requested
|
||||
and not self._is_allowed_during_shutdown(
|
||||
method,
|
||||
path
|
||||
)
|
||||
):
|
||||
|
||||
body = b"Application is shutting down.\n"
|
||||
|
||||
start_response(
|
||||
"503 Service Unavailable",
|
||||
[
|
||||
("Content-Type", "text/plain; charset=utf-8"),
|
||||
("Content-Length", str(len(body))),
|
||||
("Retry-After", "5"),
|
||||
("Cache-Control", "no-store"),
|
||||
]
|
||||
)
|
||||
|
||||
return [body]
|
||||
|
||||
self._active_requests += 1
|
||||
|
||||
try:
|
||||
|
||||
response = self.application(
|
||||
environ,
|
||||
start_response
|
||||
)
|
||||
|
||||
except BaseException:
|
||||
|
||||
self._release_request()
|
||||
|
||||
raise
|
||||
|
||||
return _TrackedIterable(
|
||||
response,
|
||||
self._release_request
|
||||
)
|
||||
|
||||
|
||||
class _TrackedIterable:
|
||||
|
||||
def __init__(self, iterable, release):
|
||||
|
||||
self._iterable = iterable
|
||||
self._iterator = iter(iterable)
|
||||
self._release = release
|
||||
self._closed = False
|
||||
|
||||
def __iter__(self):
|
||||
|
||||
return self
|
||||
|
||||
def __next__(self):
|
||||
|
||||
try:
|
||||
|
||||
return next(self._iterator)
|
||||
|
||||
except BaseException:
|
||||
|
||||
self.close()
|
||||
|
||||
raise
|
||||
|
||||
def close(self):
|
||||
|
||||
if self._closed:
|
||||
return
|
||||
|
||||
self._closed = True
|
||||
|
||||
try:
|
||||
|
||||
close = getattr(
|
||||
self._iterable,
|
||||
"close",
|
||||
None
|
||||
)
|
||||
|
||||
if close is not None:
|
||||
close()
|
||||
|
||||
finally:
|
||||
|
||||
self._release()
|
||||
Reference in New Issue
Block a user