Exemplo n.º 1
0
def subscriber():
    while True:
        counter = 10
        temp1 = []
        while (counter != -1):

            pubsub_client = pubsub.Client()
            topic_name = 'my-new-topic'
            topic = pubsub_client.topic(topic_name)
            subscription_name = 'sub1'
            subscription = topic.subscription(subscription_name)

            #subscription.create()

            #for subscription in topic.list_subscriptions():
            #	   print(subscription.name)

            #logging.debug('Subscription {} created on topic {}.'.format(
            #	subscription.name, topic.name))

            try:
                results = subscription.pull(return_immediately=True)
                if counter != 0:
                    #print counter
                    print('Received {} messages.'.format(len(results)))

                    for ack_id, message in results:
                        #print('* {}: {}, {}'.format(
                        #message.message_id, message.data, message.attributes))
                        temp1.append(str(message.data))
                    #var=var+1
                    #print results
                    #print counter
                    #print b1
                    #ts = self.request.get("ts")
                else:
                    i = 0
                    while (i != 10):
                        #print len(temp1)
                        #print i
                        #print temp1[i]
                        try:
                            b = json.loads(temp1[i])
                            #print('json load: {}'.format(b))
                        except:
                            print('Cannot able to load')
                            print i

                        try:
                            #defing dataset variables
                            dataset_name = 'searce_poc_vuukle'
                            table_name = 'page_impressions'
                            today = b["timestamp"]
                            table_name = "%s$%s" % (table_name, today)

                            #print "hello1"
                            #putting data into bigquery
                            bigquery_client = bigquery.Client()
                            dataset = bigquery_client.dataset(dataset_name)
                            table = dataset.table(table_name)
                            data = b
                            #time_stamp1=time.strftime("%c")

                            #data['timestamp'] = time_stamp1

                            # Reload the table to get the schema.
                            table.reload()

                            ## get the names of schema
                            temp = list()
                            for key in data:
                                temp.append(data[key])
                                #rows = [data]
                                #print rows
                            errors = table.insert_data([temp])
                            if not errors:
                                logging.debug('Loaded 1 row into {}:{}'.format(
                                    dataset_name, table_name))
                            else:
                                logging.error(errors)
                        except:
                            print "cannot load"
                        i = i + 1

                    # # # Acknowledge received messages. If you do not acknowledge, Pub/Sub will
                    # # # redeliver the message.
                if results:
                    subscription.acknowledge(
                        [ack_id for ack_id, message in results])
                    #print( "one acknowledged")
            except:
                print("next")
                #continue
            counter = counter - 1
Exemplo n.º 2
0
 def start_bundle(self):
   if self._topic is None:
     self._topic = pubsub.Client(project=self.project).topic(
         self.topic_name)
   self._buffer = []
Exemplo n.º 3
0
 def __init__(self):
     self.psc = pubsub.Client(project=utility.project_id())
import os
import time

# setup for the pub sub client
os.environ['GOOGLE_CLOUD_DISABLE_GRPC'] = 'true'
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = os.path.expanduser(
    '~') + '/' + '\Desktop\My_pub_sub\google_cloud_personal.json'
if os.environ.get('PUBSUB_EMULATOR_HOST'):
    del os.environ['PUBSUB_EMULATOR_HOST']
from google.cloud import pubsub
# pip install google-cloud-pubsub

pubsub_client = pubsub.Client()


def receive_message_from_pub_sub(topic_name, subscription_name):
    topic = pubsub_client.topic(topic_name)
    subscription = topic.subscription(subscription_name)
    results = subscription.pull()
    print('Received {} messages.'.format(len(results)))
    for ack_id, message in results:
        print('* {}: {}, {}'.format(
            message.message_id, message.data, message.attributes))

        print 'processed the message ' + repr(message.message_id)

    #if results:
        #subscription.acknowledge([ack_id for ack_id, message in results])


def process_order(data):
Exemplo n.º 5
0
def start_sender():
    try:
        global __network_status
        global __db_con
        global __db_cur
        global __topic
        global __devices
        global __network_status

        ################################################################################################################
        # database connection                                                                                          #
        ################################################################################################################
        __db_con = lite.connect('fermentation.db')
        __db_cur = __db_con.cursor()

        ################################################################################################################
        # Network d-bus communitcation                                                                                 #
        ################################################################################################################
        NetworkManager.NetworkManager.connect_to_signal(
            'DeviceAdded', device_add_remove, **__d_args)
        NetworkManager.NetworkManager.connect_to_signal(
            'DeviceRemoved', device_add_remove, **__d_args)

        for dev in NetworkManager.NetworkManager.GetDevices():
            print("DEVICE!!")
            dev.connect_to_signal('StateChanged', device_state_change,
                                  **__d_args)
            __devices[dev.object_path] = {}
            if dev.DeviceType == NetworkManager.NM_DEVICE_TYPE_ETHERNET and \
                            NetworkManager.const('device_state', dev.State) == "activated":
                __devices[dev.object_path]["active"] = True
                __devices[dev.object_path]["type"] = "Ethernet"
                __network_status.ethernet = True
            if dev.DeviceType == NetworkManager.NM_DEVICE_TYPE_WIFI and \
                            NetworkManager.const('device_state', dev.State) == "activated":
                __devices[dev.object_path]["active"] = True
                __devices[dev.object_path]["type"] = "Wifi"
                __network_status.wifi = True

        print "Starting network manager thread"
        thread = Thread(target=start_network_manager_loop)
        thread.start()

        ################################################################################################################
        # Send fermentation settings                                                                                   #
        ################################################################################################################
        settings_thread = Thread(target=send_fermentation_settings)
        settings_thread.start()

        ################################################################################################################
        # PubSub Cloud topic setup                                                                                     #
        ################################################################################################################
        print "Starting cloud connection"
        pubsub_client = pubsub.Client()
        __topic = pubsub_client.topic('temperature')

        ################################################################################################################
        # Local MQTT server                                                                                            #
        ################################################################################################################
        print "Starting local MQTT connection"
        client = mqtt.Client(client_id='PUBSUB', protocol=mqtt.MQTTv31)

        client.on_connect = on_connect
        client.on_message = on_message

        client.connect("127.0.0.1", 1883)
        client.loop_forever()

    except KeyboardInterrupt:
        print("Pressed CTRL+C! :(")
    finally:
        if __db_con:
            __db_con.close()
