Search code examples
pythongoogle-cloud-pubsub

PubSub Google - I am unable to publish messages in unique signatures


I have the following code snippet in python that publishes messages to pubsub queues from the content of a file that falls into cloud storage. The logic is as follows: If the file falls in the ANALYTICS folder, it should allocate the messages in the Analytics subscription. If it falls in the MARKETING folder, it should publish in the Marketing subscription. But when it reads the file, it is publishing in both subscriptions at the same time. I have doubts how to fix this.

    # Publish messages to PubSub
    publisher = pubsub.PublisherClient()
    topic = publisher.topic_path('dev-data-31ce', 'da-topic-dev-natdes')
    count = 0
    signature = None
    log.info('Identifying destination messages signature')
    
    for line in lines:
      data = line.decode()
      m_data = json.dumps(data).encode('utf-8')
      
      if 'ANALYTICS' in filename:
        signature = 'Analytics'
        publisher.publish(topic, data=m_data, subscription=analytics)
        count = count+1
      else:
        signature = 'Marketing'
        publisher.publish(topic, data=m_data,subscription=marketing)
        count = count+1

I have a problem with setting up a file stream. I have two queues called "ANALYTICS" and "MARKETING", and a file source stream. If a file drops into the ANALYTICS folder, it should allocate messages into the ANALYTICS subscription, and if it drops into the MARKETING folder, it should publish to the MARKETING subscription. When publishing to ANALYTICS for example, only the ANALYTICS subscription should have messages in the queue.


Solution

  • You don't publish messages to subscriptions, you publish them to topics. In both calls to publisher.publish, you are using the same topic. By setting "subscription=X", all you are doing is passing along an attribute in the message with the key "subscription" and the value either "analytics" or "marketing".

    You need to do one of two things:

    1. Publish to two different topics, each with a single subscription.
    2. Publish to the same topic with two subscriptions where each subscription uses a filter to only accept messages with the "subscription" attribute set to the appropriate value.