2018-10-31 19:34:40 +00:00
|
|
|
# coding=utf-8
|
|
|
|
|
2018-09-12 02:32:09 +00:00
|
|
|
import os
|
2019-09-05 15:30:14 +00:00
|
|
|
import platform
|
2020-01-12 14:27:14 +00:00
|
|
|
import signal
|
2020-01-31 22:04:01 +00:00
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
import time
|
2018-10-03 10:53:22 +00:00
|
|
|
|
2018-10-31 19:34:40 +00:00
|
|
|
from bazarr.get_args import args
|
2020-01-31 22:04:01 +00:00
|
|
|
from libs.six import PY3
|
2018-10-03 10:53:22 +00:00
|
|
|
|
2019-09-05 15:30:14 +00:00
|
|
|
|
|
|
|
def check_python_version():
|
|
|
|
python_version = platform.python_version_tuple()
|
2020-01-31 22:04:01 +00:00
|
|
|
minimum_py2_tuple = (2, 7, 13)
|
|
|
|
minimum_py3_tuple = (3, 6, 0)
|
|
|
|
minimum_py2_str = ".".join(str(i) for i in minimum_py2_tuple)
|
|
|
|
minimum_py3_str = ".".join(str(i) for i in minimum_py3_tuple)
|
|
|
|
|
|
|
|
if (int(python_version[0]) == minimum_py3_tuple[0] and int(python_version[1]) < minimum_py3_tuple[1]) or \
|
|
|
|
(int(python_version[0]) != minimum_py3_tuple[0] and int(python_version[0]) != minimum_py2_tuple[0]):
|
|
|
|
print("Python " + minimum_py3_str + " or greater required. "
|
|
|
|
"Current version is " + platform.python_version() + ". Please upgrade Python.")
|
|
|
|
sys.exit(1)
|
|
|
|
elif int(python_version[0]) == minimum_py2_tuple[0] and int(python_version[1]) < minimum_py2_tuple[1]:
|
|
|
|
print("Python " + minimum_py2_str + " or greater required. "
|
|
|
|
"Current version is " + platform.python_version() + ". Please upgrade Python.")
|
|
|
|
sys.exit(1)
|
2019-09-05 15:30:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
check_python_version()
|
|
|
|
|
2018-10-11 01:23:30 +00:00
|
|
|
dir_name = os.path.dirname(__file__)
|
|
|
|
|
2018-10-31 17:09:46 +00:00
|
|
|
|
2020-01-12 14:27:14 +00:00
|
|
|
class ProcessRegistry:
|
|
|
|
|
|
|
|
def register(self, process):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def unregister(self, process):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class DaemonStatus(ProcessRegistry):
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.__should_stop = False
|
|
|
|
self.__processes = set()
|
|
|
|
|
|
|
|
def register(self, process):
|
|
|
|
self.__processes.add(process)
|
|
|
|
|
|
|
|
def unregister(self, process):
|
|
|
|
self.__processes.remove(process)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def __wait_for_processes(processes, timeout):
|
2020-01-31 22:04:01 +00:00
|
|
|
"""
|
|
|
|
Waits all the provided processes for the specified amount of time in seconds.
|
|
|
|
"""
|
2020-01-12 14:27:14 +00:00
|
|
|
reference_ts = time.time()
|
|
|
|
elapsed = 0
|
|
|
|
remaining_processes = list(processes)
|
|
|
|
while elapsed < timeout and len(remaining_processes) > 0:
|
|
|
|
remaining_time = timeout - elapsed
|
|
|
|
for ep in list(remaining_processes):
|
|
|
|
if ep.poll() is not None:
|
|
|
|
remaining_processes.remove(ep)
|
|
|
|
else:
|
|
|
|
if remaining_time > 0:
|
|
|
|
if PY3:
|
|
|
|
try:
|
|
|
|
ep.wait(remaining_time)
|
|
|
|
remaining_processes.remove(ep)
|
2020-01-31 22:04:01 +00:00
|
|
|
except subprocess.TimeoutExpired:
|
2020-01-12 14:27:14 +00:00
|
|
|
pass
|
|
|
|
else:
|
2020-01-31 22:04:01 +00:00
|
|
|
# In python 2 there is no such thing as some mechanism to wait with a timeout
|
2020-01-12 14:27:14 +00:00
|
|
|
time.sleep(1)
|
|
|
|
elapsed = time.time() - reference_ts
|
|
|
|
remaining_time = timeout - elapsed
|
|
|
|
return remaining_processes
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def __send_signal(processes, signal_no, live_processes=None):
|
2020-01-31 22:04:01 +00:00
|
|
|
"""
|
|
|
|
Sends to every single of the specified processes the given signal and (if live_processes is not None) append to
|
|
|
|
it processes which are still alive.
|
|
|
|
"""
|
2020-01-12 14:27:14 +00:00
|
|
|
for ep in processes:
|
|
|
|
if ep.poll() is None:
|
|
|
|
if live_processes is not None:
|
|
|
|
live_processes.append(ep)
|
|
|
|
try:
|
|
|
|
ep.send_signal(signal_no)
|
|
|
|
except Exception as e:
|
2020-01-31 22:04:01 +00:00
|
|
|
print('Failed sending signal %s to process %s because of an unexpected error: %s' % (
|
|
|
|
signal_no, ep.pid, e))
|
2020-01-12 14:27:14 +00:00
|
|
|
return live_processes
|
|
|
|
|
|
|
|
def stop(self):
|
2020-01-31 22:04:01 +00:00
|
|
|
"""
|
|
|
|
Flags this instance as should stop and terminates as smoothly as possible children processes.
|
|
|
|
"""
|
2020-01-12 14:27:14 +00:00
|
|
|
self.__should_stop = True
|
|
|
|
live_processes = DaemonStatus.__send_signal(self.__processes, signal.SIGINT, list())
|
|
|
|
live_processes = DaemonStatus.__wait_for_processes(live_processes, 120)
|
|
|
|
DaemonStatus.__send_signal(live_processes, signal.SIGTERM)
|
|
|
|
|
|
|
|
def should_stop(self):
|
|
|
|
return self.__should_stop
|
2020-01-31 22:04:01 +00:00
|
|
|
|
|
|
|
|
2020-01-12 14:27:14 +00:00
|
|
|
def start_bazarr(process_registry=ProcessRegistry()):
|
2018-10-31 19:34:40 +00:00
|
|
|
script = [sys.executable, "-u", os.path.normcase(os.path.join(dir_name, 'bazarr', 'main.py'))] + sys.argv[1:]
|
2020-01-31 22:04:01 +00:00
|
|
|
|
2019-09-13 19:12:26 +00:00
|
|
|
print("Bazarr starting...")
|
2020-02-09 12:23:56 +00:00
|
|
|
ep = subprocess.Popen(script, stdout=None, stderr=None, stdin=subprocess.DEVNULL)
|
|
|
|
process_registry.register(ep)
|
2018-10-14 13:32:16 +00:00
|
|
|
try:
|
2020-02-09 12:23:56 +00:00
|
|
|
ep.wait()
|
|
|
|
process_registry.unregister(ep)
|
2018-10-14 13:32:16 +00:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
pass
|
2018-10-04 18:16:49 +00:00
|
|
|
|
|
|
|
|
2018-10-03 10:53:22 +00:00
|
|
|
if __name__ == '__main__':
|
2018-10-31 19:34:40 +00:00
|
|
|
restartfile = os.path.normcase(os.path.join(args.config_dir, 'bazarr.restart'))
|
|
|
|
stopfile = os.path.normcase(os.path.join(args.config_dir, 'bazarr.stop'))
|
2020-01-31 22:04:01 +00:00
|
|
|
|
2018-09-12 02:32:09 +00:00
|
|
|
try:
|
2018-10-03 10:53:22 +00:00
|
|
|
os.remove(restartfile)
|
2020-01-31 22:04:01 +00:00
|
|
|
except Exception:
|
2018-10-11 01:23:30 +00:00
|
|
|
pass
|
2020-01-31 22:04:01 +00:00
|
|
|
|
2018-10-11 01:23:30 +00:00
|
|
|
try:
|
2018-10-03 10:53:22 +00:00
|
|
|
os.remove(stopfile)
|
2020-01-31 22:04:01 +00:00
|
|
|
except Exception:
|
2018-09-12 02:32:09 +00:00
|
|
|
pass
|
2020-01-31 22:04:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
def daemon(bazarr_runner=lambda: start_bazarr()):
|
2018-10-03 10:53:22 +00:00
|
|
|
if os.path.exists(stopfile):
|
|
|
|
try:
|
|
|
|
os.remove(stopfile)
|
2020-01-31 22:04:01 +00:00
|
|
|
except Exception:
|
2019-09-13 19:12:26 +00:00
|
|
|
print('Unable to delete stop file.')
|
2018-10-03 10:53:22 +00:00
|
|
|
else:
|
2019-09-13 19:12:26 +00:00
|
|
|
print('Bazarr exited.')
|
2020-01-31 22:04:01 +00:00
|
|
|
sys.exit(0)
|
|
|
|
|
2018-10-03 10:53:22 +00:00
|
|
|
if os.path.exists(restartfile):
|
|
|
|
try:
|
|
|
|
os.remove(restartfile)
|
2020-01-31 22:04:01 +00:00
|
|
|
except Exception:
|
2019-09-13 19:12:26 +00:00
|
|
|
print('Unable to delete restart file.')
|
2018-10-03 10:53:22 +00:00
|
|
|
else:
|
2020-01-19 13:46:27 +00:00
|
|
|
bazarr_runner()
|
2020-01-31 22:04:01 +00:00
|
|
|
|
|
|
|
|
2020-01-19 13:46:27 +00:00
|
|
|
bazarr_runner = lambda: start_bazarr()
|
2020-01-31 22:04:01 +00:00
|
|
|
|
2020-01-19 13:46:27 +00:00
|
|
|
should_stop = lambda: False
|
2020-01-31 22:04:01 +00:00
|
|
|
|
2020-01-19 13:46:27 +00:00
|
|
|
if PY3:
|
|
|
|
daemonStatus = DaemonStatus()
|
2020-01-31 22:04:01 +00:00
|
|
|
|
2020-01-19 13:46:27 +00:00
|
|
|
def shutdown():
|
|
|
|
# indicates that everything should stop
|
|
|
|
daemonStatus.stop()
|
|
|
|
# emulate a Ctrl C command on itself (bypasses the signal thing but, then, emulates the "Ctrl+C break")
|
|
|
|
os.kill(os.getpid(), signal.SIGINT)
|
|
|
|
|
|
|
|
signal.signal(signal.SIGTERM, lambda signal_no, frame: shutdown())
|
2020-01-31 22:04:01 +00:00
|
|
|
|
2020-01-19 13:46:27 +00:00
|
|
|
should_stop = lambda: daemonStatus.should_stop()
|
|
|
|
bazarr_runner = lambda: start_bazarr(daemonStatus)
|
2020-01-31 22:04:01 +00:00
|
|
|
|
2020-01-19 13:46:27 +00:00
|
|
|
bazarr_runner()
|
2020-01-31 22:04:01 +00:00
|
|
|
|
2020-01-12 14:27:14 +00:00
|
|
|
# Keep the script running forever until stop is requested through term or keyboard interrupt
|
2020-01-19 13:46:27 +00:00
|
|
|
while not should_stop():
|
|
|
|
daemon(bazarr_runner)
|
2019-05-31 23:00:32 +00:00
|
|
|
time.sleep(1)
|