1
0
Fork 0
mirror of https://github.com/morpheus65535/bazarr synced 2024-12-23 00:03:33 +00:00

Continuing development

This commit is contained in:
Louis Vézina 2018-10-10 21:23:30 -04:00
parent 3119502a0f
commit 21104b4407
2 changed files with 33 additions and 107 deletions

115
bazarr.py
View file

@ -7,6 +7,7 @@ import platform
import logging
import sys
import getopt
import atexit
log = logging.getLogger()
log.setLevel(logging.DEBUG)
@ -26,102 +27,27 @@ for opt, arg in opts:
arguments.append(arg)
dir_name = os.path.dirname(__file__)
def start_bazarr():
script = [sys.executable, os.path.normcase(os.path.join(os.path.dirname(__file__), 'bazarr/main.py'))] + globals()['arguments']
script = [sys.executable, os.path.normcase(os.path.join(globals()['dir_name'], 'bazarr/main.py'))] + globals()['arguments']
pidfile = os.path.normcase(os.path.join(os.path.dirname(__file__), 'bazarr.pid'))
ep = sp.Popen(script, stdout=sp.PIPE, stderr=sp.STDOUT)
try:
file = open(pidfile,'w')
except:
logging.error("Error trying to write pid file.")
else:
file.write(str(ep.pid))
file.close()
logging.info("Bazarr starting with process id: " + str(ep.pid) + "...")
for line in iter(ep.stdout.readline, ''):
sys.stdout.write(line)
def shutdown_bazarr(restarting):
pidfile = os.path.normcase(os.path.join(os.path.dirname(__file__), 'bazarr.pid'))
if os.path.exists(pidfile):
try:
file = open(pidfile,'r')
except:
logging.error("Error trying to read pid file.")
else:
pid = file.read()
file.close()
try:
os.remove(pidfile)
except:
logging.error("Unable to delete pid file.")
else:
logging.info('Bazarr stopping...')
if restarting is False:
stopfile = os.path.normcase(os.path.join(os.path.dirname(__file__), 'bazarr.stop'))
file = open(stopfile, 'w')
file.write('')
file.close()
os.kill(int(pid), signal.SIGINT)
else:
logging.warn("pid file doesn't exist. You must start Bazarr first.")
def restart_bazarr():
restartfile = os.path.normcase(os.path.join(os.path.dirname(__file__), 'bazarr.restart'))
file = open(restartfile, 'w')
file.write('')
file.close()
# GetExitCodeProcess uses a special exit code to indicate that the process is
# still running.
_STILL_ACTIVE = 259
def is_pid_running(pid):
return (_is_pid_running_on_windows(pid) if platform.system() == "Windows"
else _is_pid_running_on_unix(pid))
def _is_pid_running_on_unix(pid):
try:
os.kill(pid, 0)
except OSError:
return False
return True
def _is_pid_running_on_windows(pid):
import ctypes.wintypes
kernel32 = ctypes.windll.kernel32
handle = kernel32.OpenProcess(1, 0, pid)
if handle == 0:
return False
# If the process exited recently, a pid may still exist for the handle.
# So, check if we can get the exit code.
exit_code = ctypes.wintypes.DWORD()
is_running = (
kernel32.GetExitCodeProcess(handle, ctypes.byref(exit_code)) == 0)
kernel32.CloseHandle(handle)
# See if we couldn't get the exit code or the exit code indicates that the
# process is still running.
return is_running or exit_code.value == _STILL_ACTIVE
logging.info("Bazarr starting...")
for line in iter(ep.stdout.readline, ''):
sys.stdout.write(line)
if __name__ == '__main__':
pidfile = os.path.normcase(os.path.join(os.path.dirname(__file__), 'bazarr.pid'))
restartfile = os.path.normcase(os.path.join(os.path.dirname(__file__), 'bazarr.restart'))
stopfile = os.path.normcase(os.path.join(os.path.dirname(__file__), 'bazarr.stop'))
restartfile = os.path.normcase(os.path.join(globals()['dir_name'], 'bazarr.restart'))
stopfile = os.path.normcase(os.path.join(globals()['dir_name'], 'bazarr.stop'))
try:
os.remove(pidfile)
os.remove(restartfile)
except:
pass
try:
os.remove(stopfile)
except:
pass
@ -143,27 +69,14 @@ if __name__ == '__main__':
except:
logging.error('Unable to delete restart file.')
else:
shutdown_bazarr(True)
start_bazarr()
if os.path.exists(pidfile):
try:
file = open(pidfile, 'r')
except:
logging.error("Error trying to read pid file.")
else:
pid = int(file.read())
file.close()
if is_pid_running(pid) is False:
logging.warn('Bazarr unexpectedly exited. Starting it back.')
start_bazarr()
daemon()
start_bazarr()
# Keep the script running forever.
while True:
time.sleep(0.001)

View file

@ -13,9 +13,7 @@ sys.path.insert(0, os.path.join(os.path.dirname(__file__), '../libs/'))
import os
import sys
sys.path.insert(1, os.path.join(sys.path[0], '..'))
from bazarr import shutdown_bazarr, restart_bazarr
import signal
import sqlite3
from init import *
from update_db import *
@ -194,11 +192,26 @@ def redirect_root():
@route(base_url + 'shutdown')
def shutdown():
shutdown_bazarr(False)
try:
stop_file = open(os.path.join(os.pardir, "bazarr.stop file"), "w")
except:
logging.CRITICAL('Cannot create bazarr.stop.')
else:
stop_file.write('')
stop_file.close()
server.
@route(base_url + 'restart')
def restart():
restart_bazarr()
try:
restart_file = open(os.path.join(os.pardir, "bazarr.restart"), "w")
except:
logging.CRITICAL('Cannot create bazarr.restart file.')
else:
restart_file.write('')
restart_file.close()
os.kill(os.getpid(), signal.SIGINT)
@route(base_url + 'static/:path#.+#', name='static')
@custom_auth_basic(check_credentials)
@ -1678,5 +1691,5 @@ import warnings
warnings.simplefilter("ignore", DeprecationWarning)
logging.info('Bazarr is started and waiting for request on http://' + str(ip) + ':' + str(port) + str(base_url))
run(host=ip, port=port, server='waitress', app=app)
server = run(host=ip, port=port, server='waitress', app=app)
logging.info('Bazarr has been stopped.')