2019-10-22 16:24:04 +02:00
|
|
|
"""shutdown function"""
|
2017-02-08 13:41:56 +01:00
|
|
|
import os
|
2019-09-26 16:08:19 +02:00
|
|
|
import queue as Queue
|
2017-02-08 13:41:56 +01:00
|
|
|
import threading
|
|
|
|
import time
|
|
|
|
|
2019-08-06 13:04:33 +02:00
|
|
|
import shared
|
|
|
|
import state
|
2017-02-08 13:41:56 +01:00
|
|
|
from debug import logger
|
|
|
|
from helper_sql import sqlQuery, sqlStoredProcedure
|
|
|
|
from inventory import Inventory
|
2019-08-06 13:04:33 +02:00
|
|
|
from knownnodes import saveKnownNodes
|
2019-10-27 14:15:45 +01:00
|
|
|
from network import StoppableThread
|
2017-09-27 17:25:14 +02:00
|
|
|
from queues import (
|
|
|
|
addressGeneratorQueue, objectProcessorQueue, UISignalQueue, workerQueue)
|
2017-02-08 13:41:56 +01:00
|
|
|
|
2017-09-27 17:25:14 +02:00
|
|
|
|
2017-02-08 13:41:56 +01:00
|
|
|
def doCleanShutdown():
|
2019-11-04 12:30:11 +01:00
|
|
|
"""
|
|
|
|
Used to tell all the treads to finish work and exit.
|
|
|
|
"""
|
2017-09-27 17:25:14 +02:00
|
|
|
state.shutdown = 1
|
|
|
|
|
2017-02-08 13:41:56 +01:00
|
|
|
objectProcessorQueue.put(('checkShutdownVariable', 'no data'))
|
|
|
|
for thread in threading.enumerate():
|
|
|
|
if thread.isAlive() and isinstance(thread, StoppableThread):
|
|
|
|
thread.stopThread()
|
2017-09-27 17:25:14 +02:00
|
|
|
|
|
|
|
UISignalQueue.put((
|
|
|
|
'updateStatusBar',
|
|
|
|
'Saving the knownNodes list of peers to disk...'))
|
2017-02-09 11:53:33 +01:00
|
|
|
logger.info('Saving knownNodes list of peers to disk')
|
|
|
|
saveKnownNodes()
|
|
|
|
logger.info('Done saving knownNodes list of peers to disk')
|
2017-09-27 17:25:14 +02:00
|
|
|
UISignalQueue.put((
|
|
|
|
'updateStatusBar',
|
|
|
|
'Done saving the knownNodes list of peers to disk.'))
|
2017-02-08 13:41:56 +01:00
|
|
|
logger.info('Flushing inventory in memory out to disk...')
|
|
|
|
UISignalQueue.put((
|
|
|
|
'updateStatusBar',
|
2017-09-27 17:25:14 +02:00
|
|
|
'Flushing inventory in memory out to disk.'
|
|
|
|
' This should normally only take a second...'))
|
2017-02-08 13:41:56 +01:00
|
|
|
Inventory().flush()
|
|
|
|
|
2017-09-27 17:25:14 +02:00
|
|
|
# Verify that the objectProcessor has finished exiting. It should have
|
|
|
|
# incremented the shutdown variable from 1 to 2. This must finish before
|
|
|
|
# we command the sqlThread to exit.
|
2017-02-08 13:41:56 +01:00
|
|
|
while state.shutdown == 1:
|
|
|
|
time.sleep(.1)
|
2017-09-27 17:25:14 +02:00
|
|
|
|
|
|
|
# Wait long enough to guarantee that any running proof of work worker
|
|
|
|
# threads will check the shutdown variable and exit. If the main thread
|
|
|
|
# closes before they do then they won't stop.
|
2017-02-08 13:41:56 +01:00
|
|
|
time.sleep(.25)
|
|
|
|
|
|
|
|
for thread in threading.enumerate():
|
2019-11-04 12:30:11 +01:00
|
|
|
if (
|
|
|
|
thread is not threading.currentThread()
|
|
|
|
and isinstance(thread, StoppableThread)
|
|
|
|
and thread.name != 'SQL'
|
|
|
|
):
|
2017-02-08 13:41:56 +01:00
|
|
|
logger.debug("Waiting for thread %s", thread.name)
|
|
|
|
thread.join()
|
|
|
|
|
2017-09-27 17:25:14 +02:00
|
|
|
# This one last useless query will guarantee that the previous flush
|
|
|
|
# committed and that the
|
2018-04-05 12:36:02 +02:00
|
|
|
# objectProcessorThread committed before we close the program.
|
|
|
|
sqlQuery('SELECT address FROM subscriptions')
|
|
|
|
logger.info('Finished flushing inventory.')
|
|
|
|
sqlStoredProcedure('exit')
|
|
|
|
|
|
|
|
# flush queues
|
2017-09-27 17:25:14 +02:00
|
|
|
for queue in (
|
|
|
|
workerQueue, UISignalQueue, addressGeneratorQueue,
|
|
|
|
objectProcessorQueue):
|
2017-02-08 13:41:56 +01:00
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
queue.get(False)
|
|
|
|
queue.task_done()
|
|
|
|
except Queue.Empty:
|
|
|
|
break
|
|
|
|
|
2019-10-22 16:24:04 +02:00
|
|
|
if shared.thisapp.daemon or not state.enableGUI: # ..fixme:: redundant?
|
2017-02-08 13:41:56 +01:00
|
|
|
logger.info('Clean shutdown complete.')
|
|
|
|
shared.thisapp.cleanup()
|
2019-10-22 16:24:04 +02:00
|
|
|
os._exit(0) # pylint: disable=protected-access
|
2017-02-08 13:41:56 +01:00
|
|
|
else:
|
|
|
|
logger.info('Core shutdown complete.')
|
|
|
|
for thread in threading.enumerate():
|
2017-09-27 17:25:14 +02:00
|
|
|
logger.debug('Thread %s still running', thread.name)
|