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 24-May-2011
2
Created on 24-May-2011
3
@author: Varun Gupta
3
@author: Varun Gupta
4
'''
4
'''
-
 
5
import optparse
-
 
6
import sys
-
 
7
import os
-
 
8
 
5
from shop2020.config.client.ConfigClient import ConfigClient
9
if __name__ == '__main__' and __package__ is None:
6
from shop2020.thriftpy.model.v1.user import PromotionService
10
    sys.path.insert(0, os.getcwd())
-
 
11
 
7
from thrift.transport import TSocket, TTransport
12
from thrift.transport import TSocket, TTransport
8
from thrift.protocol.TBinaryProtocol import TBinaryProtocolFactory
13
from thrift.protocol.TBinaryProtocol import TBinaryProtocolFactory
9
from thrift.server import TServer
14
from thrift.server import TServer
10
from shop2020.model.v1.user.impl.PromotionServiceHandler import PromotionServiceHandler
-
 
11
 
15
 
12
host_name = 'localhost'
16
from shop2020.utils.daemon import Daemon
13
port = 9005
17
from shop2020.config.client.ConfigClient import ConfigClient
14
dbname = 'user'
18
from shop2020.thriftpy.model.v1.user import PromotionService
15
db_hostname='localhost'
19
from shop2020.model.v1.user.impl.PromotionServiceHandler import PromotionServiceHandler
16
 
20
 
17
def main():
-
 
18
    #get the config client
-
 
19
    try:
-
 
20
        config_client = ConfigClient()
-
 
21
        host_name = config_client.get_property('promotion_service_server_host')
-
 
22
        port = config_client.get_property('promotion_service_server_port')
-
 
23
        dbname = config_client.get_property('promotion_service_dbname')
-
 
24
        db_hostname = config_client.get_property('promotion_service_db_hostname')
-
 
25
    except:
-
 
26
        #error while spawning the config server
-
 
27
        host_name = 'localhost'
21
class PromotionServer(Daemon):
28
        port = 9005
-
 
29
        dbname = 'user'
-
 
30
        db_hostname='localhost'
-
 
31
    
22
    
-
 
23
    def __init__(self, logfile='/var/log/services/promotion.log', pidfile='/tmp/promotion-server.pid'):
-
 
24
        Daemon.__init__(self, pidfile, stdout=logfile, stderr=logfile)
-
 
25
 
-
 
26
    def run(self):
-
 
27
        #get the config client
-
 
28
        try:
-
 
29
            config_client = ConfigClient()
-
 
30
            host_name = config_client.get_property('promotion_service_server_host')
-
 
31
            port = config_client.get_property('promotion_service_server_port')
-
 
32
            dbname = config_client.get_property('promotion_service_dbname')
-
 
33
            db_hostname = config_client.get_property('promotion_service_db_hostname')
-
 
34
        except:
-
 
35
            #error while spawning the config server
-
 
36
            host_name = 'localhost'
-
 
37
            port = 9005
-
 
38
            dbname = 'user'
-
 
39
            db_hostname='localhost'
-
 
40
        
32
    handler = PromotionServiceHandler(dbname, db_hostname)
41
        handler = PromotionServiceHandler(dbname, db_hostname)
33
    processor = PromotionService.Processor(handler)
42
        processor = PromotionService.Processor(handler)
34
    transport = TSocket.TServerSocket(port=port)
43
        transport = TSocket.TServerSocket(port=port)
35
    tfactory = TTransport.TFramedTransportFactory()
44
        tfactory = TTransport.TFramedTransportFactory()
36
    pfactory = TBinaryProtocolFactory()
45
        pfactory = TBinaryProtocolFactory()
37
    server = TServer.TThreadedServer(processor, transport, tfactory, pfactory)
46
        server = TServer.TThreadedServer(processor, transport, tfactory, pfactory)
38
    print "Starting Promotion Service at, port " + str(port) + " host " + host_name
47
        print "Starting Promotion Service at, port " + str(port) + " host " + host_name
-
 
48
        sys.stdout.flush()
39
    server.serve()
49
        server.serve()
40
    print "Server functioning"
50
        print "Server functioning"
41
    
51
    
42
if __name__ == '__main__':
-
 
43
    main()
-
 
44
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 = PromotionServer(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)
-
 
79
45
80