Skip to content

Commit

Permalink
core: log stop when restarting in interactive, foreground or debug mo…
Browse files Browse the repository at this point in the history
…de, see #493
  • Loading branch information
bmxp committed Oct 31, 2022
1 parent 63d870c commit 2e09382
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions lib/smarthome.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
# along with SmartHomeNG. If not, see <http://www.gnu.org/licenses/>.
#########################################################################

__docformat__ = 'reStructuredText'

#########################################################################
#
Expand Down Expand Up @@ -112,7 +113,7 @@
class SmartHome():
"""
SmartHome ist the main class of SmartHomeNG. All other objects can be addressed relative to
the main oject, which is an instance of this class. Mostly it is reffered to as ``sh``, ``_sh`` or ``smarthome``.
the main oject, which is an instance of this class. Mostly it is referred to as ``sh``, ``_sh`` or ``smarthome``.
"""

# default values, if values are not specified in smarthome.yaml
Expand Down Expand Up @@ -681,7 +682,7 @@ def stop(self, signum=None, frame=None):
self.shng_status = {'code': 31, 'text': 'Stopping'}

self.alive = False
self._logger.info("stop: Number of Threads: {}".format(threading.activeCount()))
self._logger.info(f"stop: Number of Threads: {threading.activeCount()}")

self.items.stop()
self.scheduler.stop()
Expand Down Expand Up @@ -732,6 +733,9 @@ def restart(self, source=''):
just quit and let the user restart manually.
"""
if self._mode in ['foreground', 'debug', 'interactive']:
if source != '':
source = ', initiated by ' + source
self._logger_main.notice(f"-------------------- SmartHomeNG should restart{source} but is in {self._mode} mode and thus will just try to stop --------------------")
self.stop()

if self.shng_status['code'] == 30:
Expand All @@ -740,22 +744,21 @@ def restart(self, source=''):
self.shng_status = {'code': 30, 'text': 'Restarting'}
if source != '':
source = ', initiated by ' + source
self._logger_main.notice("-------------------- SmartHomeNG restarting" + source + " --------------------")
self._logger_main.notice(f"-------------------- SmartHomeNG restarting{source} --------------------")
# python_bin could contain spaces (at least on windows)
python_bin = sys.executable
if ' ' in python_bin:
python_bin = '"'+python_bin+'"'
python_bin = f'"{python_bin}"'
command = python_bin + ' ' + os.path.join(self._base_dir, 'bin', 'smarthome.py') + ' -r'
self._logger.info("Restart command = '{}'".format(command))
self._logger.info(f"Restart command = '{command}'")
try:
p = subprocess.Popen(command, shell=True)
exit(5) # exit code 5 -> for systemctl to restart SmartHomeNG
except subprocess.SubprocessError as e:
self._logger.error("Restart command '{}' failed with error {}".format(command,e))
self._logger.error(f"Restart command '{command}' failed with error {e}")


def list_threads(self, txt):

cp_threads = 0
http_threads = 0
for thread in threading.enumerate():
Expand Down

0 comments on commit 2e09382

Please sign in to comment.