-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
65 lines (48 loc) · 1.77 KB
/
server.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
#!/usr/bin/env python
import pika
import pymongo
import json
import datetime
import signal
import sys
def signal_handler(signum, frame):
"""
Suppress exception on CTRL+C
"""
print "SIGINT received, exiting ..."
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
mongo_client = pymongo.MongoClient()
db = mongo_client['auto_test_numbers']
conn = pika.BlockingConnection(pika.ConnectionParameters(
host='localhost', heartbeat_interval=600
))
channel = conn.channel()
channel.queue_declare(queue='numbers')
print '[*] Waiting for messages. To exit press CTRL+C'
def callback(ch, method, properties, body):
numbers_to_add = json.loads(body)
print "[x] New request has been received at {}".format(
datetime.datetime.now().replace(microsecond=0).__str__()
)
for dst, dst_data in numbers_to_add.iteritems():
dst_collection = db.get_collection(dst)
for n, setup_time in dst_data:
setup_time = datetime.datetime.strptime(
setup_time.split('.')[0], '%Y-%m-%d %H:%M:%S'
)
number = dst_collection.find_one({"_id": n})
if not number:
dst_collection.insert({"_id": n, "setup_time": setup_time})
elif number["setup_time"] < setup_time:
dst_collection.update_one(
{"_id": n}, {"$set": {"setup_time": setup_time}}
)
#dst_collection.find_one_and_update(
# {"_id": n}, {"$set": {"setup_time": setup_time}}, upsert=True
#)
print "[x] Request has been processed at {}".format(
datetime.datetime.now().replace(microsecond=0).__str__()
)
channel.basic_consume(callback, queue='numbers', no_ack=True)
channel.start_consuming()