def __init__(self,
              adjustments=DEFAULT_ADJUSTMENTS,
              children=None,
              expected=DEFAULT_EXPECTED,
              logname=None,
              name="supervisor",
              strategy=DEFAULT_STRATEGY,
              window=DEFAULT_WINDOW,
              **kwargs):
     """
     Constructor.
     :param name: the supervisor name
     :param expected: the expected status (running|stopped)
     :param window: the window of adjustments cycles to consider
     :param adjustments: the maximum number of adjustments allowed
     :param strategy: the strategy to be applied
     :param children: the list of children attached to the supervisor
     :param kwargs: extra parameters
     """
     if children is None:
         children = dict()
     if logname is None:
         logname = self.__class__.__name__
     self.logname = logname
     self.logger = logging.getLogger(logname)
     self.name = name
     self._expected = expected.lower()
     self._window = get_int_or_die(
         window,
         "supervisor %s should have a valid integer window value: %s" %
         (name, window))
     self._adjustments = get_int_or_die(
         adjustments,
         "supervisor %s should have a valid integer adjustments value: %s" %
         (name, adjustments))
     if strategy not in ALLOWED_STRATEGIES:
         raise ValueError(
             "supervisor %s does not support given strategy: %s" %
             (name, strategy, ))
     self._strategy_name = strategy
     self._strategy = Supervisor._strategy_by_name(
         ALLOWED_STRATEGIES[strategy])(self)
     for key in kwargs.keys():
         if not key.startswith("var_"):
             raise SimplevisorError(
                 "an invalid property has been specified for"
                 "supervisor %s: %s" %
                 (name, key))
     self._children = list()
     self._children_by_id = dict()
     self._children_by_name = dict()
     self.add_child_set(children.get("entry", list()))
     if len(self._children) == 0:
         raise SimplevisorError(
             "empty supervisor found: %s" % (self.name, ))
     self._cycles = list()
     self._is_new = True
 def sleep_interval(self):
     """ Return the interval between every supervision cycle. """
     value = self._config.get("interval", DEFAULT_INTERVAL)
     result = get_int_or_die(
         value,
         "interval value must be an integer: %s" % (value, ))
     return result
 def __init__(self, name, expected=DEFAULT_EXPECTED,
              timeout=DEFAULT_TIMEOUT,
              control=None, daemon=None, path=None, pattern=None,
              restart=None, start=None, status=None, stop=None,
              **kwargs):
     """ Service constructor. """
     self.name = name
     self._opts = {
         "name": name,
         "expected": expected,
         "control": control,
         "daemon": daemon,
         "path": path,
         "pattern": pattern,
         "restart": restart,
         "start": start,
         "status": status,
         "stop": stop,
         "timeout": get_int_or_die(
             timeout,
             "timeout value for %s is not a valid integer: %s" %
             (name, timeout)), }
     self._status = {
         "name": name,
         "log": list(), }
     for key in kwargs:
         if not key.startswith("var_"):
             raise ValueError(
                 "invalid property for service %s: %s" % (name, key))
     self._is_new = True
     try:
         self._validate_opts()
     except ValueError:
         error = sys.exc_info()[1]
         raise ValueError(
             "service %s is not properly configured: %s" %
             (self.name, error))
     if control is None and daemon is not None:
         si_loop = which("simplevisor-loop")
         if si_loop is None:
             raise RuntimeError(
                 "cannot find simplevisor-loop command in the "
                 "environment: %s" % (os.environ["PATH"], ))
         common_path = "%s --pidfile %s" % (si_loop, daemon, )
         self._opts["start"] = (
             "%s -c 1 --daemon %s" % (common_path, start))
         self._opts["stop"] = ("%s --quit" % (common_path, ))
         self._opts["status"] = ("%s --status" % (common_path, ))
     elif control is None and status is None:
         if sys.platform != "linux2":
             raise ValueError(
                 "don't know how to read process table, you must specify "
                 "a status command for service: %s" % (name, ))
         if pattern is not None:
             pat = pattern
         else:
             pat = " ".join(self.get_cmd("start"))
         try:
             self._opts["pattern_re"] = re.compile(pat)
             LOGGER.debug(
                 "using %s as pattern for service %s" % (pat, name))
         except re.error:
             error = sys.exc_info()[1]
             raise ValueError(
                 "%s service pattern not valid: %s" % (name, error))