Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
5944 mandeep.dh 1
'''
2
Created on 25-Mar-2010
3
 
4
@author: ashish
5
'''
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
 
13
from thrift.transport import TSocket, TTransport
14
from thrift.protocol.TBinaryProtocol import TBinaryProtocolFactory
15
from thrift.server import TServer
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
20
from shop2020.model.v1.inventory.impl.InventoryServiceHandler import InventoryServiceHandler
21
 
22
class InventoryHistoryServer(Daemon):
23
 
24
    def __init__(self, logfile='/var/log/services/inventoryHistory.log', pidfile='/tmp/inventory-history-server.pid'):
25
        Daemon.__init__(self, pidfile, stdout=logfile, stderr=logfile)
26
 
27
    def run(self):
28
        #get the config client
29
        try:
30
            config_client = ConfigClient()
31
            host_name = config_client.get_property('inventory_history_service_server_host')
32
            port = config_client.get_property('inventory_history_service_server_port')
33
            dbname = config_client.get_property('catalog_service_dbname')
34
            db_hostname = config_client.get_property('inventory_history_service_db_hostname')
35
        except:
36
            #error while spawning the config server
37
            host_name = 'localhost'
38
            port = 9015
39
            dbname = 'catalog'
40
            db_hostname='localhost'
41
 
42
        handler = InventoryServiceHandler(dbname, db_hostname)
43
        processor = InventoryService.Processor(handler)
44
        transport = TSocket.TServerSocket(port=port)
45
        tfactory = TTransport.TFramedTransportFactory()
46
        pfactory = TBinaryProtocolFactory()
47
        server = TServer.TThreadedServer(processor, transport, tfactory, pfactory)
48
        print "Starting InventoryHistoryService at, port "+ str(port)+" host "+host_name
49
        server.serve()
50
        print "Server functioning"
51
 
52
if __name__ == "__main__":
53
    parser = optparse.OptionParser()
54
    parser.add_option("-l", "--logfile", dest="logfile",
55
                      type="string",
56
                      help="Log all output to LOG_FILE",
57
                      )
58
    parser.add_option("-i", "--pidfile", dest="pidfile",
59
                      type="string",
60
                      help="Write the PID to pidfile")
61
    (options, args) = parser.parse_args()
62
    daemon = InventoryHistoryServer(options.logfile, options.pidfile)
63
    if len(args) == 0:
64
        daemon.run()
65
    elif len(args) == 1:
66
        if 'start' == args[0]:
67
            daemon.start()
68
        elif 'stop' == args[0]:
69
            daemon.stop()
70
        elif 'restart' == args[0]:
71
            daemon.restart()
72
        else:
73
            print "Unknown command"
74
            sys.exit(2)
75
        sys.exit(0)
76
    else:
77
        print "usage: %s start|stop|restart" % sys.argv[0]
78
        sys.exit(2)