Skip to content

Commit 7c03458

Browse files
Merge pull request automatic-ripping-machine#1730 from microtechno9000/bugfix_armui_service
[BUGFIX] Gracefull shutdown of armui service outside docker
2 parents c9d01d9 + d0ae5a0 commit 7c03458

5 files changed

Lines changed: 51 additions & 33 deletions

File tree

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.23.1
1+
2.23.2

arm/runui.py

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"""Main run page for armui"""
33
import os
44
import sys
5+
import signal
56

67
# set the PATH to /arm/arm, so we can handle imports properly
78
sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), ".."))
@@ -13,24 +14,45 @@
1314

1415
from arm.ui import app # noqa E402
1516

17+
shutdown_requested = False
18+
1619

1720
def startup():
21+
"""ARM UI Startup check on database config"""
1822
db_update = arm.ui.utils.arm_db_check()
1923
if db_update["db_current"]:
2024
app.logger.info("Updating Optical Drives")
2125
arm.ui.settings.DriveUtils.drives_update(startup=True)
2226

2327

28+
def handle_shutdown(signum, frame):
29+
"""ARM handle SIGTERM/SIGINT for graceful shutdown"""
30+
global shutdown_requested
31+
shutdown_requested = True
32+
app.logger.info("Received shutdown signal (%s). Shutting down ARM-UI.", signum)
33+
sys.exit(0)
34+
35+
36+
# Register signal handlers
37+
signal.signal(signal.SIGTERM, handle_shutdown) # systemd shutdown command
38+
signal.signal(signal.SIGINT, handle_shutdown) # keyboard interrupt
39+
40+
2441
def is_docker():
2542
"""
2643
Test to check if running inside a docker/container
2744
returns: Boolean
2845
"""
2946
path = '/proc/self/cgroup'
30-
return (
31-
os.path.exists('/.dockerenv') or
32-
os.path.isfile(path) and any('docker' in line for line in open(path))
33-
)
47+
48+
if os.path.exists('/.dockerenv'):
49+
return True
50+
51+
if os.path.isfile(path):
52+
with open(path) as f:
53+
return any('docker' in line for line in f)
54+
55+
return False
3456

3557

3658
def get_host():
@@ -59,6 +81,15 @@ def get_host():
5981
host = get_host()
6082
port = cfg.arm_config['WEBSERVER_PORT']
6183
app.logger.info("Starting ARM-UI on interface address - %s:%s", host, port)
84+
85+
# Run ARM Startup
6286
startup()
87+
6388
from waitress import serve
64-
serve(app, host=host, port=port, threads=40)
89+
90+
try:
91+
serve(app, host=host, port=port, threads=40)
92+
except KeyboardInterrupt:
93+
app.logger.info("Keyboard interrupt received, shutting down ARM-UI.")
94+
finally:
95+
app.logger.info("ARM-UI shutdown complete.")

scripts/installers/DebianInstaller.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -960,7 +960,7 @@ function SetupFolders() {
960960

961961
function CreateAndStartService() {
962962
echo -e "${RED}Installing ARM service${NC}"
963-
cp /opt/arm/setup/arm.service /lib/systemd/system/armui.service
963+
cp /opt/arm/setup/armui.service /lib/systemd/system/armui.service
964964
systemctl daemon-reload
965965
systemctl enable armui
966966
systemctl start armui

setup/arm.service

Lines changed: 0 additions & 23 deletions
This file was deleted.

setup/armui.service

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,31 @@
11
[Unit]
2-
Description=Arm service
2+
Description=ARM service
33
## Added to force armui to wait for network
44
After=network-online.target
55
Wants=network-online.target
6+
## Shutdown ARM before system shutdown
7+
Before=shutdown.target
68

79
[Service]
810
Type=simple
911
User=arm
1012
Group=arm
11-
## Add your path to your logfiles if you want to enable logging
12-
## Remember to remove the # at the start of the line
13+
## Define log paths
1314
StandardOutput=append:/home/arm/logs/WebUI.log
1415
StandardError=append:/home/arm/logs/WebUI.log
1516
Restart=always
1617
RestartSec=3
1718
ExecStartPre=/bin/bash -xc '/usr/bin/systemctl is-active --quiet armui.service && exit 1 || exit 0'
1819
ExecStart=/opt/arm/venv/bin/python3 /opt/arm/arm/runui.py
1920

21+
## Allow ARM to run in protected ports (ei: ports 1~1024)
22+
AmbientCapabilities=CAP_NET_BIND_SERVICE
23+
24+
## Graceful shutdown behaviour
25+
KillSignal=SIGTERM
26+
TimeoutStopSec=60
27+
KillMode=mixed
28+
SendSIGKILL=no
29+
2030
[Install]
2131
WantedBy=multi-user.target

0 commit comments

Comments
 (0)