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 06-Apr-2010
2
Created on 06-Apr-2010
3
 
3
 
4
@author: ashish
4
@author: ashish
-
 
5
@author: Chandranshu
5
'''
6
'''
-
 
7
import optparse
-
 
8
import sys
-
 
9
import os
-
 
10
 
6
from shop2020.model.v1.order.impl.OrderServiceHandler import OrderServiceHandler
11
if __name__ == '__main__' and __package__ is None:
7
from shop2020.thriftpy.model.v1.order import TransactionService
12
    sys.path.insert(0, os.getcwd())
-
 
13
 
8
from thrift.transport import TSocket, TTransport
14
from thrift.transport import TSocket, TTransport
9
from thrift.protocol.TBinaryProtocol import TBinaryProtocolFactory
15
from thrift.protocol.TBinaryProtocol import TBinaryProtocolFactory
10
from thrift.server import TServer
16
from thrift.server import TServer
-
 
17
 
-
 
18
from shop2020.utils.daemon import Daemon
-
 
19
from shop2020.model.v1.order.impl.OrderServiceHandler import OrderServiceHandler
-
 
20
from shop2020.thriftpy.model.v1.order import TransactionService
11
from shop2020.config.client.ConfigClient import ConfigClient
21
from shop2020.config.client.ConfigClient import ConfigClient
12
 
22
 
13
host_name = 'localhost'
-
 
14
port = 9002
-
 
15
dbname = 'transaction'
-
 
16
db_hostname='localhost'
-
 
17
 
-
 
18
def main():
-
 
19
    try:
-
 
20
        config_client = ConfigClient()
-
 
21
        host_name = config_client.get_property('transaction_service_server_host')
-
 
22
        port = config_client.get_property('transaction_service_server_port')
-
 
23
        dbname = config_client.get_property('transaction_service_dbname')
-
 
24
        db_hostname = config_client.get_property('transaction_service_db_hostname')
-
 
25
    except:
-
 
26
        #error while spawning the config server
-
 
27
        host_name = 'localhost'
-
 
28
        port = 9002
-
 
29
        dbname = 'transaction'
-
 
30
        db_hostname='localhost'
-
 
31
        
-
 
32
    handler = OrderServiceHandler(dbname, db_hostname)
-
 
33
    processor = TransactionService.Processor(handler)
23
class TransactionServer(Daemon):
34
    transport = TSocket.TServerSocket(port=port)
-
 
35
    tfactory = TTransport.TFramedTransportFactory()
-
 
36
    pfactory = TBinaryProtocolFactory()
-
 
37
    server = TServer.TThreadedServer(processor, transport, tfactory, pfactory)
-
 
38
    print "Starting Transaction Service at, port "+ str(port)+" host "+host_name
-
 
39
    server.serve()
-
 
40
    print "Server functioning"
-
 
41
    
-
 
42
    
24
    
-
 
25
    def __init__(self, logfile='/var/log/services/order.log', pidfile='/tmp/transaction-server.pid'):
-
 
26
        Daemon.__init__(self, pidfile, stdout=logfile, stderr=logfile)
-
 
27
        
-
 
28
    def run(self):
-
 
29
        try:
-
 
30
            config_client = ConfigClient()
-
 
31
            host_name = config_client.get_property('transaction_service_server_host')
-
 
32
            port = config_client.get_property('transaction_service_server_port')
-
 
33
            dbname = config_client.get_property('transaction_service_dbname')
-
 
34
            db_hostname = config_client.get_property('transaction_service_db_hostname')
-
 
35
        except:
-
 
36
            #error while spawning the config server
-
 
37
            host_name = 'localhost'
-
 
38
            port = 9002
-
 
39
            dbname = 'transaction'
-
 
40
            db_hostname='localhost'
-
 
41
            
-
 
42
        handler = OrderServiceHandler(dbname, db_hostname)
-
 
43
        processor = TransactionService.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 Transaction Service at, port " + str(port) + " host " + host_name
-
 
49
        sys.stdout.flush()
-
 
50
        server.serve()
-
 
51
        print "Server functioning"
43
 
52
 
44
if __name__ == '__main__':
-
 
45
    main()
-
 
46
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 = TransactionServer(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)
-
 
77
    else:
-
 
78
        print "usage: %s start|stop|restart" % sys.argv[0]
-
 
79
        sys.exit(2)
-
 
80
47
81