class Consumer(object): def __init__(self): self.client = client self.executor = Executor() self.context = Context() def read(self): while True: # XREAD() 阻塞式读取消息 items = self.client.xread( {'clover': '0-0'}, # FIFO消费方式消费队列 block=0, # 阻塞式消费 count=1 # xread每次从消息队列最多取一个消息 ) stream_id = items[0][1][0][0] fields = items[0][1][0][1] data = json.loads(fields['businessData']) try: print(f'{stream_id}消息 开始消费...') self.context.build_context(data) self.executor.execute(self.context) except Exception as e: print(f'{stream_id}消息消费失败; 暂无重试机制,请修改业务代码bug后重新发消息消费') print(e) self.client.xdel('clover', stream_id) else: print(f'{stream_id}消息消费成功') self.client.xdel('clover', stream_id) # 消费频率,监听队列间隔,轮询间隔 time.sleep(1)
class Run(object): def __init__(self): super(Run, self).__init__() self.executor = Executor() self.context = Context() def executor_run(self, data): self.context.build_context(data) self.executor.execute(self.context)
def update(self, data): """ # 使用id作为条件,更新数据库重的数据记录。 # 通过id查不到数据时增作为一条新的记录存入。 :param data: :return: """ old_model = InterfaceModel.query.get(data['id']) if old_model is None: model = InterfaceModel(**data) db.session.add(model) db.session.commit() old_model = model else: {setattr(old_model, k, v) for k, v in data.items()} old_model.updated = datetime.datetime.now() db.session.commit() context = Context() context.build_context({ 'type': 'interface', 'id': data['id'], 'user': data }) executor = Executor('debug') status, message, data = executor.execute(context) return old_model.id, status, message, data
def create(self, data): """ # 将页面数据保存到数据库。 :param data: :return: """ model = InterfaceModel(**data) db.session.add(model) db.session.commit() context = Context() context.build_context({ 'type': 'interface', 'id': model.id, 'user': data }) executor = Executor('debug') status, message, data = executor.execute(context) return model.id, status, message, data