Пример #1
0
    def execute(self, context):
        hook = PubSubHook(
            gcp_conn_id=self.gcp_conn_id,
            delegate_to=self.delegate_to,
            impersonation_chain=self.impersonation_chain,
        )

        self.log.info("Creating subscription for topic %s", self.topic)
        result = hook.create_subscription(
            project_id=self.project_id,
            topic=self.topic,
            subscription=self.subscription,
            subscription_project_id=self.subscription_project_id,
            ack_deadline_secs=self.ack_deadline_secs,
            fail_if_exists=self.fail_if_exists,
            push_config=self.push_config,
            retain_acked_messages=self.retain_acked_messages,
            message_retention_duration=self.message_retention_duration,
            labels=self.labels,
            enable_message_ordering=self.enable_message_ordering,
            expiration_policy=self.expiration_policy,
            filter_=self.filter_,
            dead_letter_policy=self.dead_letter_policy,
            retry_policy=self.retry_policy,
            retry=self.retry,
            timeout=self.timeout,
            metadata=self.metadata,
        )

        self.log.info("Created subscription for topic %s", self.topic)
        return result
Пример #2
0
    def execute(self, context):
        hook = PubSubHook(
            gcp_conn_id=self.gcp_conn_id,
            delegate_to=self.delegate_to,
            impersonation_chain=self.impersonation_chain,
        )

        pulled_messages = hook.pull(
            project_id=self.project_id,
            subscription=self.subscription,
            max_messages=self.max_messages,
            return_immediately=True,
        )

        handle_messages = self.messages_callback or self._default_message_callback

        ret = handle_messages(pulled_messages, context)

        if pulled_messages and self.ack_messages:
            hook.acknowledge(
                project_id=self.project_id,
                subscription=self.subscription,
                messages=pulled_messages,
            )

        return ret
Пример #3
0
    def execute(self, context):
        hook = PubSubHook(gcp_conn_id=self.gcp_conn_id,
                          delegate_to=self.delegate_to)

        self.log.info("Publishing to topic %s", self.topic)
        hook.publish(project_id=self.project_id, topic=self.topic, messages=self.messages)
        self.log.info("Published to topic %s", self.topic)
Пример #4
0
    def poke(self, context):
        hook = PubSubHook(
            gcp_conn_id=self.gcp_conn_id,
            delegate_to=self.delegate_to,
        )

        pulled_messages = hook.pull(
            project_id=self.project_id,
            subscription=self.subscription,
            max_messages=self.max_messages,
            return_immediately=self.return_immediately,
        )

        handle_messages = self.messages_callback or self._default_message_callback

        self._return_value = handle_messages(pulled_messages, context)

        if pulled_messages and self.ack_messages:
            hook.acknowledge(
                project_id=self.project_id,
                subscription=self.subscription,
                messages=pulled_messages,
            )

        return bool(pulled_messages)
Пример #5
0
    def execute(self, context: 'Context') -> None:
        hook = PubSubHook(
            gcp_conn_id=self.gcp_conn_id,
            delegate_to=self.delegate_to,
            impersonation_chain=self.impersonation_chain,
        )

        self.log.info("Creating topic %s", self.topic)
        hook.create_topic(
            project_id=self.project_id,
            topic=self.topic,
            fail_if_exists=self.fail_if_exists,
            labels=self.labels,
            message_storage_policy=self.message_storage_policy,
            kms_key_name=self.kms_key_name,
            retry=self.retry,
            timeout=self.timeout,
            metadata=self.metadata,
        )
        self.log.info("Created topic %s", self.topic)
        PubSubTopicLink.persist(
            context=context,
            task_instance=self,
            topic_id=self.topic,
            project_id=self.project_id or hook.project_id,
        )
Пример #6
0
    def execute(self, context: 'Context') -> None:
        hook = PubSubHook(
            gcp_conn_id=self.gcp_conn_id,
            delegate_to=self.delegate_to,
            impersonation_chain=self.impersonation_chain,
        )

        self.log.info("Publishing to topic %s", self.topic)
        hook.publish(project_id=self.project_id, topic=self.topic, messages=self.messages)
        self.log.info("Published to topic %s", self.topic)
