-
Notifications
You must be signed in to change notification settings - Fork 0
/
DatabaseOp.py
87 lines (76 loc) · 2.95 KB
/
DatabaseOp.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
from celery import Celery
import MySQLdb
import datetime
import memcache
from db_op import DB
import json
app = Celery('tasks', broker='amqp://guest@localhost//')
class Caching(object):
def __init__(self, hostname="127.0.0.1", port="11211"):
self.hostname = "%s:%s" % (hostname, port)
self.server = memcache.Client([self.hostname])
def set(self, key, value, expiry=900):
self.server.set(key, value, expiry)
def get(self, key):
return self.server.get(key)
def delete(self, key):
self.server.delete(key)
@app.task
def UpdateDelivery(transactionId, subscriber, status, shortcode):
db = MySQLdb.connect("localhost", "meh.mous", "5tgbNHY^", "mmp" )
cursor = db.cursor()
result = cursor.execute('''UPDATE outbound
SET status = %s, description = %s, updated_at = %s
WHERE (transactionid = %s)''', (Delivery(status), FindDescription(status), datetime.datetime.now(),transactionId))
WriteToFile.delay(transactionId, result)
db.commit()
db.close()
@app.task
def update_delivery_json(json_string):
db = DB()
list_delivery = json.loads(json_string)
for data in list_delivery:
checkcache = Caching()
s = checkcache.get(str(data['Id']))
if s is None:
checkcache.set(str(data['Id']), data['subscriber'])
cursor = db.query('''UPDATE outbound
SET status = %s, description = %s, updated_at = %s
WHERE (transactionid = %s)''', (Delivery(data['status']), FindDescription(data['status']), datetime.datetime.now(),data['Id']))
WriteToFile.delay(data['Id'], cursor)
@app.task
def WriteToFile(TransactionId,Result):
print "Hello"
f = open('Log/myfile.out','a')
f.write('update DataBase with transactionId: %s Successfull:{%s} at :%s\n' % (TransactionId, Result, datetime.datetime.now()))
f.close()
def Delivery(x):
return {
'1': 'delivered',
1: 'delivered',
'2': 'failed',
2: 'failed',
'3': 'failed',
3: 'failed',
'4': 'failed',
4: 'failed',
5: 'failed',
'5': 'failed',
'6': 'error Status',
6: 'error Status',
}.get(x, 'ocs')
def FindDescription(x):
return {
'1': 'Message Reported As Delivered',
1: 'Message Reported As Delivered',
'2': 'Message Reported As Failed With Error Code: %s' % x,
2: 'Message Reported As Failed With Error Code: %s' % x,
'3': 'Message Reported As Failed With Error Code: %s' % x,
3: 'Message Reported As Failed With Error Code: %s' % x,
'4': 'Message Reported As Failed With Error Code: %s' % x,
4: 'Message Reported As Failed With Error Code: %s' % x,
'5': 'Message Reported As Failed With Error Code: %s' % x,
5: 'Message Reported As Failed With Error Code: %s' % x,
'6': 'error Status',
6: 'error Status',
} .get(x, 'Message Reported As OCS With Error Code: %s' % x)