-
Notifications
You must be signed in to change notification settings - Fork 1
/
GarnishWorker.py
96 lines (75 loc) · 3.03 KB
/
GarnishWorker.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
__author__ = 'voldy'
import threading
import Queue
import time, random
import json
import unirest
import pymysql
#import requests
# 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 "Garnish Worker Listening"
while 1:
# print "Hey Im working: MeatWorker"
#Keep listening there is no customer yet in store
if garnishQueue.empty():
#Chill
time.sleep(random.randint(10, 100) / 50.0)
continue
else:
#Hey there is a Customer in my shop.
item = self.__queue.get()
#parse to get Customer Info
customer_channel = parseInfo(item)
# print "Connecting to "+customer_channel
print "Asking Customer (Cheese/Guacamole/Lettuce)"
#for demo sleep
time.sleep(random.randint(10, 100) / 50.0)
#Lets ask him his requirements Burrito/Bowl
responseGarnish = unirest.get("http://"+customer_channel+"/getGarnish", headers={ "Accept": "application/json" },
params={ "sauce": "Cheese,Guacamole,Lettuce" })
#If customer replies with his choice, Process order and send to next worker
if responseGarnish.code==200:
garnishValue = responseGarnish._body
self.cursor = self.connection.cursor()
print "I will add Delicious "+responseGarnish.body+" for you !"
self.cursor.execute("UPDATE Orders SET Stage ='{0}' WHERE CustomerName = '{1}'".format("4",parseCustomerInfo(item)))
self.connection.commit()
self.cursor.close()
sendToNextWorker(item,garnishValue)
garnishQueue = Queue.Queue(0)
def startWorker():
print "Start Worker Thread"
Worker(garnishQueue).start()
def parseInfo(item):
#print item
data = json.loads(item)
# data = requests.get(item).json()
# print data['clientChannel']
return data['clientChannel']
def parseCustomerInfo(item):
data = json.loads(item)
return data['customerName']
def sendToNextWorker(item,garnishValue):
data = json.loads(item)
data["garnish"] = garnishValue
datadumps = json.dumps(data)
customer_channel = parseInfo(item)
print "Send to Cashier:"
#for demo sleep
time.sleep(random.randint(10, 100) / 50.0)
#Send to next worker
response = unirest.post("http://localhost:8084/send", headers={"Accept": "application/json"},
params=datadumps)
#print response.code
return response
#return data