-
Notifications
You must be signed in to change notification settings - Fork 0
/
webservice.py
58 lines (43 loc) · 1.16 KB
/
webservice.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
import threading
from flask import Flask, request
webservice = Flask(__name__)
@webservice.route("/health")
def root():
return 'Alive.'
from crawler import Merchant
lock = threading.Lock()
merchants = []
@webservice.route("/work")
def work():
lock.acquire()
for merchant in merchants:
merchant.update_all_payments();
merchant.charge_payments();
merchant.print_payments();
lock.release()
return 'done'
@webservice.route("/merchants", methods=['POST'])
def add_merchant():
body = request.get_json();
print body
_id = body['id']
token = body['token']
lock.acquire()
if _id in [merchant._id for merchant in merchants]:
lock.release()
return 'Merchant %s already on database.' % _id
merchant = Merchant(_id, token)
print merchant.token
merchants.append(merchant)
lock.release()
return 'Merchant %s added.' % _id
import time
class CrawlerWork(threading.Thread):
def __init__(self):
"""TODO: to be defined1. """
threading.Thread.__init__(self)
def run(self):
while True:
work()
print "working..."
time.sleep(1)