Simpler loader script
This commit is contained in:
+56
-48
@@ -1,10 +1,9 @@
|
||||
#!/usr/bin/env python
|
||||
from os.path import dirname
|
||||
from signal import signal, SIGTERM
|
||||
import os
|
||||
import signal
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
|
||||
|
||||
# Root path
|
||||
@@ -13,60 +12,69 @@ base_path = dirname(os.path.abspath(__file__))
|
||||
# Insert local directories into path
|
||||
sys.path.insert(0, os.path.join(base_path, 'libs'))
|
||||
|
||||
from couchpotato.core.logger import CPLog
|
||||
log = CPLog(__name__)
|
||||
|
||||
# Get options via arg
|
||||
from couchpotato.runner import getOptions
|
||||
from couchpotato.core.helpers.variable import getDataDir
|
||||
options = getOptions(base_path, sys.argv[1:])
|
||||
data_dir = getDataDir()
|
||||
class Loader(object):
|
||||
|
||||
def start():
|
||||
try:
|
||||
args = [sys.executable] + [os.path.join(base_path, __file__)] + sys.argv[1:]
|
||||
new_environ = os.environ.copy()
|
||||
new_environ['cp_main'] = 'true'
|
||||
do_restart = True
|
||||
|
||||
if os.name == 'nt':
|
||||
for key, value in new_environ.iteritems():
|
||||
if isinstance(value, unicode):
|
||||
new_environ[key] = value.encode('iso-8859-1')
|
||||
def __init__(self):
|
||||
|
||||
subprocess.call(args, env = new_environ)
|
||||
return os.path.isfile(os.path.join(data_dir, 'restart'))
|
||||
except KeyboardInterrupt, e:
|
||||
pass
|
||||
except Exception, e:
|
||||
log.critical(e)
|
||||
return 0
|
||||
from couchpotato.core.logger import CPLog
|
||||
self.log = CPLog(__name__)
|
||||
|
||||
# Get options via arg
|
||||
from couchpotato.runner import getOptions
|
||||
from couchpotato.core.helpers.variable import getDataDir
|
||||
self.options = getOptions(base_path, sys.argv[1:])
|
||||
self.data_dir = getDataDir()
|
||||
|
||||
def addSignals(self):
|
||||
|
||||
signal.signal(signal.SIGINT, self.onExit)
|
||||
signal.signal(signal.SIGTERM, lambda signum, stack_frame: sys.exit(1))
|
||||
|
||||
from couchpotato.core.event import addEvent
|
||||
addEvent('app.after_shutdown', self.afterShutdown)
|
||||
|
||||
def afterShutdown(self, restart):
|
||||
self.do_restart = restart
|
||||
|
||||
def onExit(self, signal, frame):
|
||||
from couchpotato.core.event import fireEvent
|
||||
fireEvent('app.crappy_shutdown', single = True)
|
||||
|
||||
def run(self):
|
||||
|
||||
self.addSignals()
|
||||
|
||||
from couchpotato.runner import runCouchPotato
|
||||
def main():
|
||||
if os.environ.get('cp_main', 'false') == 'true':
|
||||
try:
|
||||
runCouchPotato(options, base_path, sys.argv[1:])
|
||||
from couchpotato.runner import runCouchPotato
|
||||
runCouchPotato(self.options, base_path, sys.argv[1:])
|
||||
except Exception, e:
|
||||
log.critical(e)
|
||||
else:
|
||||
while 1:
|
||||
restart = start()
|
||||
if not restart:
|
||||
break
|
||||
self.log.critical(e)
|
||||
|
||||
from couchpotato.core.event import fireEvent
|
||||
fireEvent('app.crappy_shutdown', single = True)
|
||||
time.sleep(1)
|
||||
if self.do_restart:
|
||||
self.restart()
|
||||
|
||||
sys.exit(0)
|
||||
|
||||
def restart(self):
|
||||
try:
|
||||
args = [sys.executable] + [os.path.join(base_path, __file__)] + sys.argv[1:]
|
||||
subprocess.Popen(args)
|
||||
except Exception, e:
|
||||
self.log.critical(e)
|
||||
return 0
|
||||
|
||||
def daemonize(self):
|
||||
|
||||
if self.options.daemon and self.options.pid_file:
|
||||
from daemon import Daemon
|
||||
daemon = Daemon(self.options.pid_file)
|
||||
daemon.daemonize()
|
||||
|
||||
sys.exit()
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
signal(SIGTERM, lambda signum, stack_frame: sys.exit(1))
|
||||
|
||||
if options.daemon and options.pid_file and not os.environ.get('cp_main'):
|
||||
from daemon import Daemon
|
||||
daemon = Daemon(options.pid_file)
|
||||
daemon.daemonize()
|
||||
|
||||
main()
|
||||
l = Loader()
|
||||
l.daemonize()
|
||||
l.run()
|
||||
|
||||
@@ -8,7 +8,6 @@ from couchpotato.environment import Env
|
||||
from flask import request
|
||||
from uuid import uuid4
|
||||
import os
|
||||
import thread
|
||||
import time
|
||||
import traceback
|
||||
import webbrowser
|
||||
@@ -32,7 +31,6 @@ class Core(Plugin):
|
||||
addEvent('app.crappy_shutdown', self.crappyShutdown)
|
||||
addEvent('app.crappy_restart', self.crappyRestart)
|
||||
addEvent('app.load', self.launchBrowser, priority = 1)
|
||||
addEvent('app.load', self.monitorParent)
|
||||
addEvent('app.base_url', self.createBaseUrl)
|
||||
addEvent('app.api_url', self.createApiUrl)
|
||||
|
||||
@@ -41,27 +39,6 @@ class Core(Plugin):
|
||||
|
||||
self.removeRestartFile()
|
||||
|
||||
def monitorParent(self):
|
||||
|
||||
def looping():
|
||||
do_shutdown = False
|
||||
while 1 and not self.shuttingDown():
|
||||
if os.name == 'nt':
|
||||
if os.getppid(os.getpid()) <= 1:
|
||||
do_shutdown = True
|
||||
break
|
||||
else:
|
||||
if os.getppid() <= 1:
|
||||
do_shutdown = True
|
||||
break
|
||||
time.sleep(1)
|
||||
|
||||
if do_shutdown:
|
||||
log.info('Starterscript has shutdown, shutdown subprocess')
|
||||
fireEvent('app.crappy_shutdown')
|
||||
|
||||
thread.start_new_thread(looping, ())
|
||||
|
||||
def md5Password(self, value):
|
||||
return md5(value) if value else ''
|
||||
|
||||
|
||||
Reference in New Issue
Block a user