Пример #7
0
    def execute(self, context):
        hook = PubSubHook(gcp_conn_id=self.gcp_conn_id,
                          delegate_to=self.delegate_to)

        self.log.info("Deleting subscription %s", self.subscription)
        hook.delete_subscription(project_id=self.project_id,
                                 subscription=self.subscription,
                                 fail_if_not_exists=self.fail_if_not_exists,
                                 retry=self.retry,
                                 timeout=self.timeout,
                                 metadata=self.metadata)
        self.log.info("Deleted subscription %s", self.subscription)
Пример #8
0
    def execute(self, context):
        hook = PubSubHook(gcp_conn_id=self.gcp_conn_id,
                          delegate_to=self.delegate_to)

        self.log.info("Deleting topic %s", self.topic)
        hook.delete_topic(project_id=self.project_id,
                          topic=self.topic,
                          fail_if_not_exists=self.fail_if_not_exists,
                          retry=self.retry,
                          timeout=self.timeout,
                          metadata=self.metadata)
        self.log.info("Deleted topic %s", self.topic)
Пример #9
0
    def execute(self, context):
        hook = PubSubHook(gcp_conn_id=self.gcp_conn_id,
                          delegate_to=self.delegate_to)

        self.log.info("Creating topic %s", self.topic)
        hook.create_topic(project_id=self.project_id,
                          topic=self.topic,
                          fail_if_exists=self.fail_if_exists,
                          labels=self.labels,
                          message_storage_policy=self.message_storage_policy,
                          kms_key_name=self.kms_key_name,
                          retry=self.retry,
                          timeout=self.timeout,
                          metadata=self.metadata)
        self.log.info("Created topic %s", self.topic)
Пример #10
0
    def poke(self, context):
        hook = PubSubHook(gcp_conn_id=self.gcp_conn_id,
                          delegate_to=self.delegate_to)
        pulled_messages = hook.pull(
            project_id=self.project_id,
            subscription=self.subscription,
            max_messages=self.max_messages,
            return_immediately=self.return_immediately
        )

        self._messages = [MessageToDict(m) for m in pulled_messages]

        if self._messages and self.ack_messages:
            ack_ids = [m['ackId'] for m in self._messages if m.get('ackId')]
            hook.acknowledge(project_id=self.project_id, subscription=self.subscription, ack_ids=ack_ids)
        return self._messages
Пример #11
0
    def execute(self, context: 'Context') -> None:
        hook = PubSubHook(
            gcp_conn_id=self.gcp_conn_id,
            delegate_to=self.delegate_to,
            impersonation_chain=self.impersonation_chain,
        )

        self.log.info("Deleting subscription %s", self.subscription)
        hook.delete_subscription(
            project_id=self.project_id,
            subscription=self.subscription,
            fail_if_not_exists=self.fail_if_not_exists,
            retry=self.retry,
            timeout=self.timeout,
            metadata=self.metadata,
        )
        self.log.info("Deleted subscription %s", self.subscription)
Пример #12
0
    def execute(self, context):
        hook = PubSubHook(gcp_conn_id=self.gcp_conn_id,
                          delegate_to=self.delegate_to)

        self.log.info("Creating subscription for topic %s", self.topic)
        result = hook.create_subscription(
            project_id=self.project_id,
            topic=self.topic,
            subscription=self.subscription,
            subscription_project_id=self.subscription_project_id,
            ack_deadline_secs=self.ack_deadline_secs,
            fail_if_exists=self.fail_if_exists,
            push_config=self.push_config,
            retain_acked_messages=self.retain_acked_messages,
            message_retention_duration=self.message_retention_duration,
            labels=self.labels,
            retry=self.retry,
            timeout=self.timeout,
            metadata=self.metadata)

        self.log.info("Created subscription for topic %s", self.topic)
        return result
Пример #13
0
 def setUp(self):
     with mock.patch(BASE_STRING.format('GoogleBaseHook.__init__'),
                     new=mock_init):
         self.pubsub_hook = PubSubHook(gcp_conn_id='test')