Subversion Repositories SmartDukaan

Rev

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

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