コード例 #1
0
ファイル: subscriber.py プロジェクト: plamut/python-pubsub
 def callback(message: pubsub_v1.subscriber.message.Message) -> None:
     print(f"Received {message.data}.")
     if message.attributes:
         print("Attributes:")
         for key in message.attributes:
             value = message.attributes.get(key)
             print(f"{key}: {value}")
     message.ack()
コード例 #2
0
 def _subscribe_callback(self,
                         message: pubsub_v1.subscriber.message.Message):
     payload = json.loads(bytes.decode(message.data))
     event = EventMessage(payload, self._auth)
     # Only accept device events once the Device Manager has been loaded.
     # We are ok with missing messages on startup since the device manager will
     # do a live read.
     if self._device_manager_task.done():
         self._device_manager_task.result().handle_event(event)
     if self._callback:
         self._callback.handle_event(event)
     message.ack()
コード例 #3
0
 async def _async_message_callback(
         self, message: pubsub_v1.subscriber.message.Message):
     """Invoked when a message is received."""
     payload = json.loads(bytes.decode(message.data))
     event = EventMessage(payload, self._auth)
     # Only accept device events once the Device Manager has been loaded.
     # We are ok with missing messages on startup since the device manager
     # will do a live read .
     if self._device_manager_task.done():
         await self._device_manager_task.result().async_handle_event(event)
     if self._callback:
         await self._callback.async_handle_event(event)
     message.ack()
コード例 #4
0
    def callback(message: pubsub_v1.subscriber.message.Message) -> None:
        # Get the message serialization type.
        encoding = message.attributes.get("googclient_schemaencoding")
        # Deserialize the message data accordingly.
        if encoding == "BINARY":
            state.ParseFromString(message.data)
            print("Received a binary-encoded message:\n{state}")
        elif encoding == "JSON":
            Parse(message.data, state)
            print(f"Received a JSON-encoded message:\n{state}")
        else:
            print(f"Received a message with no encoding:\n{message}")

        message.ack()
コード例 #5
0
    def callback(message: pubsub_v1.subscriber.message.Message) -> None:
        # Get the message serialization type.
        encoding = message.attributes.get("googclient_schemaencoding")
        # Deserialize the message data accordingly.
        if encoding == "BINARY":
            bout = io.BytesIO(message.data)
            decoder = BinaryDecoder(bout)
            reader = DatumReader(avro_schema)
            message_data = reader.read(decoder)
            print(f"Received a binary-encoded message:\n{message_data}")
        elif encoding == "JSON":
            message_data = json.loads(message.data)
            print(f"Received a JSON-encoded message:\n{message_data}")
        else:
            print(f"Received a message with no encoding:\n{message}")

        message.ack()
コード例 #6
0
def callback(message: pubsub_v1.subscriber.message.Message) -> None:
    from datetime import datetime
    import json

    data = json.loads(message.data.decode("utf-8"))

    def event_devel():
        log.debug(f"Handling 'request={data['request']}'.")

        if lkp.instance_role == "controller":
            setup.fetch_devel_scripts()
        elif lkp.instance_role == "compute":
            setup.fetch_devel_scripts()
        elif lkp.instance_role == "login":
            log.info(f"NO-OP for 'Request={data['request']}'.")
        else:
            log.error(f"Unknown node role: {lkp.instance_role}")

    def event_reconfig():
        log.debug(f"Handling 'request={data['request']}'.")
        lkp.clear_template_info_cache()

        if lkp.instance_role == "controller":
            # Inactive all partitions to prevent further scheduling
            partitions = get_partitions()
            update_partitions(partitions, "INACTIVE")

            # Fetch and write new config.yaml
            util.cfg = util.config_from_metadata()
            if not util.cfg.pubsub_topic_id:
                log.info("Auto reconfigure is disabled. Aborting...")
                exit(0)
            util.save_config(util.cfg, util.CONFIG_FILE)
            util.lkp = util.Lookup(util.cfg)

            # Regenerate *.conf files
            log.info("Clean install custom scripts")
            setup.install_custom_scripts(clean=True)
            log.info("Generating new cloud.conf for slurm.conf")
            setup.gen_cloud_conf(util.lkp)
            log.info("Generating new slurm.conf")
            setup.install_slurm_conf(util.lkp)
            log.info("Generating new slurmdbd.conf")
            setup.install_slurmdbd_conf(util.lkp)
            log.info("Generating new cloud_gres.conf")
            setup.gen_cloud_gres_conf(util.lkp)
            log.info("Generating new cgroup.conf")
            setup.install_cgroup_conf()

            # Send restart message to cluster topic
            message_json = json.dumps({
                "request":
                "restart",
                "timestamp":
                datetime.utcnow().isoformat(),
            })
            publish_message(project, util.cfg.pubsub_topic_id, message_json)
        elif lkp.instance_role == "compute":
            log.info(f"NO-OP for 'Request={data['request']}'.")
        elif lkp.instance_role == "login":
            log.info(f"NO-OP for 'Request={data['request']}'.")
        else:
            log.error(f"Unknown node role: {lkp.instance_role}")

    def event_restart():
        log.debug(f"Handling 'request={data['request']}'.")

        if lkp.instance_role == "controller":
            run("systemctl restart slurmctld")
        elif lkp.instance_role == "compute":
            run("systemctl restart slurmd")
        elif lkp.instance_role == "login":
            log.info(f"NO-OP for 'Request={data['request']}'.")
        else:
            log.error(f"Unknown node role: {lkp.instance_role}")

    event_handler = dict.get(
        {
            "devel": event_devel,
            "reconfig": event_reconfig,
            "restart": event_restart,
        },
        data["request"].lower(),
        lambda: log.error(f"Unknown 'Request={data['request']}' received."),
    )
    event_handler()
    message.ack()
コード例 #7
0
ファイル: subscribe_pubsub.py プロジェクト: whigg/sawatabi
 def callback(message: pubsub_v1.subscriber.message.Message) -> None:
     dt_now = datetime.datetime.now()
     print(f"[{dt_now}] Received message from '{subscription_path}': {message.data.decode('utf-8')}")
     message.ack()
コード例 #8
0
ファイル: subscriber.py プロジェクト: plamut/python-pubsub
 def callback(message: pubsub_v1.subscriber.message.Message) -> None:
     print(f"Received {message}.")
     print(f"With delivery attempts: {message.delivery_attempt}.")
     message.ack()
コード例 #9
0
ファイル: subscriber.py プロジェクト: plamut/python-pubsub
 def callback(message: pubsub_v1.subscriber.message.Message) -> None:
     print(f"Received {message}.")
     message.ack()
コード例 #10
0
ファイル: subscriber.py プロジェクト: plamut/python-pubsub
 def callback(message: pubsub_v1.subscriber.message.Message) -> None:
     print(f"Received {message.data}.")
     time.sleep(timeout +
                3.0)  # Pocess longer than streaming pull future timeout.
     message.ack()
     print(f"Done processing the message {message.data}.")
コード例 #11
0
ファイル: sub.py プロジェクト: plamut/python-pubsub
 def callback(message: pubsub_v1.subscriber.message.Message) -> None:
     print(f"Received {message}.")
     # Acknowledge the message. Unack'ed messages will be redelivered.
     message.ack()
     print(f"Acknowledged {message.message_id}.")
コード例 #12
0
 def _callback(self, message: pubsub_v1.subscriber.message.Message):
     """Callback for consumer."""
     data = message.data
     print(data)
     self.func(data)
     message.ack()