Ejemplo n.º 1
0
    def run(self):
        import settings
        import game_config
        from logics.share import debug_sync_change_time

        while True:
            try:
                if self.server.pid != os.getpid():
                    break

                kill_worker = False

                if settings.DEBUG and debug_sync_change_time():
                    kill_worker = True

                if not game_config.is_config_out():
                    game_config.load_all()
                    kill_worker = True

                if kill_worker:
                    self.server.kill_workers(signal.SIGQUIT)

            except:
                pass

            time.sleep(self.loop_time)
Ejemplo n.º 2
0
def get_user(uid):
    """# get_user: docstring
    args:
        uid:    ---    arg
    returns:
        0    ---
    """
    import game_config
    if not game_config.is_config_out():
        game_config.load_all()

    from logics.user import User
    user = User(uid)

    setattr(user, 'game_config_version', game_config.config_version)

    return user
Ejemplo n.º 3
0
    def get(self):
        import game_config
        if not game_config.is_config_out():
            game_config.load_all()

        from views import config
        method = self.get_argument('method', 'resource_version')
        func = getattr(config, method)
        d = {
            'status': 0,
            'data': func(self),
            'msg': '',
            'user_status': {},
        }

        self.set_header('content_type', 'application/json; charset=UTF-8')
        self.set_header('Content-Type', 'application/json; charset=UTF-8')
        r = json.dumps(d,
                       ensure_ascii=False,
                       separators=(',', ':'),
                       encoding="utf-8",
                       default=to_json)
        self.write(r)
        self.finish()
Ejemplo n.º 4
0
    def api(self):
        import game_config
        if not game_config.is_config_out():
            game_config.load_all()
        method = self.get_argument('method', 'new_user')
        from views import user
        msg = ''
        is_login = True
        if method == 'loading':
            rc, user, data = 0, None, {}
            # 通过version配置的格式 来判断是否提交了新版本的version
            # 如果提交了新版version 开始强制更新前端,则老版本前端包不再下载各个 game_config 配置,等到更新完新版客户端之后再去下载game_config 配置
            # 老版本的version配置格式 {'version': '','url': '','msg': unicode('''《超级英雄》敬请期待!''','utf-8'),}
            # 新版本的version配置格式 按平台区分更新信息 {'sogou':[],'androidcmge':[],'itools':[], ...}
            # 这代码为了解决 前端 遗留bug, 等更新完新客户端之后 可删除
            version_info = {}
            for version_info in game_config.version.itervalues():
                break
            need_upgrade = 'tongbu' in version_info or 'cmge' in version_info or 'itools' in version_info
            version = self.get_argument('version', '1.0.2')
            if version < '1.0.7' and need_upgrade:
                is_login = False
        else:
            func = getattr(user, method)
            rc, user, data = func(self)
            method_param = '%s.%s' % ('user', func.__name__)
            if rc != 0:
                msg = game_config.return_msg_config.get(method_param, {}).get(
                    rc, method_param + '_error_%d' % rc)

        # get_user_server_list 接口判断客户端是否需要强制更新
        if method == 'new_user':
            from models.user import UnameUid

            account = self.get_argument('account', '')
            uu = UnameUid.get(account)
            cur_server = uu.current_server
            if cur_server and cur_server in game_config.version:
                client_upgrade = {
                    'url': 'http://www.baidu.com',  # 前端需要跳转到的新包下载地址,一般是官网什么的
                    'msg': u'当前测试已经结束啦。感谢您对《禁区》的支持,我们将于2月中旬进行第二轮测试,敬请期待。',
                    'version': '',
                    'need_upgrade': False
                }

                _platform = account.split('_')[0].lower()
                _version_config = game_config.version[cur_server]
                _platform_config = _version_config.get(_platform, [])
                if _platform_config and len(_platform_config) == 3:
                    # [[version], [url], [msg]]
                    client_upgrade['version'] = _platform_config[0][0]
                    client_upgrade['url'] = _platform_config[1][0]
                    client_upgrade['msg'] = _platform_config[2][0]

                version = self.get_argument('version', '1.0.2')
                new_version = client_upgrade.get('version', '')
                if new_version and version < new_version:
                    client_upgrade['need_upgrade'] = True

                    d = {
                        'status': 0,
                        'data': {},
                        'msg': '',
                        'user_status': {},
                        'client_upgrade': client_upgrade
                    }
                    self.set_header('content_type',
                                    'application/json; charset=UTF-8')
                    self.set_header('Content-Type',
                                    'application/json; charset=UTF-8')
                    r = json.dumps(d,
                                   ensure_ascii=False,
                                   encoding="utf-8",
                                   indent=2,
                                   default=to_json)
                    self.write(r)
                    self.finish()
                    return -99009, {}, '', None

        ########################################
        self.set_header('content_type', 'application/json; charset=UTF-8')
        self.set_header('Content-Type', 'application/json; charset=UTF-8')
        try:
            r = handler_tools.result_generator(rc,
                                               data,
                                               msg,
                                               user,
                                               login=True,
                                               request=self)
            self.write(r)
        finally:
            self.finish()
            return rc, data, msg, user
