Subversion Repositories SmartDukaan

Rev

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

'''
Created on 24-May-2011
@author: Varun Gupta
'''
import optparse
import sys
import os

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

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

from shop2020.utils.daemon import Daemon
from shop2020.config.client.ConfigClient import ConfigClient
from shop2020.thriftpy.model.v1.user import PromotionService
from shop2020.model.v1.user.impl.PromotionServiceHandler import PromotionServiceHandler

class PromotionServer(Daemon):
    
    def __init__(self, logfile='/var/log/services/promotion.log', pidfile='/tmp/promotion-server.pid'):
        Daemon.__init__(self, pidfile, stdout=logfile, stderr=logfile)

    def run(self):
        #get the config client
        config_client = ConfigClient()
        host_name = config_client.get_property('promotion_service_server_host')
        port = config_client.get_property('promotion_service_server_port')
        dbname = config_client.get_property('promotion_service_dbname')
        db_hostname = config_client.get_property('promotion_service_db_hostname')
        
        handler = PromotionServiceHandler(dbname, db_hostname)
        processor = PromotionService.Processor(handler)
        transport = TSocket.TServerSocket(port=port)
        tfactory = TTransport.TFramedTransportFactory()
        pfactory = TBinaryProtocolFactory()
        server = TServer.TThreadedServer(processor, transport, tfactory, pfactory)
        print "Starting Promotion Service at, port " + str(port) + " host " + host_name
        sys.stdout.flush()
        server.serve()
        print "Server functioning"
    
if __name__ == "__main__":
    parser = optparse.OptionParser()
    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()
    daemon = PromotionServer(options.logfile, options.pidfile)
    if len(args) == 0:
        daemon.run()
    elif len(args) == 1:
        if 'start' == args[0]:
            daemon.start()
        elif 'stop' == args[0]:
            daemon.stop()
        elif 'restart' == args[0]:
            daemon.restart()
        else:
            print "Unknown command"
            sys.exit(2)
        sys.exit(0)
    else:
        print "usage: %s start|stop|restart" % sys.argv[0]
        sys.exit(2)