-
Notifications
You must be signed in to change notification settings - Fork 0
/
elasticsearch_queuer_server.py
96 lines (79 loc) · 2.46 KB
/
elasticsearch_queuer_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
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
94
95
96
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import cherrypy
import getopt
import sys
import json
from datetime import datetime
from redis_client import RedisClient
from redis_conf import redis_conf
HOST = '0.0.0.0'
PORT = 9902
THREAD_POOL = 1000
ENCODE_ON = True
ENCODING = 'utf-8'
"""
Elasticsearch persistor rest api server
"""
class Root:
@cherrypy.expose
def index(self):
return "Elasticsearch persistor is alive!!"
@cherrypy.expose
def polling(self):
condition = True
minute = datetime.now().minute
if(minute % 5==0):
return json.dumps({"status":"DONE", "minute":minute})
else:
return json.dumps({"status":"PROCESSING", "minute": minute})
@cherrypy.expose
class StringGeneratorWebService(object):
@cherrypy.tools.accept(media='text/plain')
def GET(self):
return "Elasticsearch persistor is alive!!"
@cherrypy.tools.json_in()
@cherrypy.tools.json_out()
def POST(self):
doc = cherrypy.request.json
print("Received doc: '%s'", doc)
rc = RedisClient(redis_conf["host"], redis_conf["port"], redis_conf["db"], redis_conf["collection"])
dumped_doc = json.dumps(doc)
rc.put(dumped_doc)
return {"received doc": dumped_doc}
def usage(command):
print("""
%s [options]
options:
--help Show help
""" % command)
def main(argv):
# Command line arguments
try:
optlist , args = getopt.getopt(argv , 'p:b:' , ['help'])
for opt, value in optlist:
if opt == '--help':
usage(argv[0])
sys.exit()
conf = {
'/': {
'request.dispatch': cherrypy.dispatch.MethodDispatcher(),
'tools.sessions.on': True,
'tools.response_headers.on': True,
'tools.response_headers.headers': [('Content-Type', 'text/plain')],
},
'global': {
'server.socket_host': HOST,
'server.socket_port': PORT,
'server.thread_pool': THREAD_POOL,
'tools.encode.on': ENCODE_ON,
'tools.encode.encoding': ENCODING
}
}
cherrypy.quickstart(StringGeneratorWebService(), config=conf)
except getopt.GetoptError as err:
logging.error(err)
usage()
sys.exit(2)
if __name__ == '__main__':
main(sys.argv[1:])