Subversion Repositories SmartDukaan

Rev

Rev 4655 | Blame | Compare with Previous | Last modification | View Log | RSS feed

#!/usr/bin/env python

'''
Created on 23-Feb-2010

@author: ashish
@author: rajveer
@author: Chandranshu
'''
import os
import sys
import optparse

if __name__ == '__main__' and __package__ is None:
    sys.path.insert(0, os.getcwd())

from thrift.protocol.TBinaryProtocol import TBinaryProtocolFactory
from thrift.transport import TSocket, TTransport
from thrift.server import TServer

from shop2020.utils.daemon import Daemon
from shop2020.config.impl.ConfigServerHandler import ConfigServerHandler
from shop2020.thriftpy.config import Configuration

class ConfigServer(Daemon):
    
    def __init__(self, options):
        Daemon.__init__(self, options.pidfile, stdout=options.logfile, stderr=options.logfile)
        self.host = "localhost"
        self.port = 9999
        
        if options.environment:
            self.environment = options.environment
        else:
            self.environment = 'DEV'
            
        self.cfg_file_name = os.path.dirname(os.path.realpath(__file__)) + '/resources/shop2020.cfg'
        
        if options.port:
            self.port = options.port
        
        if options.cfg_file_name:
            self.cfg_file_name = options.cfg_file_name
            
        
    def run(self):
        handler = ConfigServerHandler(self.cfg_file_name, self.environment)
        processor = Configuration.Processor(handler)
        transport = TSocket.TServerSocket(self.host, self.port)
        tfactory = TTransport.TFramedTransportFactory()
        pfactory = TBinaryProtocolFactory()
        server = TServer.TThreadedServer(processor, transport, tfactory, pfactory)
        print "Starting config server with config params, port "+ str(self.port)+" file "+ self.cfg_file_name
        sys.stdout.flush()
        server.serve()
        print "Server functioning" 

def main():
    parser = optparse.OptionParser()
    parser.add_option("-f", "--file", dest="cfg_file_name",
                   type="string",
                   help="Read the configuration from FILE",
                   metavar="FILE")
    parser.add_option("-p", "--port", dest="port",
                      type="int",
                      help="Start config server on PORT",
                      metavar="PORT")
    parser.add_option("-e", "--env", dest="environment",
                      type="string",
                      help="Load properties for ENV environment",
                      metavar="ENV")
    parser.add_option("-l", "--logfile", dest="logfile",
                      type="string",
                      help="Log all output to LOG_FILE",
                      )
    parser.add_option("-i", "--pidfile", dest="pidfile",
                      type="string",
                      help="Write the PID to pidfile")
    (options, args) = parser.parse_args()
    config_server = ConfigServer(options)
    if len(args) == 0:
        config_server.run()
    elif len(args) == 1:
        if 'start' == args[0]:
            config_server.start()
        elif 'stop' == args[0]:
            config_server.stop()
        elif 'restart' == args[0]:
            config_server.restart()
        else:
            print "Unknown command"
            sys.exit(2)
        sys.exit(0)
    else:
        print "usage: %s start|stop|restart %s" % sys.argv[0], parser.get_usage()
        sys.exit(2)

if __name__ == '__main__':
    main()