Exemple #1
0
 def handle_events(self, message):
     # 这里应该分开处理,先都在这处理了
     if message.get('type') != 'message': return
     result = json.loads(message.get('data', {}))
     event_name = result.get('event')
     data = result.get('data')
     from py12306.helpers.event import Event
     method = getattr(Event(), event_name)
     if method:
         create_thread_and_run(Event(), event_name, Const.IS_TEST, kwargs={'data': data, 'callback': True})
Exemple #2
0
 def user_did_load(self):
     """
     用户已经加载成功
     :return:
     """
     if self.user_loaded: return
     self.user_loaded = True
     Event().user_loaded({'key': self.key})  # 发布通知
Exemple #3
0
 def did_loaded_user(self):
     """
     恢复用户成功
     :return:
     """
     UserLog.add_quick_log(UserLog.MESSAGE_LOADED_USER.format(self.user_name)).flush()
     if self.check_user_is_login() and self.get_user_info():
         UserLog.add_quick_log(UserLog.MESSAGE_LOADED_USER_SUCCESS.format(self.user_name)).flush()
         Event().user_loaded({'key': self.key}) # 发布通知
         self.is_ready = True
         UserLog.print_welcome_user(self)
     else:
         UserLog.add_quick_log(UserLog.MESSAGE_LOADED_USER_BUT_EXPIRED).flush()
Exemple #4
0
    def refresh_users(self, old):
        for account in self.user_accounts:
            key = account.get('key')
            old_account = array_dict_find_by_key_value(old, 'key', key)
            if old_account and account != old_account:
                user = self.get_user(key)
                user.init_data(account)
            elif not old_account:  # 新用户 添加到 多线程
                new_user = self.init_user(account)
                create_thread_and_run(jobs=new_user, callback_name='run', wait=Const.IS_TEST)

        for account in old:  # 退出已删除的用户
            if not array_dict_find_by_key_value(self.user_accounts, 'key', account.get('key')):
                Event().user_job_destroy({'key': account.get('key')})
Exemple #5
0
    def handle_seats(self, allow_seats, ticket_info):
        for seat in allow_seats:  # 检查座位是否有票
            self.set_seat(seat)
            ticket_of_seat = ticket_info[self.current_seat]
            if not self.is_has_ticket_by_seat(ticket_of_seat):  # 座位是否有效
                continue
            QueryLog.print_ticket_seat_available(left_date=self.get_info_of_left_date(),
                                                 train_number=self.get_info_of_train_number(), seat_type=seat,
                                                 rest_num=ticket_of_seat)
            if not self.is_member_number_valid(ticket_of_seat):  # 乘车人数是否有效
                if self.allow_less_member:
                    self.member_num_take = int(ticket_of_seat)
                    QueryLog.print_ticket_num_less_than_specified(ticket_of_seat, self)
                else:
                    QueryLog.add_quick_log(
                        QueryLog.MESSAGE_GIVE_UP_CHANCE_CAUSE_TICKET_NUM_LESS_THAN_SPECIFIED).flush()
                    continue
            if Const.IS_TEST: return
            # 检查完成 开始提交订单
            QueryLog.print_ticket_available(left_date=self.get_info_of_left_date(),
                                            train_number=self.get_info_of_train_number(),
                                            rest_num=ticket_of_seat)
            if User.is_empty():
                QueryLog.add_quick_log(QueryLog.MESSAGE_USER_IS_EMPTY_WHEN_DO_ORDER.format(self.retry_time))
                return stay_second(self.retry_time)

            order_result = False
            user = self.get_user()
            if not user:
                QueryLog.add_quick_log(QueryLog.MESSAGE_ORDER_USER_IS_EMPTY.format(self.retry_time))
                return stay_second(self.retry_time)

            lock_id = Cluster.KEY_LOCK_DO_ORDER + '_' + user.key
            if Config().is_cluster_enabled():
                if self.cluster.get_lock(lock_id, Cluster.lock_do_order_time,
                                         {'node': self.cluster.node_name}):  # 获得下单锁
                    order_result = self.do_order(user)
                    if not order_result:  # 下单失败,解锁
                        self.cluster.release_lock(lock_id)
                else:
                    QueryLog.add_quick_log(
                        QueryLog.MESSAGE_SKIP_ORDER.format(self.cluster.get_lock_info(lock_id).get('node'),
                                                           user.user_name))
                    stay_second(self.retry_time)  # 防止过多重复
            else:
                order_result = self.do_order(user)

            # 任务已成功 通知集群停止任务
            if order_result:
                Event().job_destroy({'name': self.job_name})