MCPcopy Index your code
hub / github.com/Bitmessage/PyBitmessage / doCleanShutdown

Function doCleanShutdown

src/shutdown.py:15–69  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

13import state
14
15def doCleanShutdown():
16 state.shutdown = 1 #Used to tell proof of work worker threads and the objectProcessorThread to exit.
17 objectProcessorQueue.put(('checkShutdownVariable', 'no data'))
18 for thread in threading.enumerate():
19 if thread.isAlive() and isinstance(thread, StoppableThread):
20 thread.stopThread()
21
22 UISignalQueue.put(('updateStatusBar','Saving the knownNodes list of peers to disk...'))
23 logger.info('Saving knownNodes list of peers to disk')
24 saveKnownNodes()
25 logger.info('Done saving knownNodes list of peers to disk')
26 UISignalQueue.put(('updateStatusBar','Done saving the knownNodes list of peers to disk.'))
27 logger.info('Flushing inventory in memory out to disk...')
28 UISignalQueue.put((
29 'updateStatusBar',
30 'Flushing inventory in memory out to disk. This should normally only take a second...'))
31 Inventory().flush()
32
33 # Verify that the objectProcessor has finished exiting. It should have incremented the
34 # shutdown variable from 1 to 2. This must finish before we command the sqlThread to exit.
35 while state.shutdown == 1:
36 time.sleep(.1)
37
38 # This one last useless query will guarantee that the previous flush committed and that the
39 # objectProcessorThread committed before we close the program.
40 sqlQuery('SELECT address FROM subscriptions')
41 logger.info('Finished flushing inventory.')
42 sqlStoredProcedure('exit')
43
44 # Wait long enough to guarantee that any running proof of work worker threads will check the
45 # shutdown variable and exit. If the main thread closes before they do then they won't stop.
46 time.sleep(.25)
47
48 for thread in threading.enumerate():
49 if thread is not threading.currentThread() and isinstance(thread, StoppableThread):
50 logger.debug("Waiting for thread %s", thread.name)
51 thread.join()
52
53 # flush queued
54 for queue in (workerQueue, UISignalQueue, addressGeneratorQueue, objectProcessorQueue):
55 while True:
56 try:
57 queue.get(False)
58 queue.task_done()
59 except Queue.Empty:
60 break
61
62 if shared.thisapp.daemon:
63 logger.info('Clean shutdown complete.')
64 shared.thisapp.cleanup()
65 os._exit(0)
66 else:
67 logger.info('Core shutdown complete.')
68 for thread in threading.enumerate():
69 logger.debug("Thread %s still running", thread.name)

Callers

nothing calls this directly

Calls 10

saveKnownNodesFunction · 0.90
InventoryClass · 0.90
sqlQueryFunction · 0.90
sqlStoredProcedureFunction · 0.90
putMethod · 0.80
task_doneMethod · 0.80
cleanupMethod · 0.80
stopThreadMethod · 0.45
flushMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected