예제 #1
0
class Account(object):
    def __init__(self, db_update_fun=None, **kwargs):
        """
        id, passwd,
        id_card, email, phone,
        leader_id,
        bank, bank_address, bank_account, bank_name,
        wechat, alipay
        :param db_update_fun:
        :param kwargs:
        :return:
        """
        self.__dict__ = kwargs

        self.dfp = DirtyFlagProcess(self)
        self.db_update_fun = db_update_fun

    def update(self, curtime):
        dirty_db_dict = self.dfp.get_db_dirty_attr()
        if dirty_db_dict and self.db_update_fun:
            dirty_db_dict['id'] = self.id
            self.db_update_fun([dirty_db_dict])

    def __str__(self):
        return str(self.get_info_dic())

    def __eq__(self, other):
        return self.id == other.id

    def __get_key(self, key):
        return self.__dict__[key] if key in self.__dict__ else getattr(
            self, "attr_%s" % key, "")

    def get_info_dic(self):
        """
        获取所有信息
        :return:
        """
        return dict([(key, self.__get_key(key)) for key in KEY_SET])

    def view_info_dic(self):
        """
        允许别人来查看的信息
        :return:
        """
        return dict([(key, self.__get_key(key))
                     for key in KEY_SET - PRIVATE_KEY_SET])

    def update_data(self, data_dic):
        """
        数据更新
        :param data_dic: 需要更新的数据
        :return:
        """
        for k, v in data_dic.items():
            self.__dict__[k] = v
        self.dfp.add_db_flag_ls(data_dic.keys())

    # passwd
    @property
    def attr_passwd(self):
        return self.passwd

    @attr_passwd.setter
    def attr_passwd(self, new_passwd):
        if self.passwd == new_passwd or not new_passwd:
            return

        self.passwd = new_passwd
        self.dfp.add_db_flag("passwd")

    # stat
    @property
    def attr_stat(self):
        return self.stat

    @attr_stat.setter
    def attr_stat(self, new_stat):
        if self.stat == new_stat:
            return

        self.stat = new_stat
        self.dfp.add_db_flag("stat")
        self.attr_active_time = time.time()

    # login_time
    @property
    def attr_login_time(self):
        return self.login_time

    @attr_login_time.setter
    def attr_login_time(self, new_login_time):
        if self.login_time == new_login_time:
            return

        self.login_time = new_login_time
        self.dfp.add_db_flag("login_time")

    # active_time
    @property
    def attr_active_time(self):
        return self.active_time

    @attr_active_time.setter
    def attr_active_time(self, new_active_time):
        if self.active_time == new_active_time:
            return

        self.active_time = new_active_time
        self.dfp.add_db_flag("active_time")

    # level
    @property
    def attr_level(self):
        finished_orders = self.attr_finished_accept + self.attr_finished_apply

        cur_lvel_idx = 0
        for i, lvl_req in enumerate(LEV_REQ):
            if finished_orders > lvl_req:
                cur_lvel_idx = i
            else:
                break

        return LEVEL[cur_lvel_idx]

    @property
    def is_seal(self):
        """
        是否封号
        :return:
        """
        return self.attr_stat == SEALED

    @property
    def is_active(self):
        """
        是否激活
        :return:
        """
        return self.attr_stat == ACTIVED