Exemplo n.º 6
0
def index():
    ps = pubsub.Client()
    #   pulled = subscription.pull(max_messages=2)
    return 'GHS'
Exemplo n.º 7
0
 def _get_subscription(self):
     return pubsub.Client(project=self.project).subscription(self.sub_name)
Exemplo n.º 8
0
def main():

    module = AnsibleModule(
        argument_spec=dict(
            topic=dict(type='str', required=True),
            state=dict(type='str', default='present', choices=['absent', 'present']),
            publish=dict(type='list'),
            subscription=dict(type='dict'),
            service_account_email=dict(type='str'),
            credentials_file=dict(type='str'),
            project_id=dict(type='str'),
        ),
    )

    if not HAS_PYTHON26:
        module.fail_json(
            msg="GCE module requires python's 'ast' module, python v2.6+")

    if not HAS_GOOGLE_CLOUD_PUBSUB:
        module.fail_json(msg="Please install google-cloud-pubsub library.")

    if not check_min_pkg_version(CLOUD_CLIENT, CLOUD_CLIENT_MINIMUM_VERSION):
        module.fail_json(msg="Please install %s client version %s" % (CLOUD_CLIENT, CLOUD_CLIENT_MINIMUM_VERSION))

    mod_params = {}
    mod_params['publish'] = module.params.get('publish')
    mod_params['state'] = module.params.get('state')
    mod_params['topic'] = module.params.get('topic')
    mod_params['subscription'] = module.params.get('subscription')

    creds, params = get_google_cloud_credentials(module)
    pubsub_client = pubsub.Client(project=params['project_id'], credentials=creds, use_gax=False)
    pubsub_client.user_agent = CLOUD_CLIENT_USER_AGENT

    changed = False
    json_output = {}

    t = None
    if mod_params['topic']:
        t = pubsub_client.topic(mod_params['topic'])
    s = None
    if mod_params['subscription']:
        # Note: default ack deadline cannot be changed without deleting/recreating subscription
        s = t.subscription(mod_params['subscription']['name'],
                           ack_deadline=mod_params['subscription'].get('ack_deadline', None),
                           push_endpoint=mod_params['subscription'].get('push_endpoint', None))

    if mod_params['state'] == 'absent':
        # Remove the most granular resource.  If subscription is specified
        # we remove it.  If only topic is specified, that is what is removed.
        # Note that a topic can be removed without first removing the subscription.
        # TODO(supertom): Enhancement: Provide an option to only delete a topic
        # if there are no subscriptions associated with it (which the API does not support).
        if s is not None:
            if s.exists():
                s.delete()
                changed = True
        else:
            if t.exists():
                t.delete()
                changed = True
    elif mod_params['state'] == 'present':
        if not t.exists():
            t.create()
            changed = True
        if s:
            if not s.exists():
                s.create()
                s.reload()
                changed = True
            else:
                # Subscription operations
                # TODO(supertom): if more 'update' operations arise, turn this into a function.
                s.reload()
                push_endpoint = mod_params['subscription'].get('push_endpoint', None)
                if push_endpoint is not None:
                    if push_endpoint != s.push_endpoint:
                        if push_endpoint == 'None':
                            push_endpoint = None
                        s.modify_push_configuration(push_endpoint=push_endpoint)
                        s.reload()
                        changed = push_endpoint == s.push_endpoint

                if 'pull' in mod_params['subscription']:
                    if s.push_endpoint is not None:
                        module.fail_json(msg="Cannot pull messages, push_endpoint is configured.")
                    (json_output['pulled_messages'], changed) = pull_messages(
                        mod_params['subscription']['pull'], s)

        # publish messages to the topic
        if mod_params['publish'] and len(mod_params['publish']) > 0:
            changed = publish_messages(mod_params['publish'], t)

    json_output['changed'] = changed
    json_output.update(mod_params)
    module.exit_json(**json_output)