def recv_command(self, cmd_type, **cmd_args): """Maps any command to a recv_* callback function""" completed_work = None gearman_command_name = get_command_name(cmd_type) if bool(gearman_command_name == cmd_type ) or not gearman_command_name.startswith('GEARMAN_COMMAND_'): unknown_command_msg = 'Could not handle command: %r - %r' % ( gearman_command_name, cmd_args) gearman_logger.error(unknown_command_msg) raise ValueError(unknown_command_msg) recv_command_function_name = gearman_command_name.lower().replace( 'gearman_command_', 'recv_') cmd_callback = getattr(self, recv_command_function_name, None) if not cmd_callback: missing_callback_msg = 'Could not handle command: %r - %r' % ( get_command_name(cmd_type), cmd_args) gearman_logger.error(missing_callback_msg) raise UnknownCommandError(missing_callback_msg) # Expand the arguments as passed by the protocol # This must match the parameter names as defined in the command handler completed_work = cmd_callback(**cmd_args) return completed_work
def assert_sent_abilities(self, expected_abilities): observed_abilities = set() self.assert_sent_command(GEARMAN_COMMAND_RESET_ABILITIES) for ability in expected_abilities: cmd_type, cmd_args = self.connection._outgoing_commands.popleft() assert get_command_name(cmd_type) == get_command_name(GEARMAN_COMMAND_CAN_DO) observed_abilities.add(cmd_args['task']) assert observed_abilities == set(expected_abilities)
def assert_sent_abilities(self, expected_abilities): observed_abilities = set() self.assert_sent_command(GEARMAN_COMMAND_RESET_ABILITIES) for ability in expected_abilities: cmd_type, cmd_args = self.connection._outgoing_commands.popleft() self.assertEqual(get_command_name(cmd_type), get_command_name(GEARMAN_COMMAND_CAN_DO)) observed_abilities.add(cmd_args['task']) self.assertEqual(observed_abilities, set(expected_abilities))
def _pack_command(self, cmd_type, cmd_args): """Converts a command to its raw binary format""" if cmd_type not in GEARMAN_PARAMS_FOR_COMMAND: raise ProtocolError('Unknown command: %r' % get_command_name(cmd_type)) if _DEBUG_MODE_: gearman_logger.debug('%s - Send - %s - %r', hex(id(self)), get_command_name(cmd_type), cmd_args) if cmd_type == GEARMAN_COMMAND_TEXT_COMMAND: return pack_text_command(cmd_type, cmd_args) else: # We'll be sending a response if we know we're a server side command is_response = bool(self._is_server_side) return pack_binary_command(cmd_type, cmd_args, is_response)
def _pack_command(self, cmd_type, cmd_args): """Converts a command to its raw binary format""" if cmd_type not in protocol.GEARMAN_PARAMS_FOR_COMMAND: raise ProtocolError('Unknown command: %r' % protocol.get_command_name(cmd_type)) log.msg("%s - send - %s - %r" % ( id(self), protocol.get_command_name(cmd_type), cmd_args), logging.DEBUG) if cmd_type == protocol.GEARMAN_COMMAND_TEXT_COMMAND: return protocol.pack_text_command(cmd_type, cmd_args) else: return protocol.pack_binary_command(cmd_type, cmd_args)
def execute_command(self, buf): self._incoming_buffer += buf cmd_type, cmd_args, cmd_len = protocol.parse_binary_command(self._incoming_buffer, False) print protocol.get_command_name(cmd_type), cmd_args, cmd_len gearman_command_name = protocol.get_command_name(cmd_type) recv_command_function_name = gearman_command_name.lower().replace('gearman_command_', 'recv_') cmd_callback = getattr(self, recv_command_function_name, None) if not cmd_callback: missing_callback_msg = 'Could not handle command: %r - %r' % (protocol.get_command_name(cmd_type), cmd_args) #gearman_logger.error(missing_callback_msg) #raise UnknownCommandError(missing_callback_msg) raise Exception(missing_callback_msg) # Expand the arguments as passed by the protocol # This must match the parameter names as defined in the command handler completed_work = cmd_callback(**cmd_args) self.read_command()
def recv_command(self, cmd_type, **cmd_args): """Maps any command to a recv_* callback function""" completed_work = None gearman_command_name = get_command_name(cmd_type) if bool(gearman_command_name == cmd_type) or not gearman_command_name.startswith('GEARMAN_COMMAND_'): unknown_command_msg = 'Could not handle command: %r - %r' % (gearman_command_name, cmd_args) gearman_logger.error(unknown_command_msg) raise ValueError(unknown_command_msg) recv_command_function_name = gearman_command_name.lower().replace('gearman_command_', 'recv_') cmd_callback = getattr(self, recv_command_function_name, None) if not cmd_callback: missing_callback_msg = 'Could not handle command: %r - %r' % (get_command_name(cmd_type), cmd_args) gearman_logger.error(missing_callback_msg) raise UnknownCommandError(missing_callback_msg) # Expand the arguments as passed by the protocol # This must match the parameter names as defined in the command handler completed_work = cmd_callback(**cmd_args) return completed_work
def _unpack_command(self, given_buffer): """Conditionally unpack a binary command or a text based server command""" if not given_buffer: cmd_type, cmd_args, cmd_len = None, None, 0 elif given_buffer[0] == protocol.NULL_CHAR: # We'll be expecting a response if we know we're a client side command cmd_type, cmd_args, cmd_len = protocol.parse_binary_command( given_buffer) else: cmd_type, cmd_args, cmd_len = protocol.parse_text_command( given_buffer) log.msg("%s - recv - %s - %r" % ( id(self), protocol.get_command_name(cmd_type), cmd_args), loglevel=logging.DEBUG) return cmd_type, cmd_args, cmd_len
def _unpack_command(self, given_buffer): """Conditionally unpack a binary command or a text based server command""" assert self._is_client_side is not None, "Ambiguous connection state" if not given_buffer: cmd_type = None cmd_args = None cmd_len = 0 elif given_buffer[0] == NULL_CHAR: # We'll be expecting a response if we know we're a client side command is_response = bool(self._is_client_side) cmd_type, cmd_args, cmd_len = parse_binary_command(given_buffer, is_response=is_response) else: cmd_type, cmd_args, cmd_len = parse_text_command(given_buffer) if _DEBUG_MODE_ and cmd_type is not None: gearman_logger.debug('%s - Recv - %s - %r', hex(id(self)), get_command_name(cmd_type), cmd_args) return cmd_type, cmd_args, cmd_len
def assert_commands_equal(self, cmd_type_actual, cmd_type_expected): self.assertEqual(get_command_name(cmd_type_actual), get_command_name(cmd_type_expected))
def assert_commands_equal(self, cmd_type_actual, cmd_type_expected): assert get_command_name(cmd_type_actual) == get_command_name( cmd_type_expected)