From 117f306aa65d221ae2d78471969018f1e6f6f395 Mon Sep 17 00:00:00 2001 From: jarbasai Date: Wed, 16 Mar 2022 22:39:14 +0000 Subject: [PATCH] refator/process_utils from ovos_utils --- mycroft/util/process_utils.py | 153 +--------------------------------- requirements/minimal.txt | 2 +- requirements/requirements.txt | 2 +- 3 files changed, 6 insertions(+), 151 deletions(-) diff --git a/mycroft/util/process_utils.py b/mycroft/util/process_utils.py index 532996ba3746..79ae86d10298 100644 --- a/mycroft/util/process_utils.py +++ b/mycroft/util/process_utils.py @@ -1,16 +1,13 @@ -from collections import namedtuple -from enum import IntEnum import json import logging import signal as sig -import sys -from threading import Event, Thread -from time import sleep - +from threading import Event from mycroft.util.log import LOG - from ovos_utils import create_daemon, wait_for_exit_signal +# backwards compat imports, DO NOT DELETE, careful with autopep8 +from ovos_utils.process_utils import ProcessState, ProcessStatus, StatusCallbackMap + def reset_sigint_handler(): """Reset the sigint handler to the default. @@ -141,145 +138,3 @@ def start_message_bus_client(service, bus=None, whitelist=None): LOG.info('Connected to messagebus') return bus - - -class ProcessState(IntEnum): - """Oredered enum to make state checks easy. - - For example Alive can be determined using >= ProcessState.ALIVE, - which will return True if the state is READY as well as ALIVE. - """ - NOT_STARTED = 0 - STARTED = 1 - ERROR = 2 - STOPPING = 3 - ALIVE = 4 - READY = 5 - - -# Process state change callback mappings. -_STATUS_CALLBACKS = [ - 'on_started', - 'on_alive', - 'on_ready', - 'on_error', - 'on_stopping', -] - -# namedtuple defaults only available on 3.7 and later python versions -if sys.version_info < (3, 7): - StatusCallbackMap = namedtuple('CallbackMap', _STATUS_CALLBACKS) - StatusCallbackMap.__new__.__defaults__ = (None,) * 5 -else: - StatusCallbackMap = namedtuple( - 'CallbackMap', - _STATUS_CALLBACKS, - defaults=(None,) * len(_STATUS_CALLBACKS), - ) - - -class ProcessStatus: - """Process status tracker. - - The class tracks process status and execute callback methods on - state changes as well as replies to messagebus queries of the - process status. - - Args: - name (str): process name, will be used to create the messagebus - messagetype "mycroft.{name}...". - bus (MessageBusClient): Connection to the Mycroft messagebus. - callback_map (StatusCallbackMap): optionally, status callbacks for the - various status changes. - """ - - def __init__(self, name, bus=None, callback_map=None): - self.name = name - self.callbacks = callback_map or StatusCallbackMap() - self.state = ProcessState.NOT_STARTED - - # Messagebus connection - self.bus = None - if bus: - self.bind(bus) - - def bind(self, bus): - self.bus = bus - self._register_handlers() - - def _register_handlers(self): - """Register messagebus handlers for status queries.""" - self.bus.on('mycroft.{}.is_alive'.format(self.name), self.check_alive) - self.bus.on('mycroft.{}.is_ready'.format(self.name), - self.check_ready) - # The next one is for backwards compatibility - # TODO: remove in 21.02 - self.bus.on( - 'mycroft.{}.all_loaded'.format(self.name), self.check_ready - ) - - def check_alive(self, message=None): - """Respond to is_alive status request. - - Args: - message: Optional message to respond to, if omitted no message - is sent. - - Returns: - bool, True if process is alive. - """ - is_alive = self.state >= ProcessState.ALIVE - - if message: - status = {'status': is_alive} - self.bus.emit(message.response(data=status)) - - return is_alive - - def check_ready(self, message=None): - """Respond to all_loaded status request. - - Args: - message: Optional message to respond to, if omitted no message - is sent. - - Returns: - bool, True if process is ready. - """ - is_ready = self.state >= ProcessState.READY - if message: - status = {'status': is_ready} - self.bus.emit(message.response(data=status)) - - return is_ready - - def set_started(self): - """Process is started.""" - self.state = ProcessState.STARTED - if self.callbacks.on_started: - self.callbacks.on_started() - - def set_alive(self): - """Basic loading is done.""" - self.state = ProcessState.ALIVE - if self.callbacks.on_alive: - self.callbacks.on_alive() - - def set_ready(self): - """All loading is done.""" - self.state = ProcessState.READY - if self.callbacks.on_ready: - self.callbacks.on_ready() - - def set_stopping(self): - """Process shutdown has started.""" - self.state = ProcessState.STOPPING - if self.callbacks.on_stopping: - self.callbacks.on_stopping() - - def set_error(self, err=''): - """An error has occured and the process is non-functional.""" - # Intentionally leave is_started True - self.state = ProcessState.ERROR - if self.callbacks.on_error: - self.callbacks.on_error(err) diff --git a/requirements/minimal.txt b/requirements/minimal.txt index 6e1c6856a272..49286b4f30da 100644 --- a/requirements/minimal.txt +++ b/requirements/minimal.txt @@ -3,5 +3,5 @@ pyee~=8.1 mycroft-messagebus-client~=0.9.1,!=0.9.2,!=0.9.3 psutil~=5.6.6 combo-lock~=0.2 -ovos-utils~=0.0.18 +ovos-utils~=0.0.20a2 ovos-plugin-manager~=0.0.11a1 \ No newline at end of file diff --git a/requirements/requirements.txt b/requirements/requirements.txt index d0116c7203a8..642599582455 100644 --- a/requirements/requirements.txt +++ b/requirements/requirements.txt @@ -8,7 +8,7 @@ python-dateutil~=2.6.0 combo-lock~=0.2 PyYAML~=5.4 -ovos-utils~=0.0.18 +ovos-utils~=0.0.20a2 ovos-plugin-manager~=0.0.11a1 ovos-tts-plugin-mimic>=0.2.6 ovos-tts-plugin-mimic2>=0.1.4