Do proper shutdown
This commit is contained in:
1 parent
b7d5f167bb
commit
55cb53b34d
3 files changed
+26
-19
No files matched your search
@@ -4,6 +4,7 @@ from signal import signal, SIGTERM
|
|||||||
import os
|
import os
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
|
import time
|
||||||
|
|
||||||
|
|
||||||
# Root path
|
# Root path
|
||||||
@@ -53,6 +54,10 @@ def main():
|
|||||||
if not restart:
|
if not restart:
|
||||||
break
|
break
|
||||||
|
|
||||||
|
from couchpotato.core.event import fireEvent
|
||||||
|
fireEvent('app.crappy_shutdown', single = True)
|
||||||
|
time.sleep(1)
|
||||||
|
|
||||||
sys.exit()
|
sys.exit()
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|||||||
@@ -60,8 +60,6 @@ class Core(Plugin):
|
|||||||
})
|
})
|
||||||
|
|
||||||
def crappyShutdown(self):
|
def crappyShutdown(self):
|
||||||
if self.shutdown_started: return
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.urlopen('%sapp.shutdown' % self.createApiUrl(), show_error = False)
|
self.urlopen('%sapp.shutdown' % self.createApiUrl(), show_error = False)
|
||||||
return True
|
return True
|
||||||
@@ -70,8 +68,6 @@ class Core(Plugin):
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
def crappyRestart(self):
|
def crappyRestart(self):
|
||||||
if self.shutdown_started: return
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.urlopen('%sapp.restart' % self.createApiUrl(), show_error = False)
|
self.urlopen('%sapp.restart' % self.createApiUrl(), show_error = False)
|
||||||
return True
|
return True
|
||||||
@@ -88,26 +84,30 @@ class Core(Plugin):
|
|||||||
return 'restarting'
|
return 'restarting'
|
||||||
|
|
||||||
def initShutdown(self, restart = False):
|
def initShutdown(self, restart = False):
|
||||||
|
if self.shutdown_started:
|
||||||
|
log.info('Already shutting down')
|
||||||
|
|
||||||
log.info('Shutting down' if not restart else 'Restarting')
|
log.info('Shutting down' if not restart else 'Restarting')
|
||||||
|
|
||||||
self.shutdown_started = True
|
self.shutdown_started = True
|
||||||
|
|
||||||
fireEvent('app.shutdown')
|
fireEvent('app.shutdown')
|
||||||
log.debug('Every plugin got shutdown event')
|
log.debug('Every plugin got shutdown event')
|
||||||
|
|
||||||
loop = True
|
loop = True
|
||||||
while loop:
|
while loop:
|
||||||
log.debug('Asking who is running')
|
log.debug('Asking who is running')
|
||||||
still_running = fireEvent('plugin.running')
|
still_running = fireEvent('plugin.running', merge = True)
|
||||||
log.debug('Still running: %s' % still_running)
|
log.debug('Still running: %s' % still_running)
|
||||||
|
|
||||||
if len(still_running) == 0:
|
if len(still_running) == 0:
|
||||||
break
|
break
|
||||||
|
|
||||||
for running in still_running:
|
running = list(set(still_running) - set(self.ignore_restart))
|
||||||
running = list(set(running) - set(self.ignore_restart))
|
if len(running) > 0:
|
||||||
if len(running) > 0:
|
log.info('Waiting on plugins to finish: %s' % running)
|
||||||
log.info('Waiting on plugins to finish: %s' % running)
|
else:
|
||||||
else:
|
loop = False
|
||||||
loop = False
|
|
||||||
|
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
|
|
||||||
|
|||||||
@@ -25,8 +25,7 @@ class Plugin(object):
|
|||||||
enabled_option = 'enabled'
|
enabled_option = 'enabled'
|
||||||
auto_register_static = True
|
auto_register_static = True
|
||||||
|
|
||||||
needs_shutdown = False
|
_needs_shutdown = False
|
||||||
running = []
|
|
||||||
|
|
||||||
http_last_use = {}
|
http_last_use = {}
|
||||||
http_time_between_calls = 0
|
http_time_between_calls = 0
|
||||||
@@ -143,7 +142,6 @@ class Plugin(object):
|
|||||||
time.sleep(last_use - now + self.http_time_between_calls)
|
time.sleep(last_use - now + self.http_time_between_calls)
|
||||||
|
|
||||||
def beforeCall(self, handler):
|
def beforeCall(self, handler):
|
||||||
#log.debug('Calling %s.%s' % (self.getName(), handler.__name__))
|
|
||||||
self.isRunning('%s.%s' % (self.getName(), handler.__name__))
|
self.isRunning('%s.%s' % (self.getName(), handler.__name__))
|
||||||
|
|
||||||
def afterCall(self, handler):
|
def afterCall(self, handler):
|
||||||
@@ -154,19 +152,23 @@ class Plugin(object):
|
|||||||
|
|
||||||
def shuttingDown(self, value = None):
|
def shuttingDown(self, value = None):
|
||||||
if value is None:
|
if value is None:
|
||||||
return self.needs_shutdown
|
return self._needs_shutdown
|
||||||
|
|
||||||
self.needs_shutdown = value
|
self._needs_shutdown = value
|
||||||
|
|
||||||
def isRunning(self, value = None, boolean = True):
|
def isRunning(self, value = None, boolean = True):
|
||||||
|
|
||||||
|
if not hasattr(self, '_running'):
|
||||||
|
self._running = []
|
||||||
|
|
||||||
if value is None:
|
if value is None:
|
||||||
return self.running
|
return self._running
|
||||||
|
|
||||||
if boolean:
|
if boolean:
|
||||||
self.running.append(value)
|
self._running.append(value)
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
self.running.remove(value)
|
self._running.remove(value)
|
||||||
except:
|
except:
|
||||||
log.error("Something went wrong when finishing the plugin function. Could not find the 'is_running' key")
|
log.error("Something went wrong when finishing the plugin function. Could not find the 'is_running' key")
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user