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
|
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
from urlparse import urlparse, parse_qsl
from daemon import Daemon
import sys, datetime, logging, json, redis
from insert_tranche_quotes import insert_quotes
from load_intex_collateral import intex_data
import psycopg2
from psycopg2.extras import DictCursor
from globeop import download_data
class MyHandler(BaseHTTPRequestHandler):
def do_GET(self):
if self.path.startswith("/insert_quotes"):
q = urlparse(self.path).query
if q:
args = dict(parse_qsl(q))
insert_quotes(**args)
else:
insert_quotes()
if self.path == "/insert_intex_data":
workdate = str(datetime.date.today())
intex_data(self.server.conn, workdate)
if self.path.startswith("/globeop"):
q = urlparse(self.path).query
if q:
args = dict(parse_qsl(q))
args['workdate'] = datetime.datetime.strptime(args['workdate'], "%Y-%m-%d").date()
download_data(**args)
else:
download_data()
self.send_response(200)
self.end_headers()
def do_POST(self):
length = int(self.headers['content-length'])
d = parse_qsl(self.rfile.read(length).decode('utf-8'))
self.log_message("%s" , json.dumps(d))
workdate = str(datetime.date.today())
pipe = self.server.queue.pipeline()
for dealname, reinvflag in d:
pipe.rpush("tasks", json.dumps(("build_portfolio",
[workdate, dealname, reinvflag])))
pipe.execute()
self.send_response(200)
self.end_headers()
def log_message(self, format, *args):
logging.info("%s - - [%s] %s" %
(self.address_string(),
self.log_date_time_string(),
format%args))
class MyServer(HTTPServer):
def __init__(self, addr, handler, queue, conn):
HTTPServer.__init__(self, addr, handler)
self.queue = queue
self.conn = conn
class MyDaemon(Daemon):
def run(self):
server_address = ('',8000)
logging.basicConfig(filename='/home/share/CorpCDOs/logs/tasks.log', level=logging.INFO)
q = redis.Redis(unix_socket_path='/var/run/redis/redis.sock')
self.conn = psycopg2.connect(database="ET",
user="et_user",
password="Serenitas1",
host="debian",
cursor_factory=DictCursor)
http = MyServer(server_address, MyHandler, q, self.conn)
http.serve_forever()
if __name__=="__main__":
d = MyDaemon('/tmp/tasks.pid')
command = getattr(d, sys.argv[1])
command()
|