Subversion Repositories SmartDukaan

Rev

Rev 3896 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3893 chandransh 1
'''
2
Created on 24-Oct-2011
3
 
4
@author: Chandranshu
5
'''
6
import optparse
7
import sys
8
import os
9
from shop2020.content.main.handler.ContentHandler import ContentHandler
10
 
11
if __name__ == '__main__' and __package__ is None:
12
    sys.path.insert(0, os.getcwd())
13
 
14
from thrift.transport import TSocket, TTransport
15
from thrift.protocol.TBinaryProtocol import TBinaryProtocolFactory
16
from thrift.server import TServer
17
 
18
from shop2020.utils.daemon import Daemon
19
from shop2020.config.client.ConfigClient import ConfigClient
20
 
21
from shop2020.thriftpy.content import ContentService
22
 
23
class ContentServer(Daemon):
24
 
25
    def __init__(self, logfile='/var/log/services/content.log', pidfile='/tmp/content-server.pid'):
26
        Daemon.__init__(self, pidfile, stdout=logfile, stderr=logfile)
27
 
28
    def run(self):
29
        #get the config client
4651 rajveer 30
        config_client = ConfigClient()
31
        host_name = config_client.get_property('content_service_server_host')
32
        port = int(config_client.get_property('content_service_server_port'))
3893 chandransh 33
 
34
        handler = ContentHandler()
35
        processor = ContentService.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 Content Service at, port "+ str(port)+" host "+host_name
41
        server.serve()
42
        print "Server functioning"
43
 
44
if __name__ == "__main__":
45
    parser = optparse.OptionParser()
46
    parser.add_option("-l", "--logfile", dest="logfile",
47
                      type="string",
48
                      help="Log all output to LOG_FILE",
49
                      )
50
    parser.add_option("-i", "--pidfile", dest="pidfile",
51
                      type="string",
52
                      help="Write the PID to pidfile")
53
    (options, args) = parser.parse_args()
54
    daemon = ContentServer(options.logfile, options.pidfile)
55
    if len(args) == 0:
56
        daemon.run()
57
    elif len(args) == 1:
58
        if 'start' == args[0]:
59
            daemon.start()
60
        elif 'stop' == args[0]:
61
            daemon.stop()
62
        elif 'restart' == args[0]:
63
            daemon.restart()
64
        else:
65
            print "Unknown command"
66
            sys.exit(2)
67
        sys.exit(0)
68
    else:
69
        print "usage: %s start|stop|restart" % sys.argv[0]
70
        sys.exit(2)