예제 #2
0
class Service(object):
    def __init__(self,
                 id,
                 ip,
                 service_group,
                 port,
                 params,
                 state,
                 start_time,
                 db_update_fun=None):
        self.id = id
        self.ip = ip
        self.service_group = service_group
        self._port = port
        self.params = params
        self.state = state
        self.start_time = start_time

        self.set_hb_info(heartbeat_time="")

        self.db_update_fun = db_update_fun
        self.tp_service_dic = {}
        self.dfp = DirtyFlagProcess(self)

        self._control_rpc = None

        Jobs().add_interval_job(HEARTBEAT_EXPIRE_TIME, self._heart_beat_expire)

        if "db_name" in self.params:
            Jobs().add_cron_job(self.__backup, hour=MYSQL_DB_BACKUP_HOUR)

    def set_hb_info(self,
                    process_name="",
                    service_version="unknown",
                    port=None,
                    current_load=1,
                    heartbeat_time=None):
        """
        init heartbeat info
        :return:
        """
        self.process_name = process_name
        self.service_version = service_version
        self.current_load = current_load
        self._hb_port = port
        self.heartbeat_time = heartbeat_time if heartbeat_time is not None else datetime.datetime.now(
        )
        self.gen_view_info()

    def gen_view_info(self):
        is_https = 'https' in self.get_port()
        http_proctol = "https" if is_https else "http"
        http_host = self.ip + ":" + str(
            self.get_port()['https'] if is_https else self.get_port()['http'])

        self.href_doc = http_host
        self.href = "%s://%s/doc" % (http_proctol, self.href_doc)
        jid = self.params.get("JID", None)
        self.locate = {http_proctol: http_host, "xmpp": "%s" % jid} \
            if jid \
            else {http_proctol: http_host}

    @property
    def control_rpc(self):
        tcp_port = self.get_port().get('tcp', None)
        if tcp_port:
            self._control_rpc = GsRpcClient(self.ip, tcp_port)
        return self._control_rpc

    def update(self, curtime):
        dirty_db_dict = self.dfp.get_db_dirty_attr()
        if dirty_db_dict and self.db_update_fun:
            dirty_db_dict['id'] = self.id
            self.db_update_fun([dirty_db_dict])

    def __str__(self):
        return str(self.__dict__)

    def __eq__(self, other):
        return self.id == other.id

    def get_port(self):
        return self._hb_port if self._hb_port else self._port

    def get_info_dic(self):
        return {
            "id": self.id,
            "ip": self.ip,
            "service_group": self.service_group,
            "port": self.get_port(),
            "params": self.params,
            "state": self.state,
            "start_time": self.start_time,
            "heartbeat_time": self.heartbeat_time,
            "service_version": self.service_version,
            "current_load": self.current_load,
            "href_doc": self.href_doc,
            "href": self.href,
            "locate": self.locate
        }

    def web_pick(self):
        heartbeat_time = datetime_to_string(self.heartbeat_time) \
            if isinstance(self.heartbeat_time, datetime.datetime) \
            else self.heartbeat_time

        start_time = datetime_to_string(self.start_time) \
            if isinstance(self.start_time, datetime.datetime) \
            else self.start_time

        return {
            "id": self.id,
            "ip": self.ip,
            "service_group": self.service_group,
            "port": self.get_port(),
            "params": self.params,
            "state": self.state,
            "start_time": start_time,
            "heartbeat_time": heartbeat_time,
            "process_name": self.process_name,
            "service_version": self.service_version,
            "current_load": self.current_load,
            "href_doc": self.href_doc if self.state == SS_RUNNING else "",
            "href": self.href if self.state == SS_RUNNING else ""
        }

    def _use(self):
        self._set_state(SS_RUNNING)

    def _free(self):
        if self.state == SS_FREE:
            return
        self._set_state(SS_FREE)
        self.set_hb_info(heartbeat_time="")

    def _set_state(self, new_state):
        if new_state == self.state:
            return

        self.state = new_state
        self.dfp.add_db_flag("state", False)
        ServiceMgr().on_service_state_change(self)

    def _heart_beat_expire(self):
        if self.state == SS_RUNNING:
            if not self.heartbeat_time:
                self.stop()
                return
            expire_time = self.heartbeat_time + datetime.timedelta(
                seconds=HEARTBEAT_EXPIRE_TIME)
            now = datetime.datetime.now()
            if expire_time <= now:
                self.stop()

    def is_free(self):
        return self.state == SS_FREE

    def start(self):
        if not self.is_free():
            return False

        self._use()
        self.start_time = datetime.datetime.now()
        self.dfp.add_db_flag("start_time", False)
        return True

    def stop(self):
        if not self.state == SS_RUNNING:
            return True

        self._free()
        return True

    def heart_beat(self, process_name, service_version, port, current_load,
                   running):
        assert isinstance(port, dict)
        self.set_hb_info(process_name, service_version, port, current_load)
        self._set_state(SS_RUNNING) if running else self.stop()

    def hash_key(self):
        return self.id

    def __backup(self):
        mysql_table = self.params["db_name"]

        from service_mgr.lib.tp_service import TPServiceMgr
        mysql_ls = TPServiceMgr().filter_tp_services(
            FilterTPServiceNotKeyResult, RT_MYSQL)
        for mysql_dic in mysql_ls:
            mysql_ip = mysql_dic['ip']

            # 支持tcp/TCP
            try:
                mysql_port = mysql_dic['port'][PT_TCP.lower()]
            except:
                mysql_port = mysql_dic['port'][PT_TCP]
            mysql_user = mysql_dic['params']['db_user']
            mysql_passwd = mysql_dic['params']['db_password']

            mysql_util.MysqlUtil.db_dump(
                mysql_ip,
                mysql_port,
                mysql_user,
                mysql_passwd,
                mysql_table,
                use_gzip=True if platform.system() == 'Linux' else False)
            logger.info(
                "Service::__backup success!!! mysql_ip:%s mysql_port:%s mysql_table:%s"
                % (mysql_ip, mysql_port, mysql_table))