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