def mount(self, ctx): """ 将缓冲区的内容装载至指定位置 外围项目启动通过当前环境的解释器去运行脚本程式,否则会出现找不到现有项目模块的问题 """ source_topic = self.boot_conf.get('source_topic') def callback(ch, method, properties, body): ctx.collect(self._collect_from_elements(body)) # 将成功收集的tag删除 delivery_tag = int(method.delivery_tag) ch.basic_ack(delivery_tag=delivery_tag) try: channel = self.connection.channel() channel.queue_declare(queue=source_topic, durable=True) channel.basic_consume( queue=source_topic, consumer_callback=callback, no_ack=False ) channel.start_consuming() except Exception as e: logger().error('RabbitMQ get exception: {}'.format(e)) return None
def handle_start_lesson(message: telebot.types.Message): """ Метод начала урока. Для привязки к преподавателю текущей группы :param message: Входящий запрос :return: """ logger(message.json) response_text = start_lesson_command(message) bot.send_message(message.chat.id, response_text)
def handler_start(message: telebot.types.Message): """ Метод старта бота. Для отправки вступительного письма :param message: Входящий запрос :return: """ logger(message.json) response_text = GREETING_MESSAGE bot.send_message(message.chat.id, response_text)
def call_back_room(c: telebot.types.CallbackQuery): """ Метод выставления оценки. :param c: :return: """ logger(c.message) response_text = score_command(c.message.chat.id, c.data) bot.send_message(c.message.chat.id, response_text)
def handler_help(message: telebot.types.Message): """ Справочный метод для помощи пользователям :param message: Входящий запрос :return: """ logger(message.json) response_text = HELP_MESSAGE.format(TABLE_URL) bot.send_message(message.chat.id, response_text)
def handle_id(message: telebot.types.Message): """ Бесполезный метод для получения идентификатора пользователя :param message: Входящий запрос :return: """ logger(message.json) response_id = message.from_user.id response_text = response_id # возвращаем user id bot.send_message(message.chat.id, response_text)
def get_dict_res(self): """获取结果字典""" try: res = json.loads(self.res.text) except ValueError: logger().warn(self.res.text) return None except Exception: return None return res
def insert_single(session, table, data): """ 插入单条 :param session: clickhouse链接 :param table: clickhouse的model :param data: 数据, 参考官方文档 :return: """ try: session.execute(table.__table__.insert(), [data]) except Exception as e: logger().error('CK INSERT ERROR error:{} data:{}', e, data)
def mount(self, ctx): """ 将缓冲区的内容装载至指定位置 外围项目启动通过当前环境的解释器去运行脚本程式,否则会出现找不到现有项目模块的问题 """ source_topic = self.boot_conf.get('source_topic') try: mq_key, mq_elements = self.RedisStore.read_buffer(source_topic) return self._collect_from_elements(ctx, mq_key, mq_elements) except Exception as e: logger().error('Redis get exception: {}'.format(e)) return None
def handle_search(message: telebot.types.Message): """ Обработчик запроса на поиска ученика по подстроке :param message: Входящий запрос :return: """ logger(message.json) response_text = search_command(message) if isinstance(response_text, list): for name in response_text: reply_keyboard = keyboard_builder(name) bot.send_message(message.chat.id, name, reply_markup=reply_keyboard) else: bot.send_message(message.chat.id, response_text)
def delete(table, filter_obj): """ 手工组装delete语句, 如果clickhouse_sqlalchemy支持delete操作, 请尽快停用此方法 :param table: store中clickhouse的model :param filter_obj: orm类,务必包含filter Example: session.query(Table).filter(Table.id=1).filter(Table.name='Fang') :return: """ filter_sql, filter_value = ClickhouseStore.__filter_format( filter_obj, table.__tablename__) exec_sql = 'ALTER TABLE {} DELETE WHERE {}'.format( table.__table__, filter_sql) try: filter_obj.session.execute(exec_sql, filter_value) except Exception as e: logger().error('CK DELETE ERROR error:{} sql:{}, data:{}', e, exec_sql, filter_value)
def update(table, filter_obj, update_dict): """ 手工组装update语句, 如果clickhouse_sqlalchemy支持update操作, 请尽快停用此方法 :param table: store中clickhouse的model :param filter_obj: orm类,务必包含filter Example: session.query(Table).filter(Table.id=1).filter(Table.name='Fang') :param update_dict: 需要更新的值字典,字符串请转义,不能更新字段不验证,直接由底层抛出异常 Example: {'id': 4, 'name': '\'Hu\''} :return: """ filter_sql, filter_value = ClickhouseStore.__filter_format( filter_obj, table.__tablename__) update_sql = ClickhouseStore.__update_format(update_dict) exec_sql = 'ALTER TABLE {} UPDATE {} WHERE {}'.format( table.__table__, update_sql, filter_sql) try: filter_obj.session.execute(exec_sql, filter_value) except Exception as e: logger().error('CK UPDATE ERROR error:{} sql:{}, data:{}', e, exec_sql, filter_value)
def run(self, ctx): # 还原上次挂起 data_source = self.__data_source data_source.get_handler() # 自动数秒 wait_seconds = get_conf('base', 'BASE').get('source_wait_second', 10) if not self.boot_conf.get('source_topic'): raise ValueError('ERROR SOURCE TOPIC') try: while self._running: position = data_source.mount(ctx) if position: data_source.set_position(position) # break else: time.sleep(wait_seconds) except Exception as err: logger().error('Generator raise error: {}, job exited'.format(err)) finally: logger().info('Job finished.\r\n')
def flatMap(self, value, collector): """ 获取redis源头中的参数,normal初始化解析 """ method, ip, args, gateway_uri, receive_time = value args = json.loads(args) try: data = args.get('data') except Exception as e: logger(LogName.TEST).info(args) return if type(data).__name__ != 'list': logger(LogName.ORIGIN).warning('cannot get data {}'.format(args)) return topic = args.get('topic') if topic not in ALLOW_TOPIC: logger(LogName.ORIGIN).warning('error topic {}'.format(args)) return # 过长做切片处理 piece_len = 10 for piece in range(0, len(data), piece_len): data_piece = data[piece:piece + piece_len] collector.collect((topic, ip, json.dumps(data_piece)))
def fail(self): """失败回调""" logger(LogName.CURL).notice('[GATEWAY_CURL] [FAIL] {} {} {}'.format( self._url, self._data, self.res)) return self.res
def fail(self): """失败回调""" logger(LogName.CURL).notice('[CURL] [FAIL] {} {} {}'.format( self._url, self._data, self.res)) self.reset_retry() return self.res
def success(self): """成功回调""" logger(LogName.CURL).notice('[CURL] [SUCCESS] {} {} {}'.format( self._url, self._data, self.res)) self.reset_retry() return self.res
def error(update, context): """Log Errors caused by Updates.""" logger().error(f"\nupdate: {update}\nerror: {context.error}\n")
def __init__(self, redis_conf='base'): try: self.__connection = RedisStore(redis_conf).get_connection() except Exception as e: logger().warning(e) self.__connection = None