Subversion Repositories SmartDukaan

Rev

Rev 3540 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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