Ejemplo n.º 5
0
    def loop(self, ):
        """# start: docstring
        args:
            :    ---    arg
        returns:
            0    ---
        """
        while 1:
            gevent.sleep(1)
            now = time.time()
            self.lock.acquire()

            if self.config_time % 60 == 0:
                # from logics.share import  debug_sync_change_time
                # 注意, 正式环境禁止启动此函数
                if settings.DEBUG:
                    debug_sync_change_time(self)

                if self.is_config_out():
                    game_config.load_all()
                    reload_all_config(self)

            pool_copy = copy.copy(self.pool)
            del_list = []
            for k, task_obj in pool_copy.iteritems():
                if now >= int(task_obj.get_next_time()):
                    try:
                        if task_obj.is_global() == 1:
                            # gevent.joinall([gevent.Greenlet.spawn(task_obj.get_func())], raise_error=True)
                            gevent.joinall(
                                [gevent.Greenlet.spawn(task_obj.get_func())])
                        elif task_obj.is_global() == 2:
                            world_ids = list(
                                set([
                                    value['world_id'] for value in
                                    game_config.server_pk_world.itervalues()
                                ]))
                            for world_id in world_ids:
                                print_log(
                                    'world func: ', task_obj.get_func(),
                                    datetime.datetime.fromtimestamp(
                                        task_obj.next_time))
                                # gevent.joinall([gevent.Greenlet.spawn(task_obj.get_func(), world_id)], raise_error=True)
                                gevent.joinall([
                                    gevent.Greenlet.spawn(
                                        task_obj.get_func(), world_id)
                                ])
                        else:
                            for server_name, server_cfg in settings.SERVERS.iteritems(
                            ):
                                if server_name == 'master': continue
                                # if server_cfg['config_type'] != CONFIG_TYPE: continue
                                if settings.get_config_type(
                                        server_name) != CONFIG_TYPE:
                                    continue
                                print_log(
                                    'func: ', task_obj.get_func(),
                                    datetime.datetime.fromtimestamp(
                                        task_obj.next_time))
                                # gevent.joinall([gevent.Greenlet.spawn(task_obj.get_func(), server_name)], raise_error=True)
                                gevent.joinall([
                                    gevent.Greenlet.spawn(
                                        task_obj.get_func(), server_name)
                                ])
                        timer_model.last_update_timestamp[
                            task_obj.get_key()] = now
                        if task_obj.is_repeat():
                            task_obj.parser()
                            timer_model.next_update_timestamp[
                                task_obj.get_key()] = int(
                                    task_obj.get_next_time())
                        else:
                            del_list.append(k)
                        timer_model.save()
                        print_log('timer, run %s, is_repeat: %s' %
                                  (k, str(task_obj.is_repeat())))
                    except:
                        trackback(msg='timer, timer %s ERROR: ' % str(k))
            for k in del_list:
                del self.pool[k]
            self.config_time += 1
            self.lock.release()
Ejemplo n.º 6
0
 def check_config():
     if not game_config.is_config_out():
         print_log('config out')
         game_config.load_all()
Ejemplo n.º 7
0
 for i in xrange(1, process_sum+1):
     pid = start_child(i)
     if pid == 0:
         parent = False
         break
 if parent and process_sum:
     signal.signal(signal.SIGTERM, sig_hander_parent)
     signal.signal(signal.SIGINT, sig_hander_parent)
     signal.signal(signal.SIGCHLD, signal.SIG_IGN)   # let init clear zombie child
     # print CHILDREN
     while 1:
         time.sleep(5)
         print CHILDREN
         if not game_config.is_config_out():
             print_log('config out')
             game_config.load_all()
             for task_id in CHILDREN.iterkeys():
                 print_log('config out, restart '+str(task_id))
                 if not restart_child(task_id):
                     parent = False
                     break
         if not parent:
             break
         for task_id, pid in CHILDREN.iteritems():   # if child is alive
             try:
                 child_process = psutil.Process(pid)
                 if not child_process.is_running() or os.getpid() != child_process.ppid():
                     print_log('NO this child, ', task_id, pid, child_process.is_running(), child_process.pid, os.getpid(), child_process.ppid())
                     raise psutil.NoSuchProcess(pid)
                 #mem_watcher(child_process)
             except psutil.NoSuchProcess, e: