| 3915 |
chandransh |
1 |
#!/usr/bin/env python
|
|
|
2 |
|
| 353 |
ashish |
3 |
'''
|
|
|
4 |
Created on 15-Jul-2010
|
|
|
5 |
|
|
|
6 |
@author: ashish
|
| 3915 |
chandransh |
7 |
@author: Chandranshu
|
| 353 |
ashish |
8 |
'''
|
| 3915 |
chandransh |
9 |
import os
|
|
|
10 |
import sys
|
|
|
11 |
import optparse
|
|
|
12 |
|
|
|
13 |
if __name__ == '__main__' and __package__ is None:
|
|
|
14 |
sys.path.insert(0, os.getcwd())
|
|
|
15 |
|
| 353 |
ashish |
16 |
from thrift.transport import TSocket, TTransport
|
|
|
17 |
from thrift.protocol.TBinaryProtocol import TBinaryProtocolFactory
|
|
|
18 |
from thrift.server import TServer
|
| 3915 |
chandransh |
19 |
|
|
|
20 |
from shop2020.utils.daemon import Daemon
|
|
|
21 |
from shop2020.config.client.ConfigClient import ConfigClient
|
| 353 |
ashish |
22 |
from shop2020.helpers.impl.HelperServiceHandler import HelperServiceHandler
|
|
|
23 |
from shop2020.thriftpy.utils import HelperService
|
|
|
24 |
|
| 3915 |
chandransh |
25 |
class HelperServer(Daemon):
|
| 353 |
ashish |
26 |
|
| 3915 |
chandransh |
27 |
def __init__(self, logfile='/var/log/services/helper.log', pidfile='/tmp/helper-server.pid'):
|
|
|
28 |
Daemon.__init__(self, pidfile, stdout=logfile, stderr=logfile)
|
|
|
29 |
|
|
|
30 |
def run(self):
|
|
|
31 |
#get the config client
|
| 4651 |
rajveer |
32 |
config_client = ConfigClient()
|
|
|
33 |
host_name = config_client.get_property('helper_service_server_host')
|
|
|
34 |
port = int(config_client.get_property('helper_service_server_port'))
|
|
|
35 |
dbname = config_client.get_property('helper_service_dbname')
|
|
|
36 |
db_hostname = config_client.get_property('helper_service_db_hostname')
|
| 3915 |
chandransh |
37 |
|
|
|
38 |
handler = HelperServiceHandler(dbname, db_hostname)
|
|
|
39 |
processor = HelperService.Processor(handler)
|
|
|
40 |
transport = TSocket.TServerSocket(port=port)
|
|
|
41 |
tfactory = TTransport.TFramedTransportFactory()
|
|
|
42 |
pfactory = TBinaryProtocolFactory()
|
|
|
43 |
server = TServer.TThreadedServer(processor, transport, tfactory, pfactory)
|
|
|
44 |
print "Starting HelperService at, port " + str(port) + " host " + host_name
|
|
|
45 |
sys.stdout.flush()
|
|
|
46 |
server.serve()
|
|
|
47 |
print "Server functioning"
|
| 353 |
ashish |
48 |
|
| 3915 |
chandransh |
49 |
if __name__ == "__main__":
|
|
|
50 |
parser = optparse.OptionParser()
|
|
|
51 |
parser.add_option("-l", "--logfile", dest="logfile",
|
|
|
52 |
type="string",
|
|
|
53 |
help="Log all output to LOG_FILE",
|
|
|
54 |
)
|
|
|
55 |
parser.add_option("-i", "--pidfile", dest="pidfile",
|
|
|
56 |
type="string",
|
|
|
57 |
help="Write the PID to pidfile")
|
|
|
58 |
(options, args) = parser.parse_args()
|
|
|
59 |
daemon = HelperServer(options.logfile, options.pidfile)
|
|
|
60 |
if len(args) == 0:
|
|
|
61 |
daemon.run()
|
|
|
62 |
elif len(args) == 1:
|
|
|
63 |
if 'start' == args[0]:
|
|
|
64 |
daemon.start()
|
|
|
65 |
elif 'stop' == args[0]:
|
|
|
66 |
daemon.stop()
|
|
|
67 |
elif 'restart' == args[0]:
|
|
|
68 |
daemon.restart()
|
|
|
69 |
else:
|
|
|
70 |
print "Unknown command"
|
|
|
71 |
sys.exit(2)
|
|
|
72 |
sys.exit(0)
|
|
|
73 |
else:
|
|
|
74 |
print "usage: %s start|stop|restart" % sys.argv[0]
|
|
|
75 |
sys.exit(2)
|