def __init__(self, **kwvals): for p in self.init_needed_keywords: if p not in kwvals.keys(): raise PIDanticUsageException( "The driver %s must have the parameter %s." % (self.driver_name, p)) for p in kwvals.keys(): if p not in self.init_needed_keywords and p not in self.init_optional_keywords: raise PIDanticUsageException( "The driver %s does not know the parameter %s." % (self.driver_name, p)) self._next_poll_time = datetime.datetime.now() self._poll_interval = 0.5 self._working_dir = kwvals['directory'] self._name = kwvals['name'] self._log = _set_param_or_default(kwvals, "log", logging) supd_exe = _set_param_or_default(kwvals, "supdexe", "%s/bin/supervisord" % sys.prefix) if not os.path.exists(supd_exe): raise PIDanticUsageException("No supervisord executable found") self._watched_processes = {} db_url = "sqlite:///%s/supd.db" % (self._working_dir) self._db = SupDDB(db_url) supd_db_a = self._db.get_all_supds() if len(supd_db_a) > 1: raise PIDanticUsageException( "The driver %s should have at most 1 supervisord process in its table." % (self.driver_name)) if not supd_db_a: template = os.path.join(self._working_dir, "supd.template") if not os.path.exists(template): template = None self._supd = SupD(self._db, name=self._name, template=template, log=self._log, executable=supd_exe, dirpath=self._working_dir) else: if supd_db_a[0].name != self._name: raise PIDanticUsageException( "The requested supd name %s is not in the db" % (self._name)) self._supd = SupD(self._db, data_object=supd_db_a[0]) self._is_alive()
def setUp(self): self.name = "test_something-" + str(uuid.uuid4()) if system() == "Darwin": # The standard mac tmp path is too long # for a socket self.dirpath = tempfile.mkdtemp(dir="/tmp") else: self.dirpath = tempfile.mkdtemp() self.supd_db_path = "sqlite:///" + os.path.join(self.dirpath, "sup.db") self.supd_db = SupDDB(self.supd_db_path) supd_path = "%s/bin/supervisord" % (sys.prefix) self.supd = SupD(self.supd_db, self.name, dirpath=self.dirpath, executable=supd_path)
def __init__(self, **kwvals): for p in self.init_needed_keywords: if p not in kwvals.keys(): raise PIDanticUsageException("The driver %s must have the parameter %s." % (self.driver_name, p)) for p in kwvals.keys(): if p not in self.init_needed_keywords and p not in self.init_optional_keywords: raise PIDanticUsageException("The driver %s does not know the parameter %s." % (self.driver_name, p)) self._next_poll_time = datetime.datetime.now() self._poll_interval = 0.5 self._working_dir = kwvals['directory'] self._name = kwvals['name'] self._log = _set_param_or_default(kwvals, "log", logging) supd_exe = _set_param_or_default(kwvals, "supdexe", "%s/bin/supervisord" % sys.prefix) if not os.path.exists(supd_exe): raise PIDanticUsageException("No supervisord executable found") self._watched_processes = {} db_url = "sqlite:///%s/supd.db" % (self._working_dir) self._db = SupDDB(db_url) supd_db_a = self._db.get_all_supds() if len(supd_db_a) > 1: raise PIDanticUsageException("The driver %s should have at most 1 supervisord process in its table." % (self.driver_name)) if not supd_db_a: template = os.path.join(self._working_dir, "supd.template") if not os.path.exists(template): template = None self._supd = SupD(self._db, name=self._name, template=template, log=self._log, executable=supd_exe, dirpath=self._working_dir) else: if supd_db_a[0].name != self._name: raise PIDanticUsageException("The requested supd name %s is not in the db" % (self._name)) self._supd = SupD(self._db, data_object=supd_db_a[0]) self._is_alive()
class SupDPidanticFactory(PidanticFactory): driver_name = "supervisord" init_needed_keywords = [ "directory", "name", ] init_optional_keywords = [ "log", "supdexe", ] run_needed_keywords = [ "command", "process_name", "directory", ] run_optional_keywords = [ "log", "autorestart", ] def __init__(self, **kwvals): for p in self.init_needed_keywords: if p not in kwvals.keys(): raise PIDanticUsageException( "The driver %s must have the parameter %s." % (self.driver_name, p)) for p in kwvals.keys(): if p not in self.init_needed_keywords and p not in self.init_optional_keywords: raise PIDanticUsageException( "The driver %s does not know the parameter %s." % (self.driver_name, p)) self._next_poll_time = datetime.datetime.now() self._poll_interval = 0.5 self._working_dir = kwvals['directory'] self._name = kwvals['name'] self._log = _set_param_or_default(kwvals, "log", logging) supd_exe = _set_param_or_default(kwvals, "supdexe", "%s/bin/supervisord" % sys.prefix) if not os.path.exists(supd_exe): raise PIDanticUsageException("No supervisord executable found") self._watched_processes = {} db_url = "sqlite:///%s/supd.db" % (self._working_dir) self._db = SupDDB(db_url) supd_db_a = self._db.get_all_supds() if len(supd_db_a) > 1: raise PIDanticUsageException( "The driver %s should have at most 1 supervisord process in its table." % (self.driver_name)) if not supd_db_a: template = os.path.join(self._working_dir, "supd.template") if not os.path.exists(template): template = None self._supd = SupD(self._db, name=self._name, template=template, log=self._log, executable=supd_exe, dirpath=self._working_dir) else: if supd_db_a[0].name != self._name: raise PIDanticUsageException( "The requested supd name %s is not in the db" % (self._name)) self._supd = SupD(self._db, data_object=supd_db_a[0]) self._is_alive() def _get_next_poll_time(self): self._next_poll_time = datetime.datetime.now() + datetime.timedelta( seconds=self._poll_interval) def _is_alive(self): self._supd.ping() def get_name(self): return self._name def get_pidantic(self, **kwvals): for p in self.run_needed_keywords: if p not in kwvals.keys(): raise PIDanticUsageException( "The driver %s must have the parameter %s." % (self.driver_name, p)) for p in kwvals.keys(): if p not in self.run_needed_keywords and p not in self.run_optional_keywords: raise PIDanticUsageException( "The driver %s does not know the parameter %s." % (self.driver_name, p)) program_object = self._supd.create_program_db(**kwvals) pidsupd = PIDanticSupD(program_object, self._supd, log=self._log) self._watched_processes[program_object.process_name] = pidsupd return pidsupd def reload_instances(self): self._watched_processes = {} data_obj = self._supd.get_data_object() for p in data_obj.programs: pidsupd = PIDanticSupD(p, self._supd, log=self._log) self._watched_processes[p.process_name] = pidsupd self.poll() return self._watched_processes def poll(self): all_state = self._supd.get_all_state() for state in all_state: name = state['name'] if name not in self._watched_processes.keys(): self._log.log( logging.ERROR, "Supervisord is reporting an unknown process %s" % (name)) pidsupd = self._watched_processes[name] pidsupd._process_state_change(state) def terminate(self): self._supd.terminate() self._supd.delete() def _backoff(self): self._poll_interval = self._poll_interval + (self._poll_interval * .5) self._get_next_poll_time()
class SupDPidanticFactory(PidanticFactory): driver_name = "supervisord" init_needed_keywords = [ "directory", "name", ] init_optional_keywords = [ "log", "supdexe", ] run_needed_keywords = [ "command", "process_name", "directory", ] run_optional_keywords = [ "log", "autorestart", ] def __init__(self, **kwvals): for p in self.init_needed_keywords: if p not in kwvals.keys(): raise PIDanticUsageException("The driver %s must have the parameter %s." % (self.driver_name, p)) for p in kwvals.keys(): if p not in self.init_needed_keywords and p not in self.init_optional_keywords: raise PIDanticUsageException("The driver %s does not know the parameter %s." % (self.driver_name, p)) self._next_poll_time = datetime.datetime.now() self._poll_interval = 0.5 self._working_dir = kwvals['directory'] self._name = kwvals['name'] self._log = _set_param_or_default(kwvals, "log", logging) supd_exe = _set_param_or_default(kwvals, "supdexe", "%s/bin/supervisord" % sys.prefix) if not os.path.exists(supd_exe): raise PIDanticUsageException("No supervisord executable found") self._watched_processes = {} db_url = "sqlite:///%s/supd.db" % (self._working_dir) self._db = SupDDB(db_url) supd_db_a = self._db.get_all_supds() if len(supd_db_a) > 1: raise PIDanticUsageException("The driver %s should have at most 1 supervisord process in its table." % (self.driver_name)) if not supd_db_a: template = os.path.join(self._working_dir, "supd.template") if not os.path.exists(template): template = None self._supd = SupD(self._db, name=self._name, template=template, log=self._log, executable=supd_exe, dirpath=self._working_dir) else: if supd_db_a[0].name != self._name: raise PIDanticUsageException("The requested supd name %s is not in the db" % (self._name)) self._supd = SupD(self._db, data_object=supd_db_a[0]) self._is_alive() def _get_next_poll_time(self): self._next_poll_time = datetime.datetime.now() + datetime.timedelta(seconds=self._poll_interval) def _is_alive(self): self._supd.ping() def get_name(self): return self._name def get_pidantic(self, **kwvals): for p in self.run_needed_keywords: if p not in kwvals.keys(): raise PIDanticUsageException("The driver %s must have the parameter %s." % (self.driver_name, p)) for p in kwvals.keys(): if p not in self.run_needed_keywords and p not in self.run_optional_keywords: raise PIDanticUsageException("The driver %s does not know the parameter %s." % (self.driver_name, p)) program_object = self._supd.create_program_db(**kwvals) pidsupd = PIDanticSupD(program_object, self._supd, log=self._log) self._watched_processes[program_object.process_name] = pidsupd return pidsupd def reload_instances(self): self._watched_processes = {} data_obj = self._supd.get_data_object() for p in data_obj.programs: pidsupd = PIDanticSupD(p, self._supd, log=self._log) self._watched_processes[p.process_name] = pidsupd self.poll() return self._watched_processes def poll(self): all_state = self._supd.get_all_state() for state in all_state: name = state['name'] if name not in self._watched_processes.keys(): self._log.log(logging.ERROR, "Supervisord is reporting an unknown process %s" % (name)) pidsupd = self._watched_processes[name] pidsupd._process_state_change(state) def terminate(self): self._supd.terminate() self._supd.delete() def _backoff(self): self._poll_interval = self._poll_interval + (self._poll_interval * .5) self._get_next_poll_time()
def setUp(self): self.name = "test_something-" + str(uuid.uuid4()) self.dirpath = tempfile.mkdtemp() self.supd_db_path = "sqlite:///" + os.path.join(self.dirpath, "sup.db") self.supd_db = SupDDB(self.supd_db_path)