-
Notifications
You must be signed in to change notification settings - Fork 1
/
BaseWorker.py
113 lines (86 loc) · 3.67 KB
/
BaseWorker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
__author__ = 'voldy'
import threading
import Queue
import time, random
import json
import unirest
import pymysql
# change the no of people adding bhaji after asking customer.
# we have only one now, we can change
WORKERS = 1
# Create thread class
class Worker(threading.Thread):
# constructor
def __init__(self, queue):
self.__queue = queue
threading.Thread.__init__(self)
self.connection = pymysql.connect("dbinstance-sjsu-amogh.cyht8ykut6xk.us-west-1.rds.amazonaws.com","voldy","voldysjsu","cmpe275")
def run(self):
print "Base Worker Listening"
while 1:
# print "Hey Im working"
#Keep listening there is no customer yet in store
if orderQueue.empty():
#Chill
time.sleep(random.randint(10, 100) / 80.0)
continue
else:
#Hey there is a Customer in my shop.
item = self.__queue.get()
#for demo sleep
time.sleep(random.randint(10, 100) / 50.0)
#parse to get Customer Info
customer_channel = parseInfo(item)
print "Connecting to "+customer_channel
print "Asking Customer (Burrito/Bowl)"
#for demo sleep
time.sleep(random.randint(10, 100) / 50.0)
#Lets ask him his requirements Burrito/Bowl
responseBase = unirest.get("http://"+customer_channel+"/getBase", headers={ "Accept": "application/json" },
params={ "base": "Tortilla/Bowl" })
#If customer replies with his choice, Process order and send to next worker
if responseBase.code==200:
print "I will add Delicious "+responseBase.body+" for you !"
baseValue = responseBase.body
print "Asking Customer (Brown/White Rice)"
#for demo sleep
time.sleep(random.randint(10, 100) / 50.0)
#Lets ask user which type of Rice he wants.
responseRice = unirest.get("http://"+customer_channel+"/getRice", headers={ "Accept": "application/json" },
params={ "rice": "Brown,White" })
#If customer replies with his choice, Process order and send to next worker
if responseRice.code==200:
print "I will add Delicious "+responseRice.body+" for you !"
self.cursor = self.connection.cursor()
self.cursor.execute("INSERT INTO Orders(CustomerName, Stage ) VALUES ( '{0}', '{1}')".format(parseCustomerInfo(item),"1"))
self.connection.commit()
self.cursor.close()
riceValue = responseRice.body
sendToNextWorker(item,baseValue,riceValue)
orderQueue = Queue.Queue(0)
def startWorker():
print "Start Worker Thread"
Worker(orderQueue).start()
'''
#end kaam
for i in range(WORKERS):
orderQueue.put(None) # add end-of-queue markers
'''
def parseInfo(item):
data = json.loads(item)
return data['clientChannel']
def parseCustomerInfo(item):
data = json.loads(item)
return data['customerName']
def sendToNextWorker(item,baseValue,riceValue):
data = json.loads(item)
data["base"] = baseValue
data["rice"] = riceValue
datadumps = json.dumps(data)
customer_channel = parseInfo(item)
print "Send to MeatWorker:"
#Send to next worker
response = unirest.post("http://localhost:8081/send", headers={"Accept": "application/json"},
params=datadumps)
#print response.code
return response