def _on_OUTGOING_SFTP_EXECUTE(self, msg, is_reconnect=False, _utcnow=datetime.utcnow): out = {} connection = self.connections[msg.id] # type: SFTPConnection start_time = _utcnow() try: result = connection.execute(msg.cid, msg.data, msg.log_level) # type: Output except ErrorReturnCode as e: out['stdout'] = e.stdout out['stderr'] = e.stderr except Exception as e: out['stderr'] = format_exc() out['is_ok'] = False else: out.update(result.to_dict()) finally: out['cid'] = msg.cid out['command_no'] = connection.command_no out['response_time'] = str(_utcnow() - start_time) return Response(data=dumps(out))
def _on_DEFINITION_WMQ_CREATE(self, msg): """ Creates a new connection to IBM MQ. """ # Require that PyMQI be available if not self.pymqi: return Response(_http_503, 'Could not find pymqi module, IBM MQ connections will not start') # Call our parent which will actually create the definition return super(IBMMQConnectionContainer, self).on_definition_create(msg)
def _on_CHANNEL_WMQ_EDIT(self, msg): """ Updates an IBM MQ MQ channel by stopping it and starting again with a new configuration. """ with self.lock: channel = self.channels[msg.id] channel.stop() channel.queue_name = msg.queue.encode('utf8') channel.service_name = msg.service_name channel.data_format = msg.data_format channel.keep_running = True channel.start() return Response()
def _on_OUTGOING_WMQ_SEND(self, msg, is_reconnect=False): """ Sends a message to a remote IBM MQ queue - note that the functionality is specific to IBM MQ and, consequently, it does not make use of any method in the parent class unlike, e.g. _on_CHANNEL_WMQ_DELETE. """ with self.lock: outconn_id = msg.get('id') or self.outconn_name_to_id[msg.outconn_name] outconn = self.outconns[outconn_id] if not outconn.is_active: return Response(_http_406, 'Cannot send messages through an inactive connection', 'text/plain') else: def_id = self.outconn_id_to_def_id[outconn_id] conn = self.connections[def_id] conn.ping() try: delivery_mode = msg.delivery_mode or outconn.delivery_mode priority = msg.priority or outconn.priority expiration = msg.expiration or outconn.expiration text_msg = TextMessage( text = msg.data, jms_delivery_mode = delivery_mode, jms_priority = priority, jms_expiration = expiration, jms_correlation_id = msg.get('correlation_id', '').encode('utf8'), jms_message_id = msg.get('msg_id', '').encode('utf8'), jms_reply_to = msg.get('reply_to', '').encode('utf8'), ) conn.send(text_msg, msg.queue_name.encode('utf8')) return Response(data=dumps(text_msg.to_dict(False))) except(self.pymqi.MQMIError, WebSphereMQException) as e: if isinstance(e, self.pymqi.MQMIError): cc_code = e.comp reason_code = e.reason else: cc_code = e.completion_code reason_code = e.reason_code # Try to reconnect if the connection is broken but only if we have not tried to already if (not is_reconnect) and cc_code == _cc_failed and reason_code == _rc_conn_broken: self.logger.warn('Caught MQRC_CONNECTION_BROKEN in send, will try to reconnect connection to %s ', conn.get_connection_info()) # Sleep for a while before reconnecting sleep(1) # Try to reconnect conn.reconnect() # Confirm it by pinging the queue manager conn.ping() # Resubmit the request return self._on_OUTGOING_WMQ_SEND(msg, is_reconnect=True) else: return self._on_send_exception() except Exception as e: return self._on_send_exception()