Subversion Repositories SmartDukaan

Rev

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

'''
Created on 28-Jul-2011

@author: Chandranshu
'''
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.purchase.main.handler.PurchaseServiceHandler import PurchaseServiceHandler
from shop2020.thriftpy.purchase import PurchaseService

class PurchaseServer(Daemon):
    
    def __init__(self, logfile='/var/log/services/purchase.log', pidfile='/tmp/purchase-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('purchase_service_server_host')
        port = int(config_client.get_property('purchase_service_server_port'))
        dbname = config_client.get_property('purchase_service_dbname')
        db_hostname=config_client.get_property('purchase_service_db_hostname')
    
        handler = PurchaseServiceHandler(dbname, db_hostname)
        processor = PurchaseService.Processor(handler)
        transport = TSocket.TServerSocket(port=port)
        tfactory = TTransport.TFramedTransportFactory()
        pfactory = TBinaryProtocolFactory()
        server = TServer.TThreadedServer(processor, transport, tfactory, pfactory)
        print "Starting Purchase Service at, port "+ str(port)+" host "+host_name
        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 = PurchaseServer(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)