controller: Improve shutdown handling
This commit is contained in:
		@@ -1,6 +1,7 @@
 | 
				
			|||||||
import configparser
 | 
					import configparser
 | 
				
			||||||
import logging
 | 
					import logging
 | 
				
			||||||
import os
 | 
					import os
 | 
				
			||||||
 | 
					import signal
 | 
				
			||||||
import sys
 | 
					import sys
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import infi.systray
 | 
					import infi.systray
 | 
				
			||||||
@@ -51,7 +52,7 @@ def main():
 | 
				
			|||||||
  conf.read(CONFIG_FILE)
 | 
					  conf.read(CONFIG_FILE)
 | 
				
			||||||
  with open(CONFIG_FILE, 'w') as f:
 | 
					  with open(CONFIG_FILE, 'w') as f:
 | 
				
			||||||
    conf.write(f)
 | 
					    conf.write(f)
 | 
				
			||||||
  conf.getboolean('general', 'dummy_impl')
 | 
					  use_dummy_impl = conf.getboolean('general', 'dummy_impl')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  log_level = parse_log_level(conf['general']['log_level'])
 | 
					  log_level = parse_log_level(conf['general']['log_level'])
 | 
				
			||||||
  logging.basicConfig(
 | 
					  logging.basicConfig(
 | 
				
			||||||
@@ -71,10 +72,10 @@ def main():
 | 
				
			|||||||
  val_sm = valconomy.ValconomyStateMachine(conf['valorant']['player_uuid'], val_handler)
 | 
					  val_sm = valconomy.ValconomyStateMachine(conf['valorant']['player_uuid'], val_handler)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  val_client = valconomy.ValorantLocalClient(val_sm.handle_presence)
 | 
					  val_client = valconomy.ValorantLocalClient(val_sm.handle_presence)
 | 
				
			||||||
  def do_quit(tray: infi.systray.SysTrayIcon):
 | 
					  def do_quit(*args):
 | 
				
			||||||
    log.info('Shutting down')
 | 
					    log.info('Shutting down')
 | 
				
			||||||
    val_client.stop()
 | 
					    val_client.stop()
 | 
				
			||||||
    if use_dummy_impl:
 | 
					    if not use_dummy_impl:
 | 
				
			||||||
      val_handler.stop()
 | 
					      val_handler.stop()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  with infi.systray.SysTrayIcon(
 | 
					  with infi.systray.SysTrayIcon(
 | 
				
			||||||
@@ -82,6 +83,8 @@ def main():
 | 
				
			|||||||
      menu_options=(
 | 
					      menu_options=(
 | 
				
			||||||
        ('Open data directory', None, do_open_datadir),
 | 
					        ('Open data directory', None, do_open_datadir),
 | 
				
			||||||
      )) as tray:
 | 
					      )) as tray:
 | 
				
			||||||
 | 
					    signal.signal(signal.SIGINT, do_quit)
 | 
				
			||||||
 | 
					    signal.signal(signal.SIGTERM, do_quit)
 | 
				
			||||||
    val_client.run()
 | 
					    val_client.run()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
if __name__ == '__main__':
 | 
					if __name__ == '__main__':
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -287,7 +287,7 @@ class HIDValconomyHandler(ValconomyHandler):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
      while not self._dev_ready():
 | 
					      while not self._dev_ready():
 | 
				
			||||||
        if not self.running:
 | 
					        if not self.running:
 | 
				
			||||||
          break
 | 
					          return
 | 
				
			||||||
        time.sleep(0.1)
 | 
					        time.sleep(0.1)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      try:
 | 
					      try:
 | 
				
			||||||
@@ -298,6 +298,8 @@ class HIDValconomyHandler(ValconomyHandler):
 | 
				
			|||||||
  def run(self):
 | 
					  def run(self):
 | 
				
			||||||
    while self.running:
 | 
					    while self.running:
 | 
				
			||||||
      while self.queue.empty():
 | 
					      while self.queue.empty():
 | 
				
			||||||
 | 
					        if not self.running:
 | 
				
			||||||
 | 
					          return
 | 
				
			||||||
        time.sleep(0.5)
 | 
					        time.sleep(0.5)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      self.service()
 | 
					      self.service()
 | 
				
			||||||
@@ -308,7 +310,9 @@ class HIDValconomyHandler(ValconomyHandler):
 | 
				
			|||||||
    self._thread.start()
 | 
					    self._thread.start()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  def stop(self):
 | 
					  def stop(self):
 | 
				
			||||||
    assert self._thread is not None and self.running
 | 
					    if self._thread is None or not self.running:
 | 
				
			||||||
 | 
					      return
 | 
				
			||||||
 | 
					    self.running = False
 | 
				
			||||||
    self._thread.join()
 | 
					    self._thread.join()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  def none(self):
 | 
					  def none(self):
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user