async def received_job(message: IncomingMessage) -> None: """ AMQP job hook """ log.info("Received new job", extra=message.info()) try: job = _validate_message(message) except ValidationError as e: log.error(f"{e}. Rejecting without requeuing", extra=message.info()) message.reject(requeue=False) return result = await _process_job(job) message.ack() log.info("Job completed", extra={ "job_id": result.job.id, "url": result.job.url, "status": result.status, "size": result.size })
def __init__( self, message: apika.IncomingMessage, log: Optional[logging.Logger] = None, ): self.command_id: str | None = None self.sender: str | None = None self.body = {} self.message = message self._log = log self.is_valid = True # Acknowledges receipt of message message.ack() self.info: Dict[Any, Any] = message.info() self.headers = self.info["headers"] for key in self.headers: if isinstance(self.headers[key], bytes): self.headers[key] = self.headers[key].decode() self.message_code = self.headers.get("message_code", None) if self.message_code is None: self.is_valid = False if self._log: self._log.warning( f"received message without message_code: {message}") return self.sender = self.headers.get("sender", None) if self.sender is None and self._log: self._log.warning(f"received message without sender: {message}") self.command_id = message.correlation_id command_id_header = self.headers.get("command_id", None) if command_id_header and command_id_header != self.command_id: if self._log: self._log.error(f"mismatch between message " f"correlation_id={self.command_id} " f"and header command_id={command_id_header} " f"in message {message}") self.is_valid = False return self.body = json.loads(self.message.body.decode())
async def new_command(self, message: apika.IncomingMessage, ack=True): """Handles a new command received by the actor.""" if ack: async with message.process(): headers = message.info()["headers"] command_body = json.loads(message.body.decode()) else: headers = message.info()["headers"] command_body = json.loads(message.body.decode()) commander_id = headers["commander_id"].decode() command_id = headers["command_id"].decode() command_string = command_body["command_string"] try: command = Command( command_string, command_id=command_id, commander_id=commander_id, consumer_id=self.name, actor=self, loop=self.loop, ) command.actor = self # Assign the actor except CommandError as ee: self.write( "f", { "error": "Could not parse the " "following as a command: " f"{command_string!r}. {ee!r}" }, ) return return self.parse_command(command)
def handle(msg: IncomingMessage): print(msg.info()) print(msg.body)