Created
July 15, 2017 06:27
-
-
Save slb350/3b082268e8dcbec6429c34a69d58f154 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import boto3 | |
| import json | |
| import signal | |
| import sys | |
| import urllib2 #prep for get request on authcache service | |
| import pymysql | |
| import logging | |
| sqs = boto3.resource('sqs', region_name='us-east-1') | |
| queue = sqs.get_queue_by_name(QueueName='cmb-analytics') | |
| logging.basicConfig(format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p') | |
| log = logging.getLogger(__name__) | |
| class SigKill: | |
| def __init__(self): | |
| signal.signal(signal.SIGINT, self.exit_gracefully) | |
| signal.signal(signal.SIGTERM, self.exit_gracefully) | |
| self.kill_now = False | |
| def exit_gracefully(self, signum, frame): | |
| self.kill_now = True | |
| if __name__ == '__main__': | |
| killer = SigKill() | |
| while not killer.kill_now: | |
| for message in queue.receive_messages(WaitTimeSeconds=5, MaxNumberOfMessages=10, MessageAttributeNames=['Name']): | |
| event_text = '' | |
| if message.message_attributes is not None: | |
| event_name = message.message_attributes.get('Name').get('StringValue') | |
| if event_name: | |
| event_text = ' ({0})'.format(event_name) | |
| events = json.loads(message.body) | |
| conn = pymysql.connect(host='', | |
| user='', | |
| password='', | |
| db='', | |
| charset='utf8mb4', | |
| autocommit=True, | |
| cursorclass=pymysql.cursors.DictCursor) | |
| AVAILABLE_FIELDS = ['profile_id', 'event'] | |
| keys_to_write = [] | |
| values_to_write = [] | |
| for field in AVAILABLE_FIELDS: | |
| if field in events: | |
| keys_to_write.append(field) | |
| values_to_write.append(events[field]) | |
| if len(keys_to_write) > 0: | |
| cur = conn.cursor() | |
| table="events" | |
| sql = "INSERT INTO %s (%s) VALUES(%s)" % ( table, ",".join(keys_to_write), ",".join(["'{}'".format(k) for k in values_to_write])) | |
| log.error(sql) | |
| cur.execute(sql) | |
| cur.close() | |
| #conn.close() | |
| else: | |
| print "FUCK" | |
| message.delete() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment