| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- import threading
- import collections
- from enum import Enum, auto
- from typing import Callable, Type, Any, Dict
- from core_types import GroupConfig, GroupStatus
- from log_macros import VSC_INFO
- class State(Enum):
- CREATED = auto()
- LOADED = auto()
- RUNNING = auto()
- STOPPING = auto()
- STOPPED = auto()
- FAILED = auto()
- class GCOWrapper:
- def __init__(self, gco_class: Type[Any], gco_cfg: GroupConfig, redis_conf: Dict[str, Any]):
- """
- @param gco_class: Specific GCO class to wrap (SentinelGCO or BookerGCO)
- @param gco_cfg: Group configuration
- @param redis_conf: Redis connection configuration dictionary
- """
- self.gco_class = gco_class
- self.m_cfg = gco_cfg
- self.redis_conf = redis_conf
-
- self._gco = None
- self._state = State.CREATED
- self._log_queue = collections.deque(maxlen=1000)
- self._log_subscribers = set()
- self._lock = threading.Lock()
- self._push_callback: Callable = lambda t, d, s: None
-
- def set_push_callback(self, callback: Callable):
- self._push_callback = callback
-
- def _log(self, msg: str):
- gid = getattr(self.m_cfg, "identifier", "")
- with self._lock:
- VSC_INFO("-", msg, group_id=gid)
- self._log_queue.append(msg)
- for sub in self._log_subscribers:
- try:
- sub(msg)
- except Exception:
- pass
- # Legacy push callback
- self._push_callback("LOG", msg.encode(), self._state)
-
- def get_logs(self, last_n=50):
- with self._lock:
- return list(self._log_queue)[-last_n:]
- def subscribe_logs(self, callback: Callable):
- with self._lock:
- self._log_subscribers.add(callback)
- def unsubscribe_logs(self, callback: Callable):
- with self._lock:
- self._log_subscribers.discard(callback)
- def _transition(self, from_state, to_state):
- if self._state != from_state:
- raise Exception(f"invalid transition {self._state.name} -> {to_state.name}")
- self._state = to_state
- def load(self):
- self._transition(State.CREATED, State.LOADED)
- def start(self):
- self._transition(State.LOADED, State.RUNNING)
- try:
- # Dynamically instantiate class with Redis configuration
- self._gco = self.gco_class(self.m_cfg, self.redis_conf, logger=self._log)
- self._gco.start() # Call Sentinel or Booker start
- except Exception:
- self._state = State.FAILED
- if self._gco:
- try:
- self._gco.stop()
- except Exception as cleanup_err:
- self._log(f'Startup rollback incomplete: {cleanup_err}')
- raise
- def stop(self):
- if self._state not in (State.RUNNING, State.FAILED, State.STOPPED):
- return
- self._state = State.STOPPING
- try:
- if self._gco:
- self._gco.stop() # Call Sentinel or Booker stop
- except Exception:
- self._state = State.FAILED
- raise
- else:
- self._state = State.STOPPED
-
- # ================= Hot Reloading Methods =================
- def update_config(self, new_cfg: GroupConfig):
- """
- Dynamically updates current Group configuration and propagates to underlying GCO instance.
- """
- with self._lock:
- self.m_cfg = new_cfg
-
- if self._gco:
- self._gco.update_config(new_cfg)
- def get_status(self) -> GroupStatus:
- """
- Returns aggregated status of current GCOWrapper and underlying GCO instance.
- """
- with self._lock:
- state_name = self._state.name
- if self._gco:
- status: GroupStatus = self._gco.get_status()
- status.wrapper_state = state_name
- return status
- return GroupStatus(
- group_id=self.m_cfg.identifier,
- enable=self.m_cfg.enable,
- plugin_name=self.m_cfg.plugin_config.plugin_name,
- wrapper_state=state_name,
- )
|