8000 Ruggedize str handling by cookingkode · Pull Request #8 · cookingkode/woof · GitHub
[go: up one dir, main page]

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions tests/test_transactions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# -*- coding: utf-8 -*-
import os
import logging
import time
import thread
from woof.transactions import TransactionLogger

logging.basicConfig(
format='%(asctime)s.%(msecs)s:%(name)s:%(thread)d:%(levelname)s:%(process)d:%(message)s',
filename='/tmp/kafkalog',
level=logging.INFO
)

logger = logging.getLogger('kafka')
logger.setLevel(logging.INFO)

srv = os.getenv("GOMSG_SRV", "localhost:9092")

stime = time.time()

# Instantiate
# Should be a long lived object
# async would be True for performance, if needed
# but in fringe cases if there is a restart, msg might not be deliverd
tr = TransactionLogger(srv, "dummy_vertical1", async=True)
print "Time taken for connection: ", time.time() - stime



def thread_test():
stime = time.time()
tr.New(txn_id="gofld3434",
amount=3500,
skus=["vcid_1", "vhid_1"],
detail="{'foo':'bar'}",
userid= u'मेरा नाम',
email="r1@gmail.com",
phone="8984758345345")
print "Time taken to send one message: ", time.time() - stime

# Modify
tr.Modify(txn_id="gofld3434",
amount=4000,
detail="{'foo':'bar', 'foo1':'bar1'}",
phone="8984758345345")

# Cancel
tr.Cancel(txn_id="gofld3434",
phone="8984758345345")

# Fulfil
tr.Fulfil(txn_id="gofld3434",
skus=[u'aaaàçççñññ'],
userid='मेरा नाम',
phone="8984758345345")

for i in range(2):
thread.start_new_thread(thread_test,())


# sleep to allow msg to go
time.sleep(60)
71 changes: 71 additions & 0 deletions woof/transactions.py
CCF6
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
from woof.partitioned_producer import PartitionedProducer
import socket
import time
import logging
import threading

log = logging.getLogger("kafka")
woof_tls = threading.local()


class TransactionLogger(object):
def __init__(self, broker, vertical, host=socket.gethostname(), async=False):
self.broker = broker
self.this_host = host
self.vertical = vertical
self.async = async
self.topic = _get_topic_from_vertical(vertical)

def New(self, txn_id, amount, skus, detail="#", userid="#", email="#", phone="#"):
self._send_log("NEW", txn_id, amount, skus, detail, userid, email, phone)

def Modify(self, txn_id, amount="#", skus=[], detail="#", userid="#", email="#", phone="#"):
self._send_log("MODIFY", txn_id, amount, skus, detail, userid, email, phone)

def Cancel(self, txn_id, amount="#", skus=[], detail="#", userid="#", email="#", phone="#"):
self._send_log("CANCEL", txn_id, amount, skus, detail, userid, email, phone)

def Fulfil(self, txn_id, amount="#", skus=[], detail="#", userid="#", email="#", phone="#"):
self._send_log("FULFIL", txn_id, amount, skus, detail, userid, email, phone)

def _send_log(self, verb, txn_id, amount, skus, detail="#", userid="#", email="#", phone="#"):
msg = self._format_message(verb, txn_id, amount, skus, detail, userid, email, phone)
log.info("[transactions log] topic %s txnid %s msg %s \n", self.topic, txn_id, msg)
try:
woof_tls.producer.send(self.topic, txn_id, msg)
except AttributeError:
woof_tls.producer = PartitionedProducer(self.broker, async=self.async)
self._send_log(verb, txn_id, amount, skus, detail, userid, email, phone)

def _format_message(self, verb, txn_id, amount, skus, detail, userid, email, phone):
"""
Generates log message.
"""
separator = '\t'

safe_skus = [_make_kafka_safe(x) for x in skus]
skus_as_string = ",".join(safe_skus)

return separator.join([self.this_host,
str(time.time()),
verb,
_make_kafka_safe(txn_id),
_make_kafka_safe(amount),
skus_as_string,
_make_kafka_safe(detail),
_make_kafka_safe(userid),
_make_kafka_safe(email),
_make_kafka_safe(phone)])


def _get_topic_from_vertical(vertical):
return "_".join(["TRANSACTIONS", vertical])


def _make_kafka_safe(input):
if type(input) != unicode:
input = str(input)
input = input.decode('utf-8')
return input.encode('ascii','ignore')
else:
return input.encode('ascii','ignore')
0