Subversion Repositories SmartDukaan

Rev

Rev 3915 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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