def handle(self): input = self.request.input require_tcp_port(input.address) with closing(self.odb.session()) as session: existing_one = session.query(ChannelZMQ.id).\ filter(ChannelZMQ.cluster_id==input.cluster_id).\ filter(ChannelZMQ.name==input.name).\ first() if existing_one: raise Exception('A ZeroMQ channel `{}` already exists in this cluster'.format(input.name)) # Is the service's name correct? service = session.query(ServiceModel).\ filter(Cluster.id==input.cluster_id).\ filter(ServiceModel.cluster_id==Cluster.id).\ filter(ServiceModel.name==input.service).first() if not service: msg = 'Service `{}` does not exist in this cluster'.format(input.service) raise Exception(msg) try: sub_key = input.get('sub_key', b'') item = ChannelZMQ() item.name = input.name item.is_active = input.is_active item.address = input.address item.socket_type = input.socket_type item.socket_method = input.socket_method item.sub_key = sub_key item.cluster_id = input.cluster_id item.service = service item.pool_strategy = input.pool_strategy item.service_source = input.service_source item.data_format = input.data_format session.add(item) session.commit() input.action = CHANNEL.ZMQ_CREATE.value input.sub_key = sub_key input.service_name = service.name input.source_server = self.server.get_full_name() input.id = item.id input.config_cid = 'channel.zmq.create.{}.{}'.format(input.source_server, self.cid) self.broker_client.publish(input) self.response.payload.id = item.id self.response.payload.name = item.name except Exception: self.logger.error('ZeroMQ channel could not be created, e:`%s`', format_exc()) session.rollback() raise
def handle(self): input = self.request.input with closing(self.odb.session()) as session: existing_one = session.query(ChannelZMQ.id).\ filter(ChannelZMQ.cluster_id==input.cluster_id).\ filter(ChannelZMQ.name==input.name).\ first() if existing_one: raise Exception('A ZeroMQ channel [{0}] already exists on this cluster'.format(input.name)) # Is the service's name correct? service = session.query(Service).\ filter(Cluster.id==input.cluster_id).\ filter(Service.name==input.service).first() if not service: msg = 'Service [{0}] does not exist on this cluster'.format(input.service) raise Exception(msg) try: sub_key = input.get('sub_key', b'') item = ChannelZMQ() item.name = input.name item.is_active = input.is_active item.address = input.address item.socket_type = input.socket_type item.sub_key = sub_key item.socket_method = input.socket_method item.cluster_id = input.cluster_id item.service = service item.pool_strategy = input.pool_strategy item.service_source = input.service_source item.data_format = input.data_format session.add(item) session.commit() input.action = CHANNEL.ZMQ_CREATE.value input.sub_key = sub_key input.service = service.impl_name self.broker_client.publish(input) self.response.payload.id = item.id self.response.payload.name = item.name except Exception, e: msg = 'Could not create a ZeroMQ channel, e:[{e}]'.format(e=format_exc(e)) self.logger.error(msg) session.rollback() raise