Simpler loader script
This commit is contained in:
1 parent
67e07c3008
commit
a42ff5e038
2 files changed
+56
-71
No files matched your search
+56
-48
@@ -1,10 +1,9 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
from os.path import dirname
|
from os.path import dirname
|
||||||
from signal import signal, SIGTERM
|
|
||||||
import os
|
import os
|
||||||
|
import signal
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
import time
|
|
||||||
|
|
||||||
|
|
||||||
# Root path
|
# Root path
|
||||||
@@ -13,60 +12,69 @@ base_path = dirname(os.path.abspath(__file__))
|
|||||||
# Insert local directories into path
|
# Insert local directories into path
|
||||||
sys.path.insert(0, os.path.join(base_path, 'libs'))
|
sys.path.insert(0, os.path.join(base_path, 'libs'))
|
||||||
|
|
||||||
from couchpotato.core.logger import CPLog
|
|
||||||
log = CPLog(__name__)
|
|
||||||
|
|
||||||
# Get options via arg
|
class Loader(object):
|
||||||
from couchpotato.runner import getOptions
|
|
||||||
from couchpotato.core.helpers.variable import getDataDir
|
|
||||||
options = getOptions(base_path, sys.argv[1:])
|
|
||||||
data_dir = getDataDir()
|
|
||||||
|
|
||||||
def start():
|
do_restart = True
|
||||||
try:
|
|
||||||
args = [sys.executable] + [os.path.join(base_path, __file__)] + sys.argv[1:]
|
|
||||||
new_environ = os.environ.copy()
|
|
||||||
new_environ['cp_main'] = 'true'
|
|
||||||
|
|
||||||
if os.name == 'nt':
|
def __init__(self):
|
||||||
for key, value in new_environ.iteritems():
|
|
||||||
if isinstance(value, unicode):
|
|
||||||
new_environ[key] = value.encode('iso-8859-1')
|
|
||||||
|
|
||||||
subprocess.call(args, env = new_environ)
|
from couchpotato.core.logger import CPLog
|
||||||
return os.path.isfile(os.path.join(data_dir, 'restart'))
|
self.log = CPLog(__name__)
|
||||||
except KeyboardInterrupt, e:
|
|
||||||
pass
|
# Get options via arg
|
||||||
except Exception, e:
|
from couchpotato.runner import getOptions
|
||||||
log.critical(e)
|
from couchpotato.core.helpers.variable import getDataDir
|
||||||
return 0
|
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:
|
try:
|
||||||
runCouchPotato(options, base_path, sys.argv[1:])
|
from couchpotato.runner import runCouchPotato
|
||||||
|
runCouchPotato(self.options, base_path, sys.argv[1:])
|
||||||
except Exception, e:
|
except Exception, e:
|
||||||
log.critical(e)
|
self.log.critical(e)
|
||||||
else:
|
|
||||||
while 1:
|
|
||||||
restart = start()
|
|
||||||
if not restart:
|
|
||||||
break
|
|
||||||
|
|
||||||
from couchpotato.core.event import fireEvent
|
if self.do_restart:
|
||||||
fireEvent('app.crappy_shutdown', single = True)
|
self.restart()
|
||||||
time.sleep(1)
|
|
||||||
|
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__':
|
if __name__ == '__main__':
|
||||||
|
l = Loader()
|
||||||
signal(SIGTERM, lambda signum, stack_frame: sys.exit(1))
|
l.daemonize()
|
||||||
|
l.run()
|
||||||
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()
|
|
||||||
@@ -8,7 +8,6 @@ from couchpotato.environment import Env
|
|||||||
from flask import request
|
from flask import request
|
||||||
from uuid import uuid4
|
from uuid import uuid4
|
||||||
import os
|
import os
|
||||||
import thread
|
|
||||||
import time
|
import time
|
||||||
import traceback
|
import traceback
|
||||||
import webbrowser
|
import webbrowser
|
||||||
@@ -32,7 +31,6 @@ class Core(Plugin):
|
|||||||
addEvent('app.crappy_shutdown', self.crappyShutdown)
|
addEvent('app.crappy_shutdown', self.crappyShutdown)
|
||||||
addEvent('app.crappy_restart', self.crappyRestart)
|
addEvent('app.crappy_restart', self.crappyRestart)
|
||||||
addEvent('app.load', self.launchBrowser, priority = 1)
|
addEvent('app.load', self.launchBrowser, priority = 1)
|
||||||
addEvent('app.load', self.monitorParent)
|
|
||||||
addEvent('app.base_url', self.createBaseUrl)
|
addEvent('app.base_url', self.createBaseUrl)
|
||||||
addEvent('app.api_url', self.createApiUrl)
|
addEvent('app.api_url', self.createApiUrl)
|
||||||
|
|
||||||
@@ -41,27 +39,6 @@ class Core(Plugin):
|
|||||||
|
|
||||||
self.removeRestartFile()
|
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):
|
def md5Password(self, value):
|
||||||
return md5(value) if value else ''
|
return md5(value) if value else ''